blob: d722c52a140849e69ac80f97739cd126e194d8c6 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>NetBeans Platform Quick Start Using Maven</title>
<link rel="stylesheet" type="text/css" href="https://netbeans.org/netbeans.css"/>
<meta name="AUDIENCE" content="NBUSER"/>
<meta name="TYPE" content="ARTICLE"/>
<meta name="EXPIRES" content="N"/>
<meta name="indexed" content="y"/>
<meta name="description"
content="A short demonstration of how to create a simple
NetBeans Platform application and module and use Maven for the build framework."/>
<!-- Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. -->
<!-- Use is subject to license terms.-->
<style type="text/css">
.examplecode {overflow:auto;width:680px; }
</style>
</head>
<body>
<h1>NetBeans Platform Quick Start Using Maven</h1>
<p>Welcome to the <a href="https://platform.netbeans.org/"><b>NetBeans Platform</b></a>!</p>
<p>This document is a quick demonstration of how to create a simple
NetBeans Platform application on top of the Maven build framework.
After using Maven archetypes to create the NetBeans Platform application build structure,
you will use the Swing UI toolkit and "Matisse" GUI Builder to create a window
component. The modular aspect of the NetBeans Platform enables you to
expand the functionality
of your application by adding new features
during development or at runtime.
</p>
<p>This document is based on the Ant-based <a href="nbm-quick-start.html">NetBeans Platform Quick Start for NetBeans Platform</a>.
and illustrates some of the differences between using Ant and Maven to develop NetBeans Platform applications.
After you understand how Maven is different from Ant, you can easily proceed through other
tutorials on the <a href="https://netbeans.org/kb/trails/platform.html">NetBeans Platform Learning Trail</a>.</p>
<p><strong class="notes">Note: </strong>This document uses NetBeans Platform 7.1 and
NetBeans IDE 7.1. If you
are using an earlier version, see <a href="../70/nbm-maven-quickstart.html">the previous version
of this document</a>.</p>
<p class="tips">If you are new to the NetBeans Platform, you might want to watch the screencast series
<a href="https://platform.netbeans.org/tutorials/nbm-10-top-apis.html">Top 10 NetBeans APIs</a>.</p>
<p><b>Contents</b></p>
<p><img src="../../images/articles/71/netbeans-stamp.png" class="stamp" width="114" height="114" alt="Content on this page applies to NetBeans IDE 7.1" title="Content on this page applies to NetBeans IDE 7.1"/></p>
<ul class="toc">
<!-- <li><a href="#config">Configuring Maven</a></li>-->
<li><a href="#01">Creating a Mavenized NetBeans Platform Application Project</a></li>
<li><a href="#02">Creating Your First Mavenized NetBeans Module</a></li>
<li><a href="#04">Creating a Module that Defines a Service</a></li>
<li><a href="#05">Creating a Module that Implements a Service</a></li>
<li><a href="#06">Publishing Content into the Selection</a></li>
<li><a href="#07">Creating a Module that Listens to the Selection</a></li>
<li><a href="#08">Running the Application</a></li>
</ul>
<p><b>To follow this tutorial, you need the software and resources listed in the following table.</b></p>
<table>
<tbody>
<tr>
<th class="tblheader" scope="col">Software or Resource</th>
<th class="tblheader" scope="col">Version Required</th>
</tr>
<tr>
<td class="tbltd1"><a href="http://download.netbeans.org/">NetBeans IDE</a></td>
<td class="tbltd1">version 7.1</td>
</tr>
<tr>
<td class="tbltd1"><a href="http://java.sun.com/javase/downloads/index.jsp">Java Developer Kit (JDK)</a></td>
<td class="tbltd1">version 6 or above</td>
</tr>
</tbody>
</table>
<p><strong class="notes">Note:</strong> You do not need to download Maven because it
is bundled with the IDE. Optionally, use your own download of Maven, in which
case use the Options window to configure your Maven settings.</p>
<p>Before starting this tutorial you may want to familiarize yourself with
the following documentation.</p>
<ul>
<li><a href="http://wiki.netbeans.org/MavenBestPractices">Best Practices for Apache Maven in NetBeans 6.x</a></li>
<li><a href="http://www.sonatype.com/books/maven-book/reference/introduction.html">Chapter 1. Introducing Apache Maven</a>
(from <a href="http://www.sonatype.com/books/maven-book/reference/public-book.html">Maven: The Definitive Guide</a>)</li>
<li><a href="https://netbeans.org/kb/docs/java/gui-functionality.html">Introduction to GUI Building</a></li>
</ul>
<!-- =================================================================== -->
<!-- +++++++++++++++ Configuring Maven +++++++++++++++++++++++++++++++++
<h2><a name="config"></a>Configuring Maven</h2>
<p>If this is your first Maven project you will want to check the Maven configuration settings in the Options window.
To complete this tutorial you must have Maven installed on your local system.
You can download the installer from the <a href="http://maven.apache.org/">Maven site</a>.</p>
<ol>
<li>Select the Miscellaneous category in the Options window and click the Maven tab.</li>
<li>Specify the location of your local Maven installation (requires 2.0.9 or newer).</li>
<li>Check that the location of the local Maven repository is correct.</li>
<li>Click OK.</li>
</ol>
<p>In most cases, if your Maven configuration is typical the information in the Options window should already be correct.</p>
<p class="notes"><strong>Note.</strong> Maven support is activated as part of the Java SE feature set.
If the Maven tab is not available in the Options window, confirm that Java SE is activated by creating a Java application.</p>
<p>The IDE uses Maven SCM to checkout Maven artifacts.
You might want to check that any clients necessary for checking out sources are installed on your local machine and configured correctly.</p>
<p class="tips">For details on Maven SCM, see the
<a href="http://maven.apache.org/scm/index.html">Maven SCM page</a>.</p>
<div class="indent">
<h3><a name="config1"></a>Viewing the Maven Repositories</h3>
<p>The artifacts that are used by Maven to build all your projects are stored in your local Maven repository.
When an artifact is declared as a project dependency, the artifact is downloaded to your local repository
from one of the registered remote repositories.</p>
<p>The NetBeans repository and several well-known indexed Maven repositories are registered and listed in the Repository Browser window by default.
The NetBeans repository contains most of the public artifacts necessary for you to build your project.
You can use the Maven Repository Browser to view the contents of your local and remote repositories.
You can expand the Local Repository node to see the artifacts that are present locally.
The artifacts listed under the NetBeans repository nodes can be added as project dependencies, but not all of
them are present locally. They are only added to the Local Repository when they are declared as project
dependencies.</p>
<p>To open the Maven Repository Browser:</p>
<ul>
<li>Choose Window &gt; Other &gt; Maven Repository Browser from the main menu.<br/>
<img src="../../images/tutorials/maven-quickstart68/maven-nbm-netbeans-repo.png" alt="Screenshot of Maven Repository Browser" title="Screenshot of Maven Repository Browser" class="margin-around b-all" />
</li>
</ul>
<p>When your cursor is over an artifact, the IDE displays a tooltip with the artifact's coordinates.
You can view additional details about an artifact by double-clicking the artifact's JAR file in the browser.</p>
<p class="tips">You can search for an artifact by clicking the Find button in the toolbar of the Maven Repository Browser
or by using the Quicksearch textfield in the main toolbar.</p>
<p class="tips">For more about managing Maven classpath dependencies and working with Maven repositories in the IDE,
see the <a href="http://wiki.netbeans.org/MavenBestPractices#Dependency_management">Dependency Management</a>
section of <a href="http://wiki.netbeans.org/MavenBestPractices">Best Practices for Apache Maven in NetBeans 6.x</a>.
</p>
<p class="tips">To see a demonstration of using the Artifact Viewer, see the <a href="https://netbeans.org/kb/docs/java/maven-dependencies-screencast.html"> Working with Maven Dependencies</a> screencast.</p>
</div>-->
<!-- =================================================================== -->
<!-- +++++++++++++++ Creating the Platform Application +++++++++++++++++ -->
<h2><a name="01"></a>Creating a Mavenized NetBeans Platform Application Project</h2>
<p>In this section you use the New Project wizard to create a
NetBeans Platform Application from a Maven archetype.
The wizard will create the Maven module projects that you need to develop an application on the NetBeans Platform.
The wizard also provides the option of creating a NetBeans module in the application project, but in this tutorial
you will create each module individually.</p>
<div class="indent">
<h3>Creating the Project</h3>
<p>Perform the following steps to create the NetBeans Platform application using the New Project wizard.</p>
<ol>
<li>Choose File &gt; New Project (Ctrl-Shift-N) to open the New Project wizard.</li>
<li><p>Select NetBeans Application from the Maven category: </p>
<p><img src="../../images/tutorials/maven-quickstart/71/new-project-1.png" alt="Screenshot of New Project wizard" title="Screenshot of New Project wizard" class="margin-around b-all" /></p>
<p>Click Next.</p></li>
<li><p>Type <strong>MavenPlatformWordApp</strong> for the Project Name and set the Project Location.
</p>
<p><img src="../../images/tutorials/maven-quickstart/71/new-project-2.png" alt="Screenshot of New Project wizard" title="Screenshot of New Project wizard" class="margin-around b-all" /></p>
<p>Click Next.</p>
</li>
<li><p>Select the version of the NetBeans APIs you want to use:
</p>
<p><img src="../../images/tutorials/maven-quickstart/71/new-project-3.png" alt="Screenshot of New Project wizard" title="Screenshot of New Project wizard" class="margin-around b-all" /></p>
<p>As you can see, you can create a module together with your application,
which we won't do in this tutorial. In this tutorial, we will create
all the modules separately.</p>
<p>Click Finish.</p>
</li>
</ol>
<p class="notes"><strong>Note.</strong> If this is your first NetBeans Platform application using Maven,
it can take some time to create the projects because the IDE needs to download any necessary artifacts
from the NetBeans Maven repository.</p>
<p>When you click Finish, by default the IDE creates the following Maven project types:</p>
<p><img src="../../images/tutorials/maven-quickstart/71/new-project-4.png" alt="Screenshot of New Project wizard" title="Screenshot of New Project wizard" /></p>
<ul>
<li><strong>app.</strong>
The app project enumerates included modules; permits interactive runs;
produces various kinds of packaging, such as ZIP by default, but optionally
JNLP, NBMs, and in the future perhaps OSGi; holds functional tests.
Each module project specifies its own compile dependencies, while
the app project's dependencies are anything additional that should be present at
runtime. For example, that includes, by default, the whole platform cluster, that is,
the set of modules constituting the "platform" cluster. The app project could
include other clusters, or subsets of clusters, or whatever plugins you want
included in your app which are not used as compilation dependencies. At a minimum,
core.startup and its transitive dependencies are included. </li>
<li><strong>branding.</strong>
This project contains the global resources used for branding the application, such as the splash screen. </li>
<li><strong>parent.</strong>
This project is a Maven reactor project for the NetBeans Platform application, which lists the modules to include and the location of the project's repositories. This project does not contain any sources.
The IDE generates the modules containing the sources and resources in sub-directories of this project.</li>
</ul>
<p>For all Maven projects, the <tt>pom.xml</tt> file (POM) is located under the Project Files node in the Projects window.
If you look at the POM for the parent project,
you can see that the two other modules created by the wizard are listed as modules in the application.</p>
<pre class="examplecode">&lt;modules&gt;
&lt;module&gt;branding&lt;/module&gt;
&lt;module&gt;application&lt;/module&gt;
&lt;/modules&gt;</pre>
<h3><a name="01a"></a>Modifying the Branding Elements</h3>
<p>The branding module provides the customization information, such
as splash screens and icons, used throughout the NetBeans Platform application.
The IDE's Branding Editor enables you to easily modify
the branding properties of the application, giving you a centralized location
for changing the name,
splash screen, and values of various text elements.</p>
<p>When you create the NetBeans Platform application,
the default name for the application is the artifactId of the application.
In this exercise, you will use the Branding Editor to modify the name of the application and
replace the default image for the splash screen.</p>
<ol>
<li>Right-click the <strong>branding</strong> module and choose Branding, which
is a menu item near the end of the right-click popup menu. Wait a moment
while the required JARs are downloaded, before continuing.</li>
<li>In the Basic tab, modify the Application Title to <strong>My Maven Platform Word App</strong>.<br/>
<img src="../../images/tutorials/maven-quickstart/71/maven-branding1.png" alt="Screenshot of New Project wizard" title="Screenshot of New Project wizard" class="margin-around b-all" />
</li>
<li>Click the Splash Screen tab and click the Browse
button next to the default splash screen image to locate a different image. Click OK.</li>
<!--<li>Click the Resource Bundles tab and expand the <strong>org/netbeans/core/startup/Bundle.properties</strong> node.</li>
<li>Double-click <strong>CTL_About_Title</strong> and type a new title for the About dialog window. Click OK.<br/>
<img src="../../images/tutorials/maven-quickstart/maven-branding2.png" alt="Screenshot of New Project wizard" title="Screenshot of New Project wizard" class="margin-around b-all" />
</li>-->
</ol>
<p class="tips">You can copy the image below to your local system and specify the image as the splash screen in the Branding dialog.</p>
<img src="../../images/tutorials/maven-quickstart68/splash.gif" alt="Example of default splash image" title="Example of default splash image" class="margin-around b-all" />
</div>
<!-- =================================================================== -->
<!-- +++++++++++++++ Creating the MavenWordEngine Module +++++++++++++++ -->
<h2><a name="02"></a>Creating Your First Mavenized NetBeans Module</h2>
<p>In this section you will create a new module named MavenWordEngine.
You will then modify the module to add a window component and a button and text area.</p>
<div class="indent">
<h3><a name="02a"></a>Creating the Module</h3>
<p>In this exercise you will create a new module project in the same directory that contains the branding module and application module.</p>
<ol>
<li>Choose File &gt; New Project from the main menu.</li>
<li><p>Select NetBeans Module from the Maven category:</p>
<p><img src="../../images/tutorials/maven-quickstart/71/new-module-1.png" alt="Screenshot of New Project wizard" title="Screenshot of New Project wizard" class="margin-around b-all" />
</p>
<p>Click Next.</p></li>
<li><p>Type <strong>MavenWordEngine</strong> as the Project Name:</p>
<p><img src="../../images/tutorials/maven-quickstart/71/new-module-3.png" alt="Screenshot of New Project wizard" title="Screenshot of New Project wizard" class="margin-around b-all" />
</p>
</li>
<li><p>Click Browse and locate the MavenPlatformWordApp directory
as the Project Location.</p>
<p> <img src="../../images/tutorials/maven-quickstart/71/new-module-2.png" alt="Screenshot of New Project wizard" title="Screenshot of New Project wizard" class="margin-around b-all" />
</p>
</li>
<li><p>Click Next and select the NetBeans APIs you want to use:</p>
<p><img src="../../images/tutorials/maven-quickstart/71/new-module-4.png" alt="Screenshot of New Project wizard" title="Screenshot of New Project wizard" class="margin-around b-all" />
</p>
<p>Click Finish.</p>
</li>
</ol>
<p>If you look at the POM for the new MavenWordEngine module you see that the <tt>artifactId</tt> of the project is <strong>MavenWordEngine</strong>.</p>
<pre class="examplecode">
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;com.mycompany&lt;/groupId&gt;
&lt;artifactId&gt;MavenPlatformWordApp&lt;/artifactId&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.mycompany&lt;/groupId&gt;
&lt;artifactId&gt;<strong>MavenWordEngine</strong>&lt;/artifactId&gt;
&lt;packaging&gt;nbm&lt;/packaging&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
&lt;name&gt;MavenWordEngine NetBeans Module&lt;/name&gt;
</pre>
<p>To build a NetBeans module you need to use the <tt>nbm-maven-plugin</tt>.
If you look at the POM for the module, you can see that the IDE automatically
specified <tt>nbm</tt> for the <tt>packaging</tt> and that the <strong>nbm-maven-plugin</strong>
is specified as a build plugin.</p>
<pre class="examplecode">
&lt;plugin&gt;
&lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
&lt;artifactId&gt;<strong>nbm-maven-plugin</strong>&lt;/artifactId&gt;
&lt;version&gt;3.6&lt;/version&gt;
&lt;extensions&gt;true&lt;/extensions&gt;
&lt;/plugin&gt;
</pre>
<p>If you look at the POM for the NetBeans Platform Application,
you can see that <strong>MavenWordEngine</strong> was added to the list of the modules in the application.</p>
<pre class="examplecode">
&lt;modules&gt;
&lt;module&gt;branding&lt;/module&gt;
&lt;module&gt;application&lt;/module&gt;
&lt;module&gt;<strong>MavenWordEngine</strong>&lt;/module&gt;
&lt;/modules&gt;
</pre>
<h3><a name="02b"></a>Making the Module a Dependency of MavenPlatformWordApp</h3>
<p>In this exercise you will declare the MavenWordEngine module as a dependency of the
<b>app</b>
by adding the dependency in the POM.
Currently, the POM of the <b>app</b> declares the following dependencies:</p>
<pre class="examplecode">&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.netbeans.cluster&lt;/groupId&gt;
&lt;artifactId&gt;platform&lt;/artifactId&gt;
&lt;version&gt;${netbeans.version}&lt;/version&gt;
&lt;type&gt;pom&lt;/type&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.mycompany&lt;/groupId&gt;
&lt;artifactId&gt;branding&lt;/artifactId&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;</pre>
<p>If you expand the Libraries node for the <b>app</b> you can see that there is a dependency on
the branding module and on some other libraries that are dependencies of the cluster required to build the application.</p>
<p class="tips">You can expand the list of non-classpath dependencies to see the full list of dependencies.</p>
<p>To add the dependency to the POM, you can edit the POM directly in the editor or by opening the Add Dependency dialog box from the Projects window.</p>
<ol>
<li>Expand the <strong>app</strong> in the Projects window,
right-click the Dependencies node, and choose Add Dependency.</li>
<li>Click the Open Projects tab and select <strong>MavenWordEngine</strong>. Click OK.</li>
</ol>
<img src="../../images/tutorials/maven-quickstart/71/maven-add-dependency1.png" alt="Screenshot of Add Dependency dialog" title="Screenshot of Add Dependency dialog" class="margin-around b-all" />
<p class="notes"><strong>Note.</strong> The new project will appear in the dialog after the IDE finishes scanning and updating indexes.</p>
<p>If you expand the <b>app</b> and the <b>parent</b> in the Projects window,
you can see that MavenWordEngine is now listed as a dependency.</p>
<p><img src="../../images/tutorials/maven-quickstart/71/new-module-5.png" alt="Screenshot of New Project wizard" title="Screenshot of New Project wizard" /></p>
<h3><a name="02c"></a>Adding a Window Component to the Module</h3>
<p>In this exercise you will use a wizard to add a Window Component to the MavenWordEngine module.</p>
<ol>
<li>Right-click <strong>MavenWordEngine</strong> in the Projects window and choose New &gt; Window. Click Next.</li>
<li>Select <strong>output</strong> in the Window Position dropdown list. Click Next.<br/>
<img src="../../images/tutorials/maven-quickstart/71/maven-new-window.png" alt="Screenshot of window component page in New File wizard" title="Screenshot of window component page in New File wizard" class="margin-around b-all" />
</li>
<li>Type <strong>Text</strong> in the Class Name Prefix field. Click Finish.</li>
</ol>
<p>When you click Finish, in the Projects window you can see that the IDE
generated the class <tt>TextTopComponent.java</tt> in <tt>com.mycompany.mavenwordengine</tt> under Source Packages.</p>
<p class="tips">You can view the structure of the project in the Files window.
To compile a Maven project, only source files can be located under
Source Packages (<tt>src/main/java</tt> directory in the Files window).
Other resources (e.g., properties files) need to be located under Other Sources
(<tt>src/main/resources</tt> directory in the Files window). </p>
<h3><a name="02d"></a>Modifying the Window Component</h3>
<p>In this exercise you will add a text area and a button to the window component.
You will then modify the method invoked by the button to change the letters in the text area to upper case letters.</p>
<ol>
<li>Click the Design tab of <tt>TextTopComponent.java</tt> in the editor.</li>
<li>Drag and drop a button and a text area from the Palette (Ctrl-Shift-8) onto the window.</li>
<li>Right-click the text area and choose Change Variable Name, and then type <strong>text</strong> as the name.
You will use the name when accessing the component from your code.</li>
<li><p>Set the text of the button to "<strong>Filter!</strong>".</p>
<p><img src="../../images/tutorials/maven-quickstart/71/maven-nbm-textopcomponent.png" alt="Screenshot of window component page in New File wizard" title="Screenshot of window component page in New File wizard" class="margin-around b-all" />
</p>
</li>
<li>Double-click the Filter! button in the Design view to open the event handler method for the button in the source code editor.
The method is created automatically when you double-click the button element
and the Source view opens.
Modify the body of the method to add the following code.
<pre class="examplecode">
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
<strong>String s = text.getText();
s = s.toUpperCase();
text.setText(s);</strong>
}</pre>
<p>Save your changes.</p>
</li>
</ol>
<h3><a name="02e"></a>Trying Out the Application</h3>
<p>If you want to test that your application is working correctly, you can right-click the project node of the
<strong>application</strong> and choose Build with Dependencies.</p>
<p>The default action mapped to Build with Dependencies is to build the project using the Reactor plugin.
When you build a project using the Reactor plugin, the dependencies of the sub-projects are built before the containing project is built.
The Output window displays the build order.</p>
<img src="../../images/tutorials/maven-quickstart/maven-buildwithdependencies1.png" alt="Screenshot of Reactor build order in Output window" title="Screenshot of Reactor build order in Output window" class="margin-around b-all" />
<p>The results of the build are also displayed in the Output window.</p>
<img src="../../images/tutorials/maven-quickstart/maven-buildwithdependencies2.png" alt="Screenshot of successful Reactor build in Output window" title="Screenshot of successful Reactor build in Output window" class="margin-around b-all" />
<p>If you look at the Projects window, you will see that the projects no longer have badges because
the artifacts of the required dependencies are now available in the Local repository under the <tt>com.mycompany</tt> node.</p>
<img src="../../images/tutorials/maven-quickstart/maven-localrepo.png" alt="Screenshot of Local repository" title="Screenshot of Local repository" class="margin-around b-all" />
<p>To run the project, in the Projects window, right-click the project node of
the <strong>application</strong>, and choose Run.
After the application launches, you can test the application by performing the following steps.</p>
<ol>
<li>Choose Window &gt; Text from the main menu of the platform application to open the Text window.</li>
<li>Type some lower case letters in the text area and click Filter! When
you click Filter!, the letters that you typed are changed to upper case and displayed in the text area.</li>
</ol>
</div>
<p>In the next sections, you will decouple the user interface from the
business logic in your application. You will start by creating a module
that provides an API. Then you will create a module that implements
the API. Finally, you will change the window defined above so that
implementations of the API are loaded at runtime into the application.
In that way, the GUI will be able to load multiple filters without
needing to care about any of the implementation details.
</p>
<!-- =================================================================== -->
<!-- +++++++++++++ Creating the TextFilter Module ++++++++++++++++++++++ -->
<h2><a name="04"></a>Creating a Module that Defines a Service</h2>
<p>In this exercise, you create a module named <b>TextFilter</b>
and add the module as a dependency to the application.
The TextFilter module will provide an API and only contain an interface.
You can then access the service from your other modules by using the
NetBeans Lookup mechanism.</p>
<div class="indent">
<h3><a name="04a"></a>Creating the Module</h3>
<p>In this exercise you will perform the following steps to create the TextFilter module.</p>
<ol>
<li> Choose File &gt; New Project (Ctrl-Shift-N).</li>
<li><p>As explained in the previous section,
select NetBeans Module from the Maven category:</p>
<p><img src="../../images/tutorials/maven-quickstart/71/new-module-1.png" alt="Screenshot of New Project wizard" title="Screenshot of New Project wizard" class="margin-around b-all" />
</p>
<p>Click Next.</p></li>
<li><p>Type <b>TextFilter</b> for the Project Name.
Click Browse to set the Project Location and locate the application directory:</p>
<p><img src="../../images/tutorials/maven-quickstart/71/api-module-1.png" alt="Screenshot of New Project wizard" title="Screenshot of New Project wizard" class="margin-around b-all" />
</p>
<p>Click Next, choose the version of the NetBeans API you will use, and click Finish.</p></li>
</ol>
<p>When you click Finish, the IDE creates the module and opens the module project
<b>TextFilter</b> in the Projects window.</p>
<p>The IDE modifies <tt>pom.xml</tt> of the <b>parent</b> project
to add the new module to the list of modules to include in the project.</p>
<pre class="examplecode">&lt;modules&gt;
&lt;module&gt;branding&lt;/module&gt;
&lt;module&gt;application&lt;/module&gt;
&lt;module&gt;MavenWordEngine&lt;/module&gt;
&lt;module&gt;TextFilter&lt;/module&gt;
&lt;/modules&gt;</pre>
<p>After you create the module you need to add the module as a dependency of
the <b>application</b>, as explained in the next exercise.</p>
<h3><a name="04b"></a>Adding the TextFilter Module as a Dependency of MavenPlatformWordApp</h3>
<p>In this exercise you add the TextFilter module as a dependency of the application.</p>
<ol>
<li>Right-click the Dependencies node of the <strong>application</strong> project and choose Add Dependency.</li>
<li>Click the Open Projects tab in the Add Dependency dialog.</li>
<li>Select the <strong>TextFilter</strong> module. Click OK.</li>
</ol>
<p>When you click OK, the IDE adds the module as a dependency of the project.
If you expand the Libraries node, you can see that the module is added to the list of dependencies.
In the POM for <strong>application</strong>,
you can see that the IDE added the following lines inside the <tt>dependencies</tt> element:</p>
<pre class="examplecode">&lt;dependency&gt;
&lt;groupId&gt;${project.groupId}&lt;/groupId&gt;
&lt;artifactId&gt;TextFilter&lt;/artifactId&gt;
&lt;version&gt;${project.version}&lt;/version&gt;
&lt;/dependency&gt;</pre>
<h3><a name="04c"></a>Adding an Interface to the TextFilter Module</h3>
<p>In this exercise you will add a simple interface to the TextFilter module.</p>
<ol>
<li> Right-click the <strong>TextFilter</strong> module and choose New &gt; Java Interface.</li>
<li> Type <strong>TextFilter</strong> as the Class Name.</li>
<li> Select <b>com.mycompany.textfilter</b> in the Package dropdown list. Click Finish.</li>
<li> Modify the class to add the following code. Save your changes.</li>
</ol>
<pre class="examplecode">package com.mycompany.textfilter;
public interface TextFilter {
<strong>private String process(String s);</strong>
}</pre>
<h3><a name="04d"></a>Making the TextFilter Module Public</h3>
<p>In this exercise you will make the contents of the <tt>com.mycompany.textfilter</tt> package
public so that other modules can access its public classes and interfaces.
To declare a package as public, you modify
the <tt>configuration</tt> element of <tt>nbm-maven-plugin</tt> in the POM to specify the packages that are exported as public by the plugin.
You can make the changes to the POM in the editor or by selecting the packages to be
made public in the project's Properties dialog box, as explained in the steps below.
</p>
<ol>
<li> Right-click the <strong>TextFilter</strong> module and choose Properties.</li>
<li> Select the Public Packages category in the Project Properties dialog.</li>
<li><p>Select the <strong>com.mycompany.textfilter</strong> package.</p>
<p><img src="../../images/tutorials/maven-quickstart/71/maven-public-packages.png" alt="Screenshot of Properties dialog" title="Screenshot of Properties dialog" class="margin-around b-all" />
</p>
<p>Click OK.</p></li>
</ol>
<p>When you click OK, the IDE modifies the project POM to modify the <tt>configuration</tt> element
of the <tt>nbm-maven-plugin</tt> artifact to add the following entries.</p>
<pre>&lt;publicPackages&gt;
&lt;publicPackage&gt;com.mycompany.textfilter&lt;/publicPackage&gt;
&lt;/publicPackages&gt;</pre>
<p>The POM entry now contains the following entries.</p>
<pre>&lt;plugin&gt;
&lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
&lt;artifactId&gt;nbm-maven-plugin&lt;/artifactId&gt;
&lt;version&gt;3.6&lt;/version&gt;
&lt;extensions&gt;true&lt;/extensions&gt;
&lt;configuration&gt;
<b>&lt;publicPackages&gt;
&lt;publicPackage&gt;com.mycompany.textfilter&lt;/publicPackage&gt;
&lt;/publicPackages&gt;</b>
&lt;/configuration&gt;
&lt;/plugin&gt;</pre>
<p class="tips">For more information, see <a href="http://mojo.codehaus.org/nbm-maven-plugin/manifest-mojo.html#publicPackages" class="external text" title="http://mojo.codehaus.org/nbm-maven-plugin/manifest-mojo.html#publicPackages" rel="nofollow">nbm-maven-plugin manifest</a>.</p>
</div>
<!-- =================================================================== -->
<!-- +++++++++++++ Creating the MyFilter Module ++++++++++++++++++++++++ -->
<h2><a name="05"></a>Creating a Module that Implements the Service</h2>
<p>In this exercise you will create the module <b>MyFilter</b> and set a dependency on
the <b>TextFilter</b> module.
In the <b>MavenWordEngine</b> you
can then use methods from <b>MyFilter</b> by looking up the <b>TextFilter</b> service.
At that point, <b>MyFilter</b> will be decoupled from <b>MavenWordEngine</b>.</p>
<div class="indent">
<h3><a name="05a"></a>Creating the Module</h3>
<p>In this exercise you will create a module named <strong>MyFilter</strong>.
To create the module, you will perform the same steps that you performed when you created the TextFilter module.</p>
<ol>
<li> Choose File &gt; New Project (Ctrl-Shift-N).</li>
<li> As explained in the previous section, select NetBeans Module from the Maven category. Click Next.</li>
<li> Type <b>MyFilter</b> for the Project Name.</li>
<li> Click Browse to set the Project Location and locate the
application directory. Click Finish.</li>
<li> Add the MyFilter module as a dependency of the <b>application</b> project.</li>
</ol>
<p class="tips">For details on the steps above, see the previous sections.</p>
<h3><a name="05b"></a>Adding the TextFilter Module as a Dependency</h3>
<p>In this exercise you add the TextFilter module as a dependency of the MyFilter module.</p>
<ol>
<li>Right-click the Libraries node of the <b>MyFilter</b> project and choose Add Dependency.</li>
<li>Click the Open Projects tab in the Add Dependency dialog.</li>
<li>Select the <strong>TextFilter</strong> module. Click OK.</li>
</ol>
<h3><a name="05c"></a>Modifying the MyFilter Module</h3>
<p>In this exercise you will add a Java class with a single method that is named <tt>process</tt>
that converts a string to upper case.
You will also specify that the class will implement the TextFilter interface.
You will use an <tt>@ServiceProvider</tt> annotation to specify that TextFilter is a service
that will be registered at compile time.</p>
<ol>
<li> Right-click the <b>MyFilter</b> module and choose New &gt; Java Class.</li>
<li> Type <b>UpperCaseFilter</b> as the Class Name.</li>
<li> Select <b>com.mycompany.myfilter</b> in the Package dropdown list. Click Finish.</li>
<li> Modify the class to add the following code. Save your changes.</li>
</ol>
<pre>package com.mycompany.myfilter;
import com.mycompany.textfilter.TextFilter;
import org.openide.util.lookup.ServiceProvider;
<strong>@ServiceProvider(service=TextFilter.class)</strong>
public class UpperCaseFilter <strong>implements TextFilter {
@Override
public String process(String s) {
return s.toUpperCase();
}</strong>
}</pre>
<p class="tips">Notice how the annotation is used to specify the Service Provider.
For more about the <tt>@ServiceProvider</tt> annotation and the behavior of
the ServiceLoader mechanism in JDK 6, see the Lookup API documentation.</p>
<h3><a name="05d"></a>Modifying the MavenWordEngine Module</h3>
<p>In this exercise you will modify the event handler in the Text window component to use a Lookup
to call the
TextFilter interface and access the process method in any available implementations, which
in this case is only provided by MyFilter.
Before adding the code to the event handler you need to declare a dependency on the TextFilter module.</p>
<ol>
<li>Right-click the Dependencies node of the <strong>MavenWordEngine</strong> module and add a dependency on the TextFilter module.</li>
<li>Expand the Source Packages of the <strong>MavenWordEngine</strong> module and open <tt>TextTopComponent</tt> in the source editor.</li>
<li>Modify the <tt>jButton1ActionPerformed</tt> button handler method to add the following code. Save your changes.
<pre>private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String s = text.getText();
<strong>for (TextFilter filter : Lookup.getDefault().lookupAll(TextFilter.class)) {
s = filter.process(s);
} </strong>
text.setText(s);
}</pre>
</li>
</ol>
<p>At this point you can check your application to see if it still works as before.
In the next sections, you will publish content whenever the Filter button is clicked,
followed by a new NetBeans module that will listen to the selection
and display the content published there.</p>
</div>
<!-- =================================================================== -->
<!-- ++++++++++++++++++ Adding a LookupListener ++++++++++++++++++++++++ -->
<h2><a name="06"></a>Publishing Content into the Selection</h2>
<p>In this exercise you add a listener and a field to store the contents of the text area when
the "Filter!" button is clicked.</p>
<ol>
<li>In the <strong>MavenWordEngine</strong> module, add the <tt>InstanceContent</tt> object and
modify the constructor of <tt>TextTopComponent</tt> by adding the following code.
<pre>public final class TextTopComponent extends TopComponent {
<strong>private InstanceContent content;</strong>
public TextTopComponent() {
initComponents();
setName(Bundle.CTL_TextTopComponent());
setToolTipText(Bundle.HINT_TextTopComponent());
<strong>content = new InstanceContent();
associateLookup(new AbstractLookup(content));</strong>
}
...
...
...
</pre>
</li>
<li>Modify the <tt>jButton1ActionPerformed</tt> method to add the old
value of the text to the <tt>InstanceContent</tt> object when the button is clicked.
<pre>private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String s = text.getText();
for (TextFilter filter : Lookup.getDefault().lookupAll(TextFilter.class)) {
<b>content.add(s);</b>
s = filter.process(s);
}
text.setText(s);
}</pre>
</li>
</ol>
<p>Now a String will be published whenever the button is clicked. The String
is published into the Lookup of the TopComponent. When the TopComponent is
selected, the content of its Lookup will be available throughout the
application. Next, we will create a new module that will listen to the
Lookup and display the Strings found there.</p>
<!-- =================================================================== -->
<!-- ++++++++++++++ Creating the History Module ++++++++++++++++++++++++ -->
<h2><a name="07"></a>Creating a Module that Listens to the Selection</h2>
<p>In this section you will create a module named <strong>History</strong> that will
display Strings
found in the Lookup of the selected TopComponent.
To create the module, you will perform the same steps that you performed when
you created the TextFilter and MyFilter modules.</p>
<div class="indent">
<h3><a name="07p"></a>Creating a New Module</h3>
<ol>
<li>Choose File &gt; New Project (Ctrl-Shift-N).</li>
<li>Select NetBeans Module from the Maven category. Click Next.</li>
<li>Type <strong>History</strong> for the Project Name.</li>
<li>Click Browse to set the Project Location and locate the application directory. Click Finish.</li>
<li>Add the History module as a dependency of the <b>application</b> project.</li>
</ol>
<h3><a name="07a"></a>Creating a Window Component in the History Module</h3>
<p>In this exercise you will use a wizard to add a window component to the module.</p>
<ol>
<li>Right-click the <b>History</b> module in the Projects window and
choose New &gt; Window.</li>
<li>Select <b>editor</b> in the Window Position dropdown list. Click Next.</li>
<li>Type <b>History</b> in the Class Name Prefix field. Click Finish.</li>
</ol>
<p>A new TopComponent is created in your module, named HistoryTopComponent.</p>
<h3><a name="07b"></a>Modifying the History Window Component</h3>
<p>You will now add a text area element to the window component that will display the filtered strings.</p>
<ol>
<li>Click the Design tab of <tt>HistoryTopComponent.java</tt> in the editor.</li>
<li>Drag and drop a text area from the Palette onto the window.</li>
<li>Right-click the text area and choose Change Variable Name, and then type <b>historyText</b> as the name.</li>
<li>Implement the <tt>LookupListener</tt> class and
add the following code to the <tt>HistoryTopComponent</tt>
so that it listens to the Lookup for the String class whenever
the <tt>HistoryTopComponent</tt> opens
and displays all retrieved String objects in the text area.
<pre class="examplecode">...
...
...
public final class HistoryTopComponent extends TopComponent <b>implements LookupListener</b> {
<b>Result&lt;String&gt; stringsInSelectedWindow;</b>
public HistoryTopComponent() {
initComponents();
setName(Bundle.CTL_HistoryTopComponent());
setToolTipText(Bundle.HINT_HistoryTopComponent());
}
<b>@Override
public void resultChanged(LookupEvent le) {
StringBuilder sb = new StringBuilder();
for (String string : stringsInSelectedWindow.allInstances()) {
sb.append(string).append('\n');
}
historyText.setText(sb.toString());
}
@Override
public void componentOpened() {
stringsInSelectedWindow = Utilities.actionsGlobalContext().lookupResult(String.class);
stringsInSelectedWindow.addLookupListener(this);
}
@Override
public void componentClosed() {
stringsInSelectedWindow.removeLookupListener(this);
}</b>
...
...
...
</pre>
</li>
</ol>
</div>
<h2><a name="08"></a>Running the Application</h2>
<p>You can now test the application.</p>
<ol>
<li>Right-click the project node of the <strong>application</strong> and choose Clean.</li>
<li>Right-click the project node of the <strong>application</strong> and choose Build with Dependencies.</li>
<li>Right-click the project node of the <strong>application</strong> and choose Run.</li>
</ol>
<p>When you click Run, the IDE launches the NetBeans Platform application.
You can open the History and Text windows from the Window menu.<br/>
<img src="../../images/tutorials/maven-quickstart/71/result.png" alt="Screenshot of final NetBeans Platform application" title="Screenshot of final NetBeans Platform application" class="margin-around b-all" />
</p>
<p>When you type text in the Text window and click the Filter! button, the text is converted to upper case letters
and the text is added to the content of the History window.</p>
<p>This tutorial demonstrated the fundamental pieces of NetBeans Platform
applications. Creating a NetBeans Platform application using Maven
is not very different from creating an application using Ant.
The major difference is understanding how the Maven POM controls how the application is assembled.
For more examples on how to build NetBeans Platform applications and modules,
see the tutorials listed in the <a href="https://netbeans.org/kb/trails/platform.html">NetBeans Platform Learning Trail</a>.</p>
<!-- ======================================================================================== -->
<div class="feedback-box"><a name="feedback"></a>
<a href="https://netbeans.org/about/contact_form.html?to=3&amp;subject=Feedback:%20NetBeans%20Platform%20Quick%20Start%207.1%20Using%20Maven">
Send Us Your Feedback</a></div>
<p>&nbsp;</p>
<!-- ======================================================================================== -->
<h2><a name="nextsteps"></a>See Also</h2>
<p>For more information about creating and developing applications, see the following resources.</p>
<ul>
<li><a href="https://netbeans.org/kb/trails/platform.html">NetBeans Platform Learning Trail</a></li>
<li><a href="http://bits.netbeans.org/dev/javadoc/">NetBeans API Javadoc</a></li>
</ul>
<p>If you have any questions about the NetBeans Platform, feel free
to write to the mailing list, dev@platform.netbeans.org, or view the
<a href="https://netbeans.org/projects/platform/lists/dev/archive">NetBeans Platform mailing list archive</a>.</p>
<!-- ======================================================================================== -->
</body>
</html>