blob: c209dbb5c390ea009352633d68fe6d359de97c21 [file] [log] [blame]
<!doctype html>
<html class="no-js" lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Creating an Enterprise Application Using Maven</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Creating an Enterprise Application Using Maven - Apache NetBeans">
<meta name="author" content="Apache NetBeans">
<meta name="description" content="Creating an Enterprise Application Using Maven - Apache NetBeans">
<meta name="keywords" content="Apache NetBeans, Tutorials, Creating an Enterprise Application Using Maven">
<meta name="generator" content="Apache NetBeans">
<link rel="stylesheet" href="../../../../../../_/css/font-awesome.min.css">
<link rel="alternate" type="application/atom+xml" title="Apache NetBeans Blog" href="https://netbeans.apache.org/blogs/atom" />
<link rel="stylesheet" href="../../../../../../_/css/highlightjs/default.min.css">
<link rel="stylesheet" href="../../../../../../_/css/netbeans.css">
<link rel="apple-touch-icon" sizes="180x180" href="../../../../../../_/images/fav/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="../../../../../../_/images/fav/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="../../../../../../_/images/fav/favicon-16x16.png">
<link rel="manifest" href="../../../../../../_/images/fav/site.webmanifest">
<link rel="mask-icon" href="../../../../../../_/images/fav/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#ffc40d">
<meta name="theme-color" content="#ffffff">
<link href="../../../../../../_/css/font-open-sans.css" rel="stylesheet">
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
</head>
<body>
<div class="title-bar" data-responsive-toggle="responsive-menu" data-hide-for="medium">
<button type="button" data-toggle="responsive-menu"><i style='font-size: 32px; color: #fff; padding: 8px' class='fa fa-bars'></i></button>
<div class="title-bar-title">Apache NetBeans</div>
</div>
<div class="top-bar" id="responsive-menu">
<div class='top-bar-left'>
<a class='title' href="../../../../../../index.html"><img src='../../../../../../_/images/apache-netbeans.svg' style='padding: 8px; height: 48px;'> Apache NetBeans</a>
</div>
<div class="top-bar-right">
<ul class="vertical medium-horizontal menu" data-responsive-menu="drilldown medium-dropdown">
<li> <input id="search-input" type="text" placeholder="Search the docs"> </li>
<li> <a href="../../../../../../front/main/community">Community</a> </li>
<li> <a href="../../../../../../front/main/participate">Participate</a> </li>
<li> <a href="../../../../../../front/main/blogs">Blog</a></li>
<li> <a href="../../../../../../front/main/help">Get Help</a> </li>
<li> <a href="https://plugins.netbeans.apache.org/">Plugins</a> </li>
<li> <a href="../../../../../../front/main/download">Download</a> </li>
</ul>
</div>
</div>
<!-- src/templates/news -->
<section class="hero news alternate">
<div class='grid-container'>
<div class='cell'>
<div class="annotation">Latest release</div>
<h1>Apache NetBeans 28</h1>
<p><a class="button success" href="../../../../../../front/main/download/nb28">Download</a></p>
</div>
</div>
</section>
<div class='grid-container main-content tutorial'>
<article class="doc">
<h1 class="sect0">Creating an Enterprise Application Using Maven</h1>
<div class="sectionbody">
<div class="admonitionblock note">
<table>
<tbody><tr>
<td class="icon"><i class="fa icon-note" title="Note"></i></td>
<td class="content">This tutorial needs a review.
You can <a href="https://github.com/apache/netbeans-antora-tutorials/edit/main/modules/ROOT/pages/kb/docs/javaee/maven-entapp.adoc" title="Edit this tutorial in github">edit it in GitHub </a>
following these <a href="../../../../../../tutorial/main/kb/docs/contributing">contribution guidelines.</a></td>
</tr></tbody>
</table>
</div>
</div>
<div id="toc" class="toc">
<div id="toctitle"></div>
<ul class="sectlevel1">
<li><a href="#_using_maven_in_the_ide">Using Maven in the IDE</a></li>
<li><a href="#_creating_the_maven_enterprise_application_project">Creating the Maven Enterprise Application Project</a></li>
<li><a href="#_coding_the_ejb_project">Coding the EJB Project</a>
<ul class="sectlevel2">
<li><a href="#_creating_the_persistence_unit">Creating the Persistence Unit</a></li>
<li><a href="#_creating_the_entity_class">Creating the Entity Class</a></li>
<li><a href="#_creating_the_message_driven_bean">Creating the Message-Driven Bean</a></li>
<li><a href="#_creating_the_session_bean">Creating the Session Bean</a></li>
</ul>
</li>
<li><a href="#_coding_the_web_application">Coding the Web Application</a>
<ul class="sectlevel2">
<li><a href="#_creating_the_listnews_servlet">Creating the ListNews Servlet</a></li>
<li><a href="#_creating_the_postmessage_servlet">Creating the PostMessage Servlet</a></li>
</ul>
</li>
<li><a href="#_building_the_application_with_maven">Building the Application with Maven</a>
<ul class="sectlevel2">
<li><a href="#_working_with_project_dependencies">Working With Project Dependencies</a></li>
<li><a href="#_building_with_dependencies">Building with Dependencies</a></li>
</ul>
</li>
<li><a href="#_deploying_and_running_the_application">Deploying and Running the Application</a>
<ul class="sectlevel2">
<li><a href="#_deploying_and_running_the_application_from_the_ide">Deploying and Running the Application from the IDE</a></li>
<li><a href="#_deploying_from_the_glassfish_admin_console">Deploying From the GlassFish Admin Console</a></li>
</ul>
</li>
<li><a href="#_downloading_the_solution_project">Downloading the Solution Project</a></li>
<li><a href="#_see_also">See Also</a></li>
</ul>
</div>
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>This tutorial demonstrates how to create a simple enterprise application using NetBeans IDE and Maven archetypes. The tutorial also provides some explanations and tips on using the IDE to create and build Maven applications.</p>
</div>
<div class="paragraph">
<p>In this tutorial you create an enterprise application that is packaged as an EAR archive and deployed to GlassFish Server Open Source Edition 3. You create the application using the Maven Enterprise Application archetype in the New Project wizard. The application contains an EJB project that contains a persistent entity class, a message-driven bean and a session bean facade for the entity. The application also contains a web project that contains two servlets.</p>
</div>
<div class="paragraph">
<p><strong>Tutorial Exercises</strong></p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#intro">Using Maven in the IDE</a></p>
</li>
<li>
<p><a href="#Exercise_1">Creating the Maven Enterprise Application Project</a></p>
</li>
<li>
<p><a href="#Exercise_2">Coding the EJB Module</a></p>
</li>
<li>
<p><a href="#Exercise_2a">Creating the Persistence Unit</a></p>
</li>
<li>
<p><a href="#Exercise_2b">Creating the Entity Class</a></p>
</li>
<li>
<p><a href="#Exercise_2c">Creating the Message-Driven Bean</a></p>
</li>
<li>
<p><a href="#Exercise_2d">Creating the Session Facade</a></p>
</li>
<li>
<p><a href="#Exercise_3">Coding the Web Module</a></p>
</li>
<li>
<p><a href="#Exercise_3a">Creating the ListNews Servlet</a></p>
</li>
<li>
<p><a href="#Exercise_3b">Creating the PostMessage Servlet</a></p>
</li>
<li>
<p><a href="#Exercise_4">Building the Application with Maven</a></p>
</li>
<li>
<p><a href="#Exercise_4a">Working with Project Dependencies</a></p>
</li>
<li>
<p><a href="#Exercise_4b">Building with Dependencies</a></p>
</li>
<li>
<p><a href="#Exercise_5">Deploying and Running the Application</a></p>
</li>
<li>
<p><a href="#Exercise_5a">Deploying From the IDE</a></p>
</li>
<li>
<p><a href="#Exercise_5b">Deploying From the GlassFish Server Admin Console</a></p>
</li>
<li>
<p><a href="#Exercise_7">Downloading the Solution Project</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p><strong>To follow this tutorial, you need the following software and resources.</strong></p>
</div>
<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Software or Resource</th>
<th class="tableblock halign-left valign-top">Version Required</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="../../../../../../front/main/download/" class="xref page">NetBeans IDE</a></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">7.2, 7.3, 7.4, 8.0, Java EE version</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Java Development Kit (JDK)</a></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">version 7 or 8</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">GlassFish Server Open Source Edition</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">4.x</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p><strong>Prerequisites</strong></p>
</div>
<div class="paragraph">
<p>This document assumes you have some basic knowledge of, or programming experience with, the following technologies:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Java Programming</p>
</li>
<li>
<p>NetBeans IDE</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Before starting this tutorial you may want to familiarize yourself with the following documentation.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="../../../../../../wiki/main/wiki/MavenBestPractices/" class="xref page">Best Practices for Apache Maven in NetBeans IDE</a></p>
</li>
<li>
<p><a href="http://books.sonatype.com/mvnref-book/reference/introduction.html">Chapter 1. Introducing Apache Maven</a> (from <a href="http://books.sonatype.com/mvnref-book/reference/index.html">+Maven: The Complete Reference +</a>)</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>You can download <a href="https://netbeans.org/projects/samples/downloads/download/Samples%252FJavaEE%252FMavenEnterpriseApp.zip">a zip archive of the finished project</a>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_using_maven_in_the_ide"><a class="anchor" href="#_using_maven_in_the_ide"></a>Using Maven in the IDE</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Support for Maven is fully integrated in NetBeans IDE. Developing a project that uses the Maven framework is nearly identical to developing a project in Ant. However, there are some differences that are related to the way Maven builds projects and works with dependencies. The following tips can help you avoid some problems if you are creating a Maven application for the first time.</p>
</div>
<div class="paragraph">
<p><strong>Check your Maven settings</strong></p>
</div>
<div class="paragraph">
<p>If this is your first Maven project you will want to check the Maven configuration settings in the Options window. Maven is bundled with the IDE and installed when you install the IDE.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Open the Options window in the IDE (Tools &gt; Options; NetBeans &gt; Preferences on Mac).</p>
</li>
<li>
<p>Select the Java category in the Options window and click the Maven tab.</p>
</li>
<li>
<p>Confirm that a Maven Home is specified.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>You can use the Maven version bundled with the IDE or specify the location of a local Maven installation (requires 2.0.9 or newer).</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Click OK to close the Options window.</p>
</li>
</ol>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
Maven support is automatically enabled when Java is enabled in the IDE. You will need to enable the Java EE plugin if it is not enabled.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p><strong>Update your Maven repositories</strong></p>
</div>
<div class="paragraph">
<p>Local and remote Maven repositories are used for code completion and when building projects. You should update the indexes for the Maven remote repositories to ensure that any artifacts you may need are readily available when you develop your project. You can configure how often the IDE checks for updates in the Maven tab of the Options window. You can perform an immediate check for updates and explore your local and remote Maven repositories in the Services window.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Choose Window &gt; Services to open the Services window.</p>
</li>
<li>
<p>Expand the Maven Repositories node in the Services window.</p>
</li>
<li>
<p>Expand a repository node to view the artifacts.</p>
</li>
<li>
<p>Right-click the repository node and choose Update Index in the popup menu.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When you click Update Indexes, the IDE will check and download the most recent index for each of your Maven remote repositories. An index represents the current state of the artifacts located in the repository and is used to provide a reference to the artifacts that are available to your application. By default, the IDE does not download an artifact from a repository until the artifact is explicitly required.</p>
</div>
<div class="paragraph">
<p>You can search for an artifact by right-clicking the Maven Repositories node in the Services window and choosing Find.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<div class="ulist">
<ul>
<li>
<p>Indexes are quite large and it can take some time to update them all.</p>
</li>
<li>
<p>If you are using NetBeans IDE 7.1 or earlier you will need to choose Window &gt; Other &gt; Maven Repository Browser and click the Update Indexes ( images:./maven-refreshrepo.png[title="Update Indexes button"]  ) at the top of the Maven Repository Browser window.</p>
</li>
</ul>
</div>
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>For more details about using Maven in NetBeans IDE, see the section on <a href="../../java/maven-hib-java-se/#02" class="xref page">Configuring Maven</a> in the <a href="../../java/maven-hib-java-se/" class="xref page">Creating a Maven Swing Application Using Hibernate</a> tutorial and also <a href="../../../../../../wiki/main/wiki/MavenBestPractices/" class="xref page">Best Practices for Apache Maven in NetBeans IDE</a>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_creating_the_maven_enterprise_application_project"><a class="anchor" href="#_creating_the_maven_enterprise_application_project"></a>Creating the Maven Enterprise Application Project</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The goal of this exercise is to create an enterprise application project using the Maven Enterprise Application archetype bundled with the IDE. The enterprise application archetype will also create an EJB project and a webapp project.</p>
</div>
<div class="paragraph">
<p>The IDE includes several Maven archetypes in the New Project wizard to help you quickly create common NetBeans project types such as enterprise application projects (EAR), web application projects (WAR) and EJB module projects (JAR). The wizard also enables you to create projects from archetypes in registered remote repositories.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Choose File &gt; New Project (Ctrl-Shift-N; ⌘-Shift-N on Mac) from the main menu.</p>
</li>
<li>
<p>Select Enterprise Application from the Maven category. Click Next.</p>
</li>
</ol>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-newproject1.png" alt="maven newproject1">
</div>
<div class="title">Figure 1. Maven Enterprise Application project type in the New Project wizard</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Type <strong>MavenEnterpriseApp</strong> for the the Project Name and set the Project Location.</p>
</li>
<li>
<p>(Optional) Modify the artifact details. Click Next.</p>
</li>
</ol>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-newproject2.png" alt="maven newproject2">
</div>
<div class="title">Figure 2. Maven Project details in the New Project wizard</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Select GlassFish Server for the Server.</p>
</li>
<li>
<p>Set the Java EE Version to Java EE 6 or Java EE 7.</p>
</li>
<li>
<p>Select Create EJB Module and Create Web App Module. Click Finish.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When you click Finish, the IDE creates the following projects from the Maven Enterprise Application archetype.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>EJB.</strong> (MavenEnterpriseApp-ejb) The EJB project generally contains the source code with the business logic of the application. The EJB project is packaged as an EJB JAR archive.</p>
</li>
<li>
<p><strong>Webapp.</strong> (MavenEnterpriseApp-web) The Webapp project generally contains the presentation layer of the application such as JSF and JSP pages and servlets. The Webapp project may also contain source code with business logic. The Webapp project is packaged as a WAR archive.</p>
</li>
<li>
<p><strong>Assembly.</strong> (MavenEnterpriseApp) The Assembly project is used to assemble an EAR archive from the EJB and WAR archives. The Assembly project does not contain any sources.</p>
</li>
<li>
<p><strong>Enterprise Application.</strong> (MavenEnterpriseApp-ear) The Enterprise Application project does not contain any sources. The Enterprise Application only contains a POM file ( <code>pom.xml</code> ) with details about the modules contained in the enterprise application.</p>
</li>
</ul>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-projectswindow2.png" alt="maven projectswindow2">
</div>
<div class="title">Figure 3. Projects window showing generated projects</div>
</div>
<div class="paragraph">
<p>After you create the enterprise application project, the enterprise application project will be badged if any dependencies are unavailable. If you expand the Dependencies node for the MavenEnterpriseApp-ear project, you can see if any required libraries are missing or not on the classpath. The enterprise application project has dependencies on the JAR and WAR that will be packaged and available after you compile the EJB project and the web project. You can see that <code>MavenEnterpriseApp-ejb</code> and <code>MavenEnterpriseApp-web</code> are listed as dependencies.</p>
</div>
<div class="paragraph">
<p>In some cases you will see a Maven icon in the status bar that you can click to run a priming build and resolve missing dependencies.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/priming-build.png" alt="priming build">
</div>
<div class="title">Figure 4. The Maven icon in the status bar enables you to run a priming build</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_coding_the_ejb_project"><a class="anchor" href="#_coding_the_ejb_project"></a>Coding the EJB Project</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The EJB project contains the business logic of the application. In this application the GlassFish container will manage transactions using the Java Transaction API (JTA). In this tutorial you will create an entity class, a message-driven bean and a session facade for the entity class in the EJB project.</p>
</div>
<div class="sect2">
<h3 id="_creating_the_persistence_unit"><a class="anchor" href="#_creating_the_persistence_unit"></a>Creating the Persistence Unit</h3>
<div class="paragraph">
<p>In this exercise you create a persistence unit in the EJB project. The persistence unit specifies the database connection details and specifies how transactions are managed. For this application you will specify JTA in the New Persistence Unit wizard because you want the GlassFish server to manage the transactions.</p>
</div>
<div class="paragraph">
<p>To create the persistence unit, perform the following steps.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click the EJB project node and choose New &gt; Other from the popup menu to open the New File wizard.</p>
</li>
<li>
<p>Select Persistence Unit from the Persistence category. Click Next.</p>
</li>
<li>
<p>Select EclipseLink as the Persistence Provider in the New Persistence Unit dialog box.</p>
</li>
<li>
<p>Select a datasource (for example, select <code>jdbc/sample</code> if you want to use JavaDB).</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>The datasource <code>jdbc/sample</code> is bundled with the IDE when you install the IDE and the GlassFish server, but you can specify a different datasource if you want to use a different database.</p>
</div>
<div class="paragraph">
<p>You can keep the other default options (persistence unit name, EclipseLink persistence provider).</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Ensure that Use Java Transaction APIs is selected and that the Table Generation Strategy is set to Create so that the tables based on your entity classes are created when the application is deployed. Click Finish.</p>
</li>
</ol>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-persistenceunit.png" alt="maven persistenceunit">
</div>
<div class="title">Figure 5. New Persistence Unit wizard</div>
</div>
<div class="paragraph">
<p>When you click Finish, the IDE creates the XML file <code>persistence.xml</code> and opens the file in the editor. In the Projects window, you can see that the file was created in the <code>Other Sources &gt; src/main/resources &gt; META-INF</code> directory. The file contains details about connecting to the database and how transactions are managed. If you click the Source tab in the editor, you can see the following details about the persistence unit.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml">...
&lt;persistence-unit name="com.mycompany_MavenEnterpriseApp-ejb_ejb_1.0-SNAPSHOTPU" transaction-type="JTA"&gt;
&lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
&lt;jta-data-source&gt;jdbc/sample&lt;/jta-data-source&gt;
&lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
&lt;properties&gt;
&lt;property name="eclipselink.ddl-generation" value="create-tables"/&gt;
&lt;/properties&gt;
&lt;/persistence-unit&gt;</code></pre>
</div>
</div>
<div class="paragraph">
<p>You can see that <code>JTA</code> is specified as the transaction type and that the application will use the registered datasource <code>jdbc/sample</code> .</p>
</div>
</div>
<div class="sect2">
<h3 id="_creating_the_entity_class"><a class="anchor" href="#_creating_the_entity_class"></a>Creating the Entity Class</h3>
<div class="paragraph">
<p>In this exercise you will create an entity class in the EJB project to represent the objects that will be persisted to the database. To create the NewsEntity entity class, perform the following steps.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click the EJB module in the Projects window and choose New &gt; Other to open the New File wizard.</p>
</li>
<li>
<p>Select Entity Class from the Persistence category. Click Next.</p>
</li>
<li>
<p>Type <strong>NewsEntity</strong> for the Class Name.</p>
</li>
<li>
<p>Type <strong>ejb</strong> for the Package and leave the Primary Key Type as Long. Click Finish.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When you click Finish, the entity class <code>NewsEntity.java</code> opens in the Source Editor. In the Source Editor, add some fields by performing the following steps.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Add the following field declarations to the class.</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">private String title;
private String body;</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click in the Source Editor between the class definition and choose Insert Code (Alt-Insert; Ctrl-I on Mac) &gt; Getter and Setter.</p>
</li>
<li>
<p>In the Generate Getters and Setters dialog box, select the <code>body</code> and <code>title</code> fields. Click Generate.</p>
</li>
<li>
<p>Save your changes to the class.</p>
</li>
</ol>
</div>
</div>
<div class="sect2">
<h3 id="_creating_the_message_driven_bean"><a class="anchor" href="#_creating_the_message_driven_bean"></a>Creating the Message-Driven Bean</h3>
<div class="paragraph">
<p>In this exercise you will create a message-driven bean in the EJB project. A message-driven bean is an enterprise bean that enables the asynchronous exchange of messages. The NewsApp application uses a message-driven bean to receive and process messages sent to the queue by a servlet in the web module.</p>
</div>
<div class="paragraph">
<p>To use a message-driven bean in an application, the connector resources used by the bean need to be registered with the server. When you are deploying to the GlassFish server, you can create the resources directly on the server through the Admin Console or you can create the resources on deployment by specifying the details in the <code>glassfish-resources.xml</code> descriptor file. When the application is deployed to the server, the server registers the resources based on the descriptor file. When you use the New File wizard in the IDE to create a message-driven bean, the IDE will generate the elements in the descriptor file for you.</p>
</div>
<div class="paragraph">
<p>In a Maven project, the <code>glassfish-resources.xml</code> file is located in the <code>src/main/setup</code> directory under the project node in the Files window.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click the EJB module in the Projects window and choose New &gt; Other to open the New File wizard.</p>
</li>
<li>
<p>From the Enterprise JavaBeans category, select Message-Driven Bean. Click Next.</p>
</li>
<li>
<p>Type <strong>NewMessage</strong> for the EJB Name.</p>
</li>
<li>
<p>Select <strong>ejb</strong> from the Package drop-down list.</p>
</li>
<li>
<p>Click the Add button next to the Project Destination field to open the Add Message Destination dialog box.</p>
</li>
<li>
<p>In the Add Message Destination dialog box, type <strong>jms/NewMessage</strong> and select Queue for the destination type. Click OK.</p>
</li>
</ol>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-messagedestination.png" alt="maven messagedestination">
</div>
<div class="title">Figure 6. Add Message Destination dialog</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Confirm that the project destination is correct. Click Next.</p>
</li>
</ol>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-newmdb.png" alt="maven newmdb">
</div>
<div class="title">Figure 7. New Message-Driven Bean wizard</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Accept the default settings in the Activation Config Properties. Click Finish.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When you click Finish, the IDE generates the bean class and adds the following annotations that identify the class as a message-driven bean and configuration properties.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@MessageDriven(mappedName = "jms/NewMessage", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class NewMessage implements MessageListener {
public NewMessage() {
}
@Override
public void onMessage(Message message) {
}
}</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Inject the <code>MessageDrivenContext</code> resource into the class by adding the following annotated field to the class.</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">public class NewMessage implements MessageListener {
*@Resource
private MessageDrivenContext mdc;*</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Introduce the entity manager into the class by adding the following annotated field (in bold).</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">public class NewMessage implements MessageListener {
@Resource
private MessageDrivenContext mdc;
*@PersistenceContext(unitName="com.mycompany_MavenEnterpriseApp-ejb_ejb_1.0-SNAPSHOTPU")
private EntityManager em;*</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>@PersistenceContext</code> annotation specifies the context by declaring the persistence unit. The <code>unitName</code> value is the name of the persistence unit.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Add the following <code>save</code> method (in bold).</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">public NewMessage() {
}
@Override
public void onMessage(Message message) {
}
*private void save(Object object) {
em.persist(object);
}*</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Modify the <code>onMessage</code> method by adding the following (in bold) to the body:</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">public void onMessage(Message message) {
*ObjectMessage msg = null;
try {
if (message instanceof ObjectMessage) {
msg = (ObjectMessage) message;
NewsEntity e = (NewsEntity) msg.getObject();
save(e);
}
} catch (JMSException e) {
e.printStackTrace();
mdc.setRollbackOnly();
} catch (Throwable te) {
te.printStackTrace();
}*
}</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Fix your import statements (Ctrl-Shift-I; ⌘-Shift-I on Mac) and save your changes.</p>
</li>
</ol>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
When generating the import statements, you want to make sure to import the * <code>jms</code> * and * <code>javax.annotation.Resource</code> * libraries.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>For more details about message-driven beans, see the chapter <a href="http://download.oracle.com/javaee/6/tutorial/doc/gipko.html">What is a Message-Driven Bean?</a> in the <a href="http://download.oracle.com/javaee/6/tutorial/doc/index.html">Java EE 6 Tutorial, Part I</a>.</p>
</div>
</div>
<div class="sect2">
<h3 id="_creating_the_session_bean"><a class="anchor" href="#_creating_the_session_bean"></a>Creating the Session Bean</h3>
<div class="paragraph">
<p>In this exercise you will use a wizard to create a session bean facade for the NewsEntity entity class. The wizard will generate <code>create</code> , <code>edit</code> and <code>find</code> methods that will be accessed from the servlets in the webapp project.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click the EJB module and choose New &gt; Other.</p>
</li>
<li>
<p>From the Persistence category, select Session Beans for Entity Classes and click Next.</p>
</li>
<li>
<p>Select <strong>ejb.NewsEntity</strong> from the list of available entity classes and click Add to move the class to the Selected Entity Classes pane. Click Next.</p>
</li>
<li>
<p>Select <strong>ejb</strong> as the Package. Click Finish.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When you click Finish the IDE generates two session facade classes: <code>AbstractFacade.java</code> , and <code>NewsEntityFacade.java</code> which extends the abstract facade class. The abstract facade class defines several methods that are commonly used with entity classes.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-sessionwizard.png" alt="maven sessionwizard">
</div>
<div class="title">Figure 8. Session Beans for Entity Classes wizard</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_coding_the_web_application"><a class="anchor" href="#_coding_the_web_application"></a>Coding the Web Application</h2>
<div class="sectionbody">
<div class="paragraph">
<p>In this section you will create two servlets in the webapp project.</p>
</div>
<div class="sect2">
<h3 id="_creating_the_listnews_servlet"><a class="anchor" href="#_creating_the_listnews_servlet"></a>Creating the ListNews Servlet</h3>
<div class="paragraph">
<p>In this exercise you will create the ListNews servlet that will be used to display a list of posted messages. You will use annotations to inject the session facade and access the <code>findAll</code> method and retrieve the posted messages.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click the web module project and choose New &gt; Servlet.</p>
</li>
<li>
<p>Type <strong>ListNews</strong> for the Class Name.</p>
</li>
<li>
<p>Type <strong>web</strong> for the Package name. Click Finish.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When you click Finish, the class <code>ListNews.java</code> opens in the Source Editor.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click in the Source Editor between the class definition and choose Insert Code (Alt-Insert; Ctrl-I on Mac) &gt; Call Enterprise Bean.</p>
</li>
<li>
<p>In the Call Enterprise Bean dialog box, expand the MavenEnterpriseApp-ejb node and select NewsEntityFacade. Click OK.</p>
</li>
</ol>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-callbean.png" alt="maven callbean">
</div>
<div class="title">Figure 9. Call Enterprise Bean dialog</div>
</div>
<div class="paragraph">
<p>When you click OK, the EJB resource is injected into the servlet using the <code>@EJB</code> annotation.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@WebServlet(name = "ListNews", urlPatterns = {"/ListNews"})
public class ListNews extends HttpServlet {
@EJB
private NewsEntityFacade newsEntityFacade;</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>In the <code>processRequest</code> method, modify the method by adding the following lines (in bold) to the body of the method:</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml">out.println("&lt;h1&gt;Servlet ListNews at " + request.getContextPath () + "&lt;/h1&gt;");
*
List news = newsEntityFacade.findAll();
for (Iterator it = news.iterator(); it.hasNext();) {
NewsEntity elem = (NewsEntity) it.next();
out.println(" &lt;b&gt;"+elem.getTitle()+" &lt;/b&gt;&lt;br /&gt;");
out.println(elem.getBody()+"&lt;br /&gt; ");
}
out.println("&lt;a href='PostMessage'&gt;Add new message&lt;/a&gt;");
*
out.println("&lt;/body&gt;");</code></pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
You might need to uncomment the code if you are using an earlier version of the IDE.
</td>
</tr>
</table>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Fix your imports (Ctrl-Shift-I; ⌘-Shift-I on Mac) and save your changes.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When generating the import statements, you want to import the <code>java.util</code> libraries.</p>
</div>
</div>
<div class="sect2">
<h3 id="_creating_the_postmessage_servlet"><a class="anchor" href="#_creating_the_postmessage_servlet"></a>Creating the PostMessage Servlet</h3>
<div class="paragraph">
<p>In this exercise you will create the PostMessage servlet that will be used to post messages. You will use annotations to inject the JMS resources you created directly into the servlet, specifying the variable name and the name to which it is mapped. You will then add the code to send the JMS message and the code for the HTML form for adding a message.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click the web module project and choose New &gt; Servlet.</p>
</li>
<li>
<p>Type <strong>PostMessage</strong> for the Class Name.</p>
</li>
<li>
<p>Select <strong>web</strong> for the Package name. Click Finish.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When you click Finish, the class <code>PostMessage.java</code> opens in the Source Editor.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>In the Source Editor, use annotations to inject the <code>ConnectionFactory</code> and <code>Queue</code> resources by adding the following field declarations.</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@WebServlet(name="PostMessage", urlPatterns={"/PostMessage"})
public class PostMessage extends HttpServlet {
*@Resource(mappedName="jms/NewMessageFactory")
private ConnectionFactory connectionFactory;
@Resource(mappedName="jms/NewMessage")
private Queue queue;*</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Fix your imports to import the * <code>javax.jms</code> * libraries.</p>
</li>
</ol>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-searchdepend1.png" alt="maven searchdepend1">
</div>
<div class="title">Figure 10. Fix All Imports dialog</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
If the IDE does not offer <code>javax.jms</code> as an option, you can search the repositories for the correct artifact by clicking the suggestion icon in the margin next to <code>private ConnectionFactory connectionFactory;</code> and choosing Search Dependency at Maven Repositories.
</td>
</tr>
</table>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-searchdependencies.png" alt="maven searchdependencies">
</div>
<div class="title">Figure 11. Hint in editor to search repositories for dependencies</div>
</div>
<div class="paragraph">
<p>You can use the Search in Maven Repositories dialog to locate the <code>javaee-api-6.0</code> artifact that contains <code>ConnectionFactory</code> .</p>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-searchdepend2.png" alt="maven searchdepend2">
</div>
<div class="title">Figure 12. Search in Maven Repositories dialog</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Add the following code to send the JMS messages to the <code>processRequest</code> method.</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">response.setContentType("text/html;charset=UTF-8");
*// Add the following code to send the JMS message
String title=request.getParameter("title");
String body=request.getParameter("body");
if ((title!=null) &amp;amp;&amp;amp; (body!=null)) {
try {
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(queue);
ObjectMessage message = session.createObjectMessage();
// here we create NewsEntity, that will be sent in JMS message
NewsEntity e = new NewsEntity();
e.setTitle(title);
e.setBody(body);
message.setObject(e);
messageProducer.send(message);
messageProducer.close();
connection.close();
response.sendRedirect("ListNews");
} catch (JMSException ex) {
ex.printStackTrace();
}
}*</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Add the following code (in bold) for the web form for adding a message.</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml">out.println("Servlet PostMessage at " + request.getContextPath() + "&lt;/h1&gt;");
*// The following code adds the form to the web page
out.println("&lt;form&gt;");
out.println("Title: &lt;input type='text' name='title'&gt;&lt;br/&gt;");
out.println("Message: &lt;textarea name='body'&gt;&lt;/textarea&gt;&lt;br/&gt;");
out.println("&lt;input type='submit'&gt;&lt;br/&gt;");
out.println("&lt;/form&gt;");
*
out.println("&lt;/body&gt;");</code></pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
You might need to uncomment the code if you are using an earlier version of the IDE.
</td>
</tr>
</table>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Fix your imports and save your changes.</p>
</li>
</ol>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-jms-imports.png" alt="maven jms imports">
</div>
<div class="title">Figure 13. Fix All Imports dialog</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
You want to import the * <code>javax.jms</code> * libraries for <code>Connection</code> , <code>ConnectionFactory</code> , <code>Session</code> and <code>Queue</code> .
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_building_the_application_with_maven"><a class="anchor" href="#_building_the_application_with_maven"></a>Building the Application with Maven</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Now that you are finished coding the application, you can use Maven to build the enterprise application. In this section you will build and package the projects in an EAR archive. The EAR archive will contain an EJB JAR archive and a WAR archive. After you create the EAR archive you can deploy the archive to the target server.</p>
</div>
<div class="sect2">
<h3 id="_working_with_project_dependencies"><a class="anchor" href="#_working_with_project_dependencies"></a>Working With Project Dependencies</h3>
<div class="paragraph">
<p>In this exercise you will examine the POM ( <code>pom.xml</code> ) of the web project and modify the POM to prevent unnecessary artifacts from being included in the WAR when packaging. Each Maven project contains a <code>pom.xml</code> file that contains details about the contents of archives. Any external libraries that are required by the project are listed as dependencies in the POM. You can modify the POM to specify the dependencies that need to be included or should be excluded when packaging the archive.</p>
</div>
<div class="paragraph">
<p>In this application, the EJB JAR and the WAR archives will be packaged in an EAR archive. If you look at the <code>pom.xml</code> of the MavenEnterpriseApp-ear project, you can see that the EJB and WAR are declared as dependencies.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-earpom.png" alt="maven earpom">
</div>
<div class="title">Figure 14. pom.xml of EAR project</div>
</div>
<div class="paragraph">
<p>If you look at the <code>pom.xml</code> of the web project in the editor you can see that the EJB archive is declared as a dependency and the scope is specified as <code>provided</code> . When the value of the scope element of an artifact is <code>provided</code> , the artifact will not be included during packaging. The web project requires the EJB archive as a dependency, but for this application you do not want the EJB archive to be included in the WAR during packaging because the EJB archive will be available and provided as part of the EAR archive.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-webpom.png" alt="maven webpom">
</div>
<div class="title">Figure 15. pom.xml of Web App project</div>
</div>
<div class="paragraph">
<p>You can open <code>pom.xml</code> in the editor and click the Graph tab to see a visual representation of the project dependencies. If you are using an older version of the IDE you can right-click in <code>pom.xml</code> in the editor and choose Show Dependency Graph. You can place your cursor over an artifact to display a tooltip with the artifact details.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-webpomgraph.png" alt="maven webpomgraph">
</div>
<div class="title">Figure 16. Graph of dependencies</div>
</div>
<div class="paragraph">
<p>Perform the following steps to modify the POM of the web project to add a <code>scope</code> element to the dependency on the <code>javaee-api</code> artifact.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Expand the Project Files node under the web project.</p>
</li>
<li>
<p>Double-click <code>pom.xml</code> to open the file in the editor.</p>
</li>
<li>
<p>Confirm that <code>provided</code> is set for the value of the <code>&lt;scope&gt;</code> for the <code>javaee-api</code> artifact.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>If the value is not <code>provided</code> you need to edit the POM to make the following changes:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml">&lt;dependency&gt;
&lt;groupId&gt;javax&lt;/groupId&gt;
&lt;artifactId&gt;javaee-api&lt;/artifactId&gt;
&lt;version&gt;7.0&lt;/version&gt;
&lt;type&gt;jar&lt;/type&gt;
*&lt;scope&gt;provided&lt;/scope&gt;*
&lt;/dependency&gt;</code></pre>
</div>
</div>
<div class="paragraph">
<p>You can use the code completion in the POM editor to help you edit the file.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-addscope-javaee.png" alt="maven addscope javaee">
</div>
<div class="title">Figure 17. code completion for scope element in POM</div>
</div>
<div class="paragraph">
<p>By declaring that the dependency is provided, Maven will not package the artifact when building the WAR archive.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Save your changes.</p>
</li>
</ol>
</div>
</div>
<div class="sect2">
<h3 id="_building_with_dependencies"><a class="anchor" href="#_building_with_dependencies"></a>Building with Dependencies</h3>
<div class="paragraph">
<p>The Maven build framework proceeds through a specific sequence of phases, and each phase consists of one or more goals and can be configured to use various Maven plugins. The Build with Dependencies menu item is mapped to the <code>install</code> phase of the Maven build lifecycle and is configured to use the Reactor plugin. When you choose Build with Dependencies in the popup menu, Maven builds the application and any required dependencies and copies the build artifacts to the local repository.</p>
</div>
<div class="paragraph">
<p>You can modify how Maven phases and goals are mapped to menu actions in the Actions panel of the project&#8217;s Properties dialog box.</p>
</div>
<div class="paragraph">
<p>To build the EAR archive, perform the following step.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Right-click the MavenEnterpriseApp-ear project node and choose Build with Dependencies.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>When you build the EAR project using the Reactor plugin, the sub-projects that are dependencies of the EAR project are built before the EAR project is built. The Output window displays the build order.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-reactor1.png" alt="maven reactor1">
</div>
<div class="title">Figure 18. Output window showing Reactor build order</div>
</div>
<div class="paragraph">
<p>The results of the build are also displayed in the Output window.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-reactor2.png" alt="maven reactor2">
</div>
<div class="title">Figure 19. Output window showing Reactor build status</div>
</div>
<div class="paragraph">
<p>After you build the EAR project, you can see the final EAR archive inside the <code>target</code> directory under the EAR project node in the Files window.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-earfileswindow1.png" alt="maven earfileswindow1">
</div>
<div class="title">Figure 20. Files window showing EAR archive</div>
</div>
<div class="paragraph">
<p>If you used the default artifact name <code>com.mycompany</code> , you can use the Maven Repository Browser to view the build artifacts by expanding <code>com.mycompany</code> in the Local Repository.</p>
</div>
<div class="paragraph">
<p>For more details about building Maven projects, see <a href="http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html">Maven - Introduction to the Build Lifecycle</a> at <a href="http://maven.apache.org">maven.apache.org</a>.</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_deploying_and_running_the_application"><a class="anchor" href="#_deploying_and_running_the_application"></a>Deploying and Running the Application</h2>
<div class="sectionbody">
<div class="paragraph">
<p>This section describes two methods for deploying the EAR archive to the server. You can deploy the application to the GlassFish server by using a menu action in the IDE or by using the Deploy tool in the GlassFish Admin Console.</p>
</div>
<div class="sect2">
<h3 id="_deploying_and_running_the_application_from_the_ide"><a class="anchor" href="#_deploying_and_running_the_application_from_the_ide"></a>Deploying and Running the Application from the IDE</h3>
<div class="paragraph">
<p>In this exercise you will use the Run action to deploy the EAR archive to the the GlassFish server. After you deploy the application you will open the ListNews page of the application in your browser and add a message.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click the EAR project node in the Projects window and choose Run.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When you click Run the IDE will deploy the EAR archive and create the JMS resources on the server. The IDE will open the default project index page (<a href="http://localhost:8080/MavenEnterpriseApp-web/">http://localhost:8080/MavenEnterpriseApp-web/</a>) in your browser.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Open your browser to the following URL to display the ListNews page.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p><a href="http://localhost:8080/MavenEnterpriseApp-web/ListNews">http://localhost:8080/MavenEnterpriseApp-web/ListNews</a>.</p>
</div>
<div class="paragraph">
<p>When you first run the project, the database is empty and there are no messages to display.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-browser1.png" alt="maven browser1">
</div>
<div class="title">Figure 21. ListNews page open in the browser</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Click Add new message.</p>
</li>
<li>
<p>Type a message in the form in the PostMessage servlet. Click Submit Query.</p>
</li>
</ol>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-browser2.png" alt="maven browser2">
</div>
<div class="title">Figure 22. PostMessage page open in the browser</div>
</div>
<div class="paragraph">
<p>When you add a message with the PostMessage servlet, the message is sent to the message-driven bean for writing to persistent storage, and the ListNews servlet is called to display the messages in the database. The list of messages in the database retrieved by ListNews often does not yet contain the new message because our message service is asynchronous.</p>
</div>
<div class="paragraph">
<p>Compile on Save and Deploy on Save are enable by default on Maven projects that specify GlassFish server as the target server. For example, if you modify a save a servlet, you can reload the servlet in the browser and view the changes without redeploying the application.</p>
</div>
</div>
<div class="sect2">
<h3 id="_deploying_from_the_glassfish_admin_console"><a class="anchor" href="#_deploying_from_the_glassfish_admin_console"></a>Deploying From the GlassFish Admin Console</h3>
<div class="paragraph">
<p>In this exercise you will deploy the EAR archive using the Deploy tool in the GlassFish Admin Console.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Expand the Servers node in the Services window.</p>
</li>
<li>
<p>Start the GlassFish server.</p>
</li>
<li>
<p>Right-click the GlassFish server node and choose View Admin Console to open the GlassFish Admin Console in your browser.</p>
</li>
<li>
<p>Click the Applications node in the left pane of the Admin Console.</p>
</li>
<li>
<p>Click the Deploy button in the main pane of the Admin Console.</p>
</li>
<li>
<p>Click Browse to locate the EAR archive for the enterprise application.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>The EAR archive is located in the <code>target</code> directory inside the enterprise application directory on your local system.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Click OK.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When you click OK, the GlassFish deploy tool deploys the application.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
If you deploy the application using the deploy tool in the GlassFish Admin Console, you will also need to manually create the resources that are required by the application if they do not exist.
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_downloading_the_solution_project"><a class="anchor" href="#_downloading_the_solution_project"></a>Downloading the Solution Project</h2>
<div class="sectionbody">
<div class="paragraph">
<p>You can download the solution to this tutorial as a project in the following ways.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Download <a href="https://netbeans.org/projects/samples/downloads/download/Samples%252FJavaEE%252FMavenEnterpriseApp.zip">a zip archive of the finished project</a>.</p>
</li>
<li>
<p>Checkout the project sources from the NetBeans Samples by performing the following steps:</p>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Choose Team &gt; Subversion &gt; Checkout from the main menu.</p>
</li>
<li>
<p>In the Checkout dialog box, enter the following Repository URL:
<code><a href="https://svn.netbeans.org/svn/samples~samples-source-code" class="bare">https://svn.netbeans.org/svn/samples~samples-source-code</a></code>
Click Next.</p>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Click Browse to open the Browse Repostiory Folders dialog box.</p>
</li>
<li>
<p>Expand the root node and select <strong>samples/javaee/MavenEnterpriseApp</strong>. Click OK.</p>
</li>
<li>
<p>Specify the Local Folder for the sources (the local folder must be empty).</p>
</li>
<li>
<p>Click Finish.</p>
</li>
</ol>
</div>
</li>
</ol>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>When you click Finish, the IDE initializes the local folder as a Subversion repository and checks out the project sources.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Click Open Project in the dialog that appears when checkout is complete.</p>
</li>
</ol>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
For more about installing Subversion, see the section on <a href="../../ide/subversion/#settingUp" class="xref page">Setting up Subversion</a> in the <a href="../../ide/subversion/" class="xref page">Guide to Subversion in NetBeans IDE</a>.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p><a href="../../../../../../front/main/community/mailing-lists/" class="xref page">Send Feedback on This Tutorial</a></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_see_also"><a class="anchor" href="#_see_also"></a>See Also</h2>
<div class="sectionbody">
<div class="paragraph">
<p>For more information about using NetBeans IDE to develop Java EE applications, see the following resources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="../javaee-intro/" class="xref page">Introduction to Java EE Technology</a></p>
</li>
<li>
<p><a href="../javaee-gettingstarted/" class="xref page">Getting Started with Java EE Applications</a></p>
</li>
<li>
<p><a href="../maven-entapp-testing/" class="xref page">Testing a Maven Enterprise Application</a></p>
</li>
<li>
<p><a href="../../java-ee/" class="xref page">Java EE &amp;amp; Java Web Learning Trail</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>You can find more information about using Enterprise Beans in the <a href="http://download.oracle.com/javaee/7/tutorial/doc/">Java EE 7 Tutorial</a>.</p>
</div>
<div class="paragraph">
<p>To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java EE development features, <a href="../../../../../../front/main/community/mailing-lists/" class="xref page">join the nbj2ee mailing list</a>.</p>
</div>
</div>
</div>
<section class='tools'>
<ul class="menu align-center">
<li><a title="Facebook" href="https://www.facebook.com/NetBeans"><i class="fa fa-md fa-facebook"></i></a></li>
<li><a title="Twitter" href="https://twitter.com/netbeans"><i class="fa fa-md fa-twitter"></i></a></li>
<li><a title="Github" href="https://github.com/apache/netbeans"><i class="fa fa-md fa-github"></i></a></li>
<li><a title="YouTube" href="https://www.youtube.com/user/netbeansvideos"><i class="fa fa-md fa-youtube"></i></a></li>
<li><a title="Atom Feed" href="https://netbeans.apache.org/blogs/atom"><i class="fa fa-mf fa-rss"></i></a></li>
<li><a title="Slack" href="https://tinyurl.com/netbeans-slack-signup/"><i class="fa fa-md fa-slack"></i></a></li>
<li><a title="Issues" href="https://github.com/apache/netbeans/issues"><i class="fa fa-mf fa-bug"></i></a></li>
</ul>
<ul class="menu align-center">
<li><a href="https://github.com/apache/netbeans-antora-tutorials/edit/main/modules/ROOT/pages/kb/docs/javaee/maven-entapp.adoc" title="See this page in github"><i class="fa fa-md fa-edit"></i> See this page in GitHub.</a></li>
</ul>
</section>
</article>
</div>
<div class='grid-container incubator-area' style='margin-top: 64px'>
<div class='grid-x grid-padding-x'>
<div class='large-auto cell text-center'>
<a href="https://www.apache.org/">
<img style="height: 60px" title="Apache Software Foundation" src="../../../../../../_/images/asf_logo_wide.svg" />
</a>
</div>
<div class='large-auto cell text-center'>
<a href="https://www.apache.org/events/current-event.html">
<img style="width:234px; height: 60px;" title="Apache Software Foundation current event" src="https://www.apache.org/events/current-event-234x60.png"/>
</a>
</div>
</div>
</div>
<footer>
<div class="grid-container">
<div class="grid-x grid-padding-x">
<div class="large-auto cell">
<h1><a href="../../../../../../front/main/about">About</a></h1>
<ul>
<li><a href="../../../../../../front/main/community/who">Who's Who</a></li>
<li><a href="https://www.apache.org/foundation/thanks.html">Thanks</a></li>
<li><a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
<li><a href="https://www.apache.org/security/">Security</a></li>
</ul>
</div>
<div class="large-auto cell">
<h1><a href="../../../../../../front/main/community">Community</a></h1>
<ul>
<li><a href="../../../../../../front/main/community/mailing-lists">Mailing lists</a></li>
<li><a href="../../../../../../front/main/community/committer">Becoming a committer</a></li>
<li><a href="../../../../../../front/main/community/events">NetBeans Events</a></li>
<li><a href="https://www.apache.org/events/current-event.html">Apache Events</a></li>
</ul>
</div>
<div class="large-auto cell">
<h1><a href="../../../../../../front/main/participate">Participate</a></h1>
<ul>
<li><a href="../../../../../../front/main/participate/submit-pr">Submitting Pull Requests</a></li>
<li><a href="../../../../../../front/main/participate/report-issue">Reporting Issues</a></li>
<li><a href="../../../../../../front/main/participate/#documentation">Improving the documentation</a></li>
</ul>
</div>
<div class="large-auto cell">
<h1><a href="../../../../../../front/main/help">Get Help</a></h1>
<ul>
<li><a href="../../../../../../front/main/help/#documentation">Documentation</a></li>
<li><a href="../../../../../../wiki/main/wiki">Wiki</a></li>
<li><a href="../../../../../../front/main/help/#support">Community Support</a></li>
<li><a href="../../../../../../front/main/help/commercial-support">Commercial Support</a></li>
</ul>
</div>
<div class="large-auto cell">
<h1><a href="../../../../../../front/main/download">Download</a></h1>
<ul>
<li><a href="../../../../../../front/main/download">Releases</a></li>
<li><a href="https://plugins.netbeans.apache.org/">Plugins</a></li>
<li><a href="../../../../../../front/main/download/#_daily_builds_and_building_from_source">Building from source</a></li>
<li><a href="../../../../../../front/main/download/#_older_releases">Previous releases</a></li>
</ul>
</div>
</div>
</div>
</footer>
<div class='footer-disclaimer'>
<div class="footer-disclaimer-content">
<p>Copyright &copy; 2017-2025 <a href="https://www.apache.org">The Apache Software Foundation</a>.</p>
<p>Licensed under the Apache <a href="https://www.apache.org/licenses/">license</a>, version 2.0</p>
<div style='max-width: 40em; margin: 0 auto'>
<p>Apache, Apache NetBeans, NetBeans, the Apache feather logo and the Apache NetBeans logo are trademarks of <a href="https://www.apache.org">The Apache Software Foundation</a>.</p>
<p>Oracle and Java are registered trademarks of Oracle and/or its affiliates.</p>
<p>The Apache NetBeans website conforms to the <a href="https://privacy.apache.org/policies/privacy-policy-public.html">Apache Software Foundation Privacy Policy</a></p>
</div>
</div>
</div>
<script src="../../../../../../_/js/vendor/lunr.js"></script>
<script src="../../../../../../_/js/search-ui.js" id="search-ui-script" data-site-root-path="../../../../../.." data-snippet-length="100" data-stylesheet="../../../../../../_/css/search.css"></script>
<script async src="../../../../../../search-index.js"></script>
<script src="../../../../../../_/js/vendor/jquery.min.js"></script>
<script src="../../../../../../_/js/vendor/what-input.min.js"></script>
<script src="../../../../../../_/js/vendor/foundation.min.js"></script>
<script src="../../../../../../_/js/vendor/jquery.colorbox-min.js"></script>
<script src="../../../../../../_/js/netbeans.js"></script>
<script>
$(function(){ $(document).foundation(); });
</script>
<script src="../../../../../../_/js/vendor/highlight.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
document.querySelectorAll('pre code').forEach((el) => {
hljs.highlightElement(el);
});
});
</script>
</body>
</html>