blob: 97d90fc59e4dc58cc593ec16bf0064209b36bc7e [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 and module and use Maven for the build framework.
In this document you will use Maven archetypes to create the NetBeans Platform application and module.
You will use the Swing UI toolkit and "Matisse" GUI Builder to create a window component in the module.
The modular aspect of the NetBeans Platform enables you to develop and expand the functionality
of your application simply by adding new modules.
</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 class="tips">If you are using NetBeans IDE 6.8, see the
<a href="68/nbm-maven-quickstart.html">Quick Start for NetBeans Platform 6.8 Using Maven</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><strong class="notes">Note: </strong>This is not the latest version of this
document. <a href="../nbm-maven-quickstart.html">Click here</a> to
see the most up to date version.</p>
<p><b>Contents</b></p>
<p><img src="../../images/articles/69/netbeans-stamp69.png" class="stamp" width="114" height="114" alt="Content on this page applies to NetBeans IDE 6.5, 6.7, 6.8" title="Content on this page applies to NetBeans IDE 6.5, 6.7, 6.8"/></p>
<ul class="toc">
<li><a href="#config">Configuring Maven</a></li>
<li><a href="#01">Creating the NetBeans Platform Application Project</a>
<ul>
<li><a href="#01a">Modifying the Branding Elements</a></li>
</ul>
</li>
<li><a href="#02">Creating the MavenWordEngine Module</a>
<ul>
<li><a href="#02b">Making the Module a Dependency</a></li>
<li><a href="#02c">Adding a Window Component</a></li>
</ul>
</li>
<li><a href="#04">Creating the TextFilter Module</a>
<ul>
<li><a href="#04b">Making the Module a Dependency</a></li>
<li><a href="#04c">Adding an Interface</a></li>
<li><a href="#04d">Making the Module Public</a></li>
</ul>
</li>
<li><a href="#05">Creating the MyFilter Module</a>
<ul>
<li><a href="#05b">Adding TextFilter as a Dependency</a></li>
<li><a href="#05c">Adding a Java Class</a></li>
<li><a href="#05d">Calling the Method From the MavenWordEngine Module</a></li>
</ul>
</li>
<li><a href="#06">Adding a Listener and Content Field</a></li>
<li><a href="#07">Creating the History Module</a>
<ul>
<li><a href="#07b">Adding a Window Component</a></li>
</ul>
</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 6.9</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</td>
</tr>
<tr>
<td class="tbltd1"><a href="http://maven.apache.org/">Maven</a></td>
<td class="tbltd1">Version 2.0.9 or higher</td>
</tr>
</tbody>
</table>
<p><strong class="notes">Note:</strong> You do not need to download a separate version
of the NetBeans Platform to develop applications for the NetBeans Platform.
Typically, you develop the applications and modules in the NetBeans IDE and
then only include the modules that are necessary to run the NetBeans Platform and your application.</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 the NetBeans Platform Application Project</h2>
<p>In this section you use the New Project wizard to create a
NetBeans 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>Select Maven NetBeans Application from the Maven category. Click Next.</li>
<li>Type <strong>MavenPlatformWordApp</strong> for the Project Name and set the Project Location. Click Finish. <br/>
<img src="../../images/tutorials/maven-quickstart/maven-newproject.png" alt="Screenshot of New Project wizard" title="Screenshot of New Project wizard" class="margin-around b-all" />
</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 repository.</p>
<p>When you click Finish, by default the IDE creates the following Maven project types.</p>
<ul>
<li><strong>NetBeans Platform Application.</strong>
This project is a container project for the Platform application and 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>
<li><strong>NetBeans Platform based application.</strong>
This project specifies the artifacts (sources) needed for compiling the application.
The required dependencies (IDE artifacts, module artifacts) are specified in the <tt>pom.xml</tt> file of the project.
If you expand the Libraries node you can see the libraries required for the NetBeans Platform application.</li>
<li><strong>Platform application branding resources.</strong>
This project contains the resources used for branding the application. </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 NetBeans Platform Application 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 specifies the branding resources that are used when building the Platform application.
The branding dialog enables you to easily modify the branding properties of the application to change the name,
splash screen and the values of text elements.</p>
<p>When you create the NetBeans Platform application from the archetype,
the default name for the application is the artifactId of the application.
In this exercise you will use the branding wizard to modify the name of the application and
replace the default image for the splash screen.</p>
<p class="notes"><strong>Note.</strong> The IDE needs to build the branding module before you can modify the branding resources.</p>
<ol>
<li>Right-click the <strong>Platform application branding resources</strong> module and choose Branding.</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/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 the MavenWordEngine 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>Select Maven NetBeans Module from the Maven category. Click Next.</li>
<li>Type <strong>MavenWordEngine</strong> as the Project Name.</li>
<li>Click Browse and locate the MavenPlatformWordApp directory as the Project Location. Click Finish.</li>
</ol>
<img src="../../images/tutorials/maven-quickstart/maven-wizard-project-location.png" alt="Screenshot of New Project wizard" title="Screenshot of New Project wizard" class="margin-around b-all" />
<p>If you look at the POM for the 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.2-SNAPSHOT&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 NetBeans Platform based application
by adding the dependency in the POM.
The POM for the application 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 NetBeans Platform based application 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>
<img src="../../images/tutorials/maven-quickstart/maven-projects-libraries.png" alt="Screenshot of Libraries node" title="Screenshot of Add Dependency dialog" class="margin-around b-all" />
<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>MavenPlatformWordApp - NetBeans Platform based application</strong> in the Projects window.</li>
<li>Right-click the Libraries 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/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 Libraries node of MavenPlatformWordApp in the Projects window,
you can see that MavenWordEngine is now listed as a dependency.</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 NetBeans Module</strong> in the Projects window and choose New &gt; Other to open the New File wizard.</li>
<li>Select Window in the Module Development category. Click Next.</li>
<li>Select <strong>output</strong> in the Window Position dropdown list. Click Next.<br/>
<img src="../../images/tutorials/maven-quickstart/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.
<p>The wizard displays a list of the files that will be created and the files that will be modified. </p></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.
The IDE also generated additional resource files in <tt>com.mycompany.mavenwordengine</tt> under Other Sources.
In this exercise you will only edit <tt>TextTopComponent.java</tt>.</p>
<p>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., XML 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 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>Set the text of the button to "<strong>Filter!</strong>".<br/>
<img src="../../images/tutorials/maven-quickstart68/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" />
</li>
<li>Double-click the Filter! button element 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.</li>
<li>Modify the body of the method to add the following code. Save your changes.
<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 class="tips">You can use the code completion in the editor to help you type the code. </p>
</li>
</ol>
<p>If you want to test that your application is working correctly, you can right-click the project node of the
<strong>MavenPlatformWordApp NetBeans Platform based 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, right-click the project node of the <strong>MavenPlatformWordApp NetBeans Platform based 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! </li>
<li>Quit the Maven Platform Word App.</li>
</ol>
<p>When you click Filter!, the letters that you typed are changed to upper case and displayed in the text area. </p>
</div>
<!-- =================================================================== -->
<!-- +++++++++++++ Creating the TextFilter Module ++++++++++++++++++++++ -->
<h2><a name="04"></a>Creating the TextFilter Module</h2>
<p>In this exercise you will create a module named <b>TextFilter</b>
and add the module as a dependency to the application.
The TextFilter module will provide a service and only contain an interface.
You can then access the service from your other modules by using a lookup.</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> Select the Maven NetBeans Module archetype from the Maven category. Click Next.</li>
<li> Type <b>TextFilter</b> for the Project Name.</li>
<li> Click Browse to set the Project Location and locate the MavenPlatformWordApp directory. Click Finish.</li>
</ol>
<p>When you click Finish, the IDE creates the module and opens the module project <b>TextFilter NetBeans Module</b> in the Projects window.</p>
<p>The IDE modifies <tt>pom.xml</tt> of the POM project MavenPlatformWordApp - NetBeans Platform Application
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 application.</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 MavenPlatformWordApp NetBeans Platform based application.</p>
<ol>
<li>Right-click the Libraries node of the <strong>MavenPlatformWordApp - NetBeans Platform based 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 NetBeans Module</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>MavenPlatformWordApp - NetBeans Platform based 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 NetBeans Module</strong> 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>public 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 the methods.
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.
</p>
<ol>
<li> Right-click the <strong>TextFilter NetBeans Module</strong> and choose Properties.</li>
<li> Select the Public Packages category in the Project Properties dialog.</li>
<li> Select the <strong>com.mycompany.textfilter</strong> package. Click OK.</li>
</ol>
<img src="../../images/tutorials/maven-quickstart/maven-public-packages.png" alt="Screenshot of Properties dialog" title="Screenshot of Properties dialog" class="margin-around b-all" />
<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.2&lt;/version&gt;
&lt;extensions&gt;true&lt;/extensions&gt;
&lt;configuration&gt;
&lt;publicPackages&gt;
&lt;publicPackage&gt;com.mycompany.textfilter&lt;/publicPackage&gt;
&lt;/publicPackages&gt;
&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 the MyFilter Module</h2>
<p>In this exercise you will create the module <b>MyFilter</b> and add the module as a dependency of TextFilter.
You can then call methods in MyFilter by looking up the TextFilter service.</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> Select Maven 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 <strong>MavenPlatformWordApp</strong> directory. Click Finish.</li>
<li> Add the MyFilter module as a dependency of the <b>MavenPlatformWordApp - NetBeans Platform based application</b> project.</li>
</ol>
<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 {
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 Utilities 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 method in 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 Libraries 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>TextFilter filter = Lookup.getDefault().lookup(TextFilter.class);
if (filter != null) {
s = filter.process(s);
}</strong>
text.setText(s);
}</pre>
</li>
</ol>
<p class="tips">You can use code completion to help you with the code.</p>
<p>At this point you can check your application to see if it works correctly.
Next you will add a new window component that will display a history of the text that you
processed using the filter.</p>
</div>
<!-- =================================================================== -->
<!-- ++++++++++++++++++ Adding a LookupListener ++++++++++++++++++++++++ -->
<h2><a name="06"></a>Adding LookupListener and InstanceContent to the Text window component</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(NbBundle.getMessage(TextTopComponent.class, &quot;CTL_TextTopComponent&quot;));
setToolTipText(NbBundle.getMessage(TextTopComponent.class, &quot;HINT_TextTopComponent&quot;));
// setIcon(Utilities.loadImage(ICON_PATH, true));
<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();
TextFilter filter = Lookup.getDefault().lookup(TextFilter.class);
if (filter&nbsp;!= null) {
<strong>content.add(s);</strong>
s = filter.process(s);
}
text.setText(s);
}</pre>
</li>
</ol>
<!-- =================================================================== -->
<!-- ++++++++++++++ Creating the History Module ++++++++++++++++++++++++ -->
<h2><a name="07"></a>Creating the History Module</h2>
<p>In this section you will create a module named <strong>History</strong> that will display the
value of the <tt>InstanceContent</tt>.
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">
<ol>
<li>Choose File &gt; New Project (Ctrl-Shift-N).</li>
<li>Select Maven 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 MavenPlatformWordApp directory. Click Finish.</li>
<li>Add the History module as a dependency of the <b>MavenPlatformWordApp - NetBeans Platform based 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 <b>History NetBeans Module</b> in the Projects window and choose New &gt; Other to open the New File dialog.</li>
<li>Select Window in the Module Development category. Click Next.</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.<br />
The wizard displays a list of the files that will be created and the files that will be modified.</li>
</ol>
<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>Add the <tt>private</tt> field <tt>result</tt> and the following code to the constructor of the <tt>HistoryTopComponent</tt>
so that it listens to the lookup of the String class of the current active window
and displays all retrieved String objects in the text area.
<pre class="examplecode">
<strong>private Lookup.Result result;</strong>
public HistoryTopComponent() {
initComponents();
...
<strong>result = org.openide.util.Utilities.actionsGlobalContext().lookupResult(String.class);
result.addLookupListener(new LookupListener() {
public void resultChanged(LookupEvent e) {
historyText.setText(result.allInstances().toString());
}
});</strong>
}</pre>
</li>
</ol>
</div>
<h2><a name="02c"></a>Building and Running the Application</h2>
<p>You can now test the application.</p>
<ol>
<li>Right-click the project node of the <strong>MavenPlatformWordApp NetBeans Platform based application</strong> and choose Clean.</li>
<li>Right-click the project node of the <strong>MavenPlatformWordApp NetBeans Platform based application</strong> and choose Build with Dependencies.</li>
<li>Right-click the project node of the <strong>MavenPlatformWordApp NetBeans Platform based 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/maven-final-app.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 quickstart demonstrated how 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%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>