| <!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 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.2 and |
| NetBeans IDE 7.2. If you |
| are using an earlier version, see <a href="71/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/74/netbeans_stamp_74_73_72.png" class="stamp" width="114" height="114" alt="Content on this page applies to NetBeans IDE 7.2" title="Content on this page applies to NetBeans IDE 7.2"/></p> |
| <ul class="toc"> |
| <!-- <li><a href="#config">Configuring Maven</a></li>--> |
| <li><a href="#01">Creating a Maven Based NetBeans Platform Application Project</a></li> |
| <li><a href="#02">Creating Your First Maven Based 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">Listening 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.2</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 7 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 Maven in NetBeans IDE</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://platform.netbeans.org/tutorials/nbm-maven-commandline.html">NetBeans Platform Command Line Tutorial</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 > Other > 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 Maven Based 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 creates 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 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> |
| |
| <div class="indent"> |
| |
| <ol> |
| <li>Choose File > 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/72/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 to an appropriate folder on your system. For purposes |
| of this tutorial, keep the default values of the Maven |
| settings, such as the group ID, as shown below: |
| </p> |
| <p><img src="../images/tutorials/maven-quickstart/72/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/72/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 create |
| all the modules separately.</p> |
| <p>Click Finish.</p> |
| </li> |
| </ol> |
| |
| </div> |
| |
| <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/72/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"><modules> |
| <module>branding</module> |
| <module>application</module> |
| </modules></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 use the Branding Editor to modify the name of the application and |
| replace the default image for the splash screen.</p> |
| <div class="indent"> |
| |
| <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/72/brand-1.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> |
| |
| </div> |
| </div> |
| |
| |
| <!-- =================================================================== --> |
| <!-- +++++++++++++++ Creating the MavenWordEngine Module +++++++++++++++ --> |
| |
| |
| <h2><a name="02"></a>Creating Your First Maven Based NetBeans Module</h2> |
| |
| <p>In this section, you create a new module named MavenWordEngine. |
| You 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 create a new module project in the same directory that contains the branding module and application module.</p> |
| <div class="indent"> |
| <ol> |
| <li>Choose File > New Project from the main menu. Select NetBeans Module from the Maven category:</p> |
| <p><img src="../images/tutorials/maven-quickstart/72/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. Click Browse and locate the MavenPlatformWordApp directory |
| as the Project Location:</p> |
| <p><img src="../images/tutorials/maven-quickstart/72/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/72/new-module-3.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> |
| </div> |
| |
| <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"><modelVersion>4.0.0</modelVersion> |
| <parent> |
| <artifactId>MavenPlatformWordApp-parent</artifactId> |
| <groupId>com.mycompany</groupId> |
| <version>1.0-SNAPSHOT</version> |
| </parent> |
| |
| <groupId>com.mycompany</groupId> |
| <artifactId><b>MavenWordEngine</b></artifactId> |
| <version>1.0-SNAPSHOT</version> |
| <packaging>nbm</packaging> |
| |
| <name>MavenWordEngine</name></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"><plugin> |
| <groupId>org.codehaus.mojo</groupId> |
| <artifactId><strong>nbm-maven-plugin</strong></artifactId> |
| <version>3.7</version> |
| <extensions>true</extensions> |
| </plugin></pre> |
| |
| <p>If you look at the POM for the parent project, |
| you can see that <strong>MavenWordEngine</strong> was added |
| to the list of modules:</p> |
| |
| <pre class="examplecode"><modules> |
| <module>branding</module> |
| <module>application</module> |
| <module><strong>MavenWordEngine</strong></module> |
| </modules></pre> |
| |
| <h3><a name="02b"></a>Making the Module a Dependency of NetBeans Platform Application</h3> |
| <p>In this exercise, you declare the MavenWordEngine module as a dependency of the |
| <b>app</b> project |
| by adding the dependency in the POM. |
| 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. You can expand the list of non-classpath dependencies |
| to see the full list of dependencies.</p> |
| |
| <p>To add the dependency to the <b>app</b> project's POM, you can edit the POM directly |
| in the editor or, as done below, use the Add Dependency dialog from the Projects window.</p> |
| |
| <div class="indent"> |
| <ol> |
| <li><p>Expand the <strong>app</strong> in the Projects window, |
| right-click the Dependencies node, and choose Add Dependency:</p> |
| <img src="../images/tutorials/maven-quickstart/72/add-dep-1.png" alt="Screenshot of Add Dependency dialog" title="Screenshot of Add Dependency dialog" /> |
| </li> |
| <li><p>Click the Open Projects tab and select <strong>MavenWordEngine</strong>:</p> |
| <p><img src="../images/tutorials/maven-quickstart/72/add-dep-2.png" alt="Screenshot of Add Dependency dialog" title="Screenshot of Add Dependency dialog" /></p> |
| <p>Click OK.</p> |
| </li> |
| <li><p>If you expand the <b>app</b> in the Projects window, |
| you can see that MavenWordEngine is now listed as a dependency:</p> |
| <p><img src="../images/tutorials/maven-quickstart/72/add-dep-3.png" alt="Screenshot of Add Dependency dialog" title="Screenshot of Add Dependency dialog" /></p> |
| </li> |
| </ol> |
| </div> |
| |
| <h3><a name="02c"></a>Adding a Window Component to the Module</h3> |
| |
| <p>In this exercise, you use a wizard to add a Window Component to the MavenWordEngine module.</p> |
| |
| <div class="indent"> |
| <ol> |
| <li>Right-click <strong>MavenWordEngine</strong> in the Projects window |
| and choose New > Window. Select <strong>output</strong> in Window Position:</p> |
| <p> |
| <img src="../images/tutorials/maven-quickstart/72/new-win-1.png" alt="Screenshot of window component page in New File wizard" title="Screenshot of window component page in New File wizard" /> |
| </p> |
| </li> |
| <li><p>Type <strong>Text</strong> in the Class Name Prefix:</p> |
| <img src="../images/tutorials/maven-quickstart/72/new-win-2.png" alt="Screenshot of window component page in New File wizard" title="Screenshot of window component page in New File wizard" /> |
| <p>Click Finish.</p> |
| </li> |
| <li><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> |
| <img src="../images/tutorials/maven-quickstart/72/add-dep-4.png" alt="Screenshot of window component page in New File wizard" title="Screenshot of window component page in New File wizard" /> |
| </li> |
| </ol> |
| </div> |
| |
| <h3><a name="02d"></a>Modifying the Window Component</h3> |
| |
| <p>In this exercise, you add a text area and a button to the window component. |
| You then modify the method invoked by the button to change the |
| letters in the text area to upper case letters.</p> |
| |
| <div class="indent"> |
| <ol> |
| <li><p>Click the Design tab of <tt>TextTopComponent.java</tt> in the editor. |
| Drag and drop a button and a text area from the Palette (Ctrl-Shift-8) |
| onto the window. Right-click the text area and choose |
| Change Variable Name, and then type <strong>text</strong> as the name. |
| You use the name when accessing the component from your code. |
| Set the text of the button to "<strong>Filter!</strong>". You |
| should now see the following:</p> |
| <p><img src="../images/tutorials/maven-quickstart/72/new-win-3.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> |
| </div> |
| |
| <h3><a name="02e"></a>Trying Out the Application</h3> |
| |
| <p>To try out the application, to make sure that everything is working correctly, |
| right-click the project node of the |
| <strong>application</strong> and choose Build with Dependencies:</p> |
| <p><img src="../images/tutorials/maven-quickstart/72/try-1.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> |
| |
| <p>The action mapped to "Build with Dependencies" builds 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> |
| |
| <pre class="examplecode">Reactor Build Order: |
| |
| MavenPlatformWordApp-parent |
| MavenPlatformWordApp-branding |
| MavenWordEngine |
| MavenPlatformWordApp-app</pre> |
| <p>The results of the build are also displayed in the Output window.</p> |
| |
| <pre class="examplecode">Reactor Summary: |
| |
| MavenPlatformWordApp-parent ....................... SUCCESS [0.720s] |
| MavenPlatformWordApp-branding ..................... SUCCESS [4.427s] |
| MavenWordEngine ................................... SUCCESS [5.845s] |
| MavenPlatformWordApp-app .......................... SUCCESS [22.644s] |
| ------------------------------------------------------------------------ |
| BUILD SUCCESS |
| ------------------------------------------------------------------------ |
| Total time: 34.679s |
| Finished at: Tue Sep 18 11:29:33 CEST 2012 |
| Final Memory: 15M/134M |
| ------------------------------------------------------------------------</pre> |
| |
| <p>To run the project, in the Projects window, right-click the project node of |
| the <strong>application</strong>, and choose Run.</p> |
| <p><img src="../images/tutorials/maven-quickstart/72/try-2.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> |
| <p>After the application launches, you can try out |
| the application by performing the following steps.</p> |
| <ol> |
| <li>Choose Window > 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 decouple the user interface from the |
| business logic in your application. You start by creating a module |
| that provides an API. Then you create a module that implements |
| the API. Finally, you change the window defined above so that |
| implementations of the API are loaded at runtime into the application. |
| In that way, the GUI is 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 provides 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>Setting Up the Module</h3> |
| <p>In this exercise, you perform the following steps to create the TextFilter module. |
| These steps are the same as you did previously to create the WordEngine module above.</p> |
| |
| <div class="indent"> |
| <ol> |
| <li><p>Choose File > New Project (Ctrl-Shift-N). |
| As explained in the previous section, |
| select NetBeans Module from the Maven category. Click Next. |
| Type <b>TextFilter</b> for the Project Name. |
| Click Browse to set the Project Location and |
| locate the directory of the parent project. |
| Click Next, choose the version of the NetBeans APIs you want |
| to use, and click Finish.</p> |
| |
| <p>When you click Finish, the IDE creates the module and opens the module project |
| <b>TextFilter</b> in the Projects window. |
| 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"><modules> |
| <module>branding</module> |
| <module>application</module> |
| <module>MavenWordEngine</module> |
| <module>TextFilter</module> |
| </modules></pre></li> |
| <li><p>After you create the module you need to add the module as a dependency of |
| the <b>app</b> project. |
| Right-click the Dependencies node of the <strong>app</strong> |
| project and choose Add Dependency. Click the Open Projects tab |
| in the Add Dependency dialog. Select the <strong>TextFilter</strong> |
| module. Click OK.</p> |
| <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"><dependency> |
| <groupId>${project.groupId}</groupId> |
| <artifactId>TextFilter</artifactId> |
| <version>${project.version}</version> |
| </dependency></pre> |
| </li> |
| </ol> |
| </div> |
| |
| <h3><a name="04c"></a>Adding an Interface to the TextFilter Module</h3> |
| <p>In this exercise, you add a simple interface to the TextFilter module.</p> |
| |
| <div class="indent"> |
| <ol> |
| <li> Right-click the <strong>TextFilter</strong> module and choose New > 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: |
| <pre class="examplecode">package com.mycompany.textfilter; |
| |
| public interface TextFilter { |
| <strong>String process(String s);</strong> |
| }</pre> |
| </li> |
| </ol> |
| </div> |
| |
| <h3><a name="04d"></a>Making the TextFilter Interface Public</h3> |
| <p>In this exercise, you 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> |
| |
| <div class="indent"> |
| <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/72/public-1.png" alt="Screenshot of Properties dialog" title="Screenshot of Properties dialog" class="margin-around b-all" /> |
| </p> |
| <p>Click OK.</p></li> |
| </ol> |
| </div> |
| |
| <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><plugin> |
| <groupId>org.codehaus.mojo</groupId> |
| <artifactId>nbm-maven-plugin</artifactId> |
| <version>3.7</version> |
| <extensions>true</extensions> |
| <configuration> |
| <b><publicPackages> |
| <publicPackage>com.mycompany.textfilter</publicPackage> |
| </publicPackages></b> |
| </configuration> |
| </plugin></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 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> is decoupled from <b>MavenWordEngine</b>.</p> |
| <div class="indent"> |
| |
| <h3><a name="05a"></a>Setting Up the Module</h3> |
| <p>In this exercise, you create a module named <strong>MyFilter</strong>. |
| To create the module, you perform the |
| same steps that you performed when you created the TextFilter module.</p> |
| <div class="indent"> |
| <ol> |
| <li>Choose File > New Project (Ctrl-Shift-N). |
| As explained in the previous section, select |
| NetBeans Module from the Maven category. Click Next. |
| Type <b>MyFilter</b> for the Project Name. |
| Click Browse to set the Project Location and locate the |
| application directory. Click Finish.</li> |
| <li>As described previously, add the MyFilter module as a dependency of |
| the <b>app</b> project. Right-click the Dependencies node of the <strong>app</strong> |
| project and choose Add Dependency. Click the Open Projects tab |
| in the Add Dependency dialog. Select the <strong>MyFilter</strong> |
| module. Click OK. |
| </li> |
| </ol> |
| </div> |
| |
| <h3><a name="05b"></a>Making Code from the TextFilter Module Accessible to the MyFilter Module</h3> |
| <p>In this exercise you add the TextFilter module as a dependency of the MyFilter module.</p> |
| <div class="indent"> |
| <ol> |
| <li><p>Right-click the Libraries node of the <b>MyFilter</b> project and choose Add Dependency:</p> |
| <p><img src="../images/tutorials/maven-quickstart/72/add-dep-5.png" alt="Screenshot of Properties dialog" title="Screenshot of Properties dialog" /> |
| </p> |
| </li> |
| <li><p>Click the Open Projects tab in the Add Dependency dialog. |
| Select the <strong>TextFilter</strong> module:</p> |
| <p><img src="../images/tutorials/maven-quickstart/72/add-dep-6.png" alt="Screenshot of Properties dialog" title="Screenshot of Properties dialog" /> |
| </p> |
| <p>Click OK.</p></li> |
| </ol> |
| </div> |
| <h3><a name="05c"></a>Implementing the TextFilter Interface</h3> |
| <p>In this exercise, you add a Java class with a single method that is named <tt>process</tt> |
| that converts a string to upper case. |
| You also specify that the class implements the TextFilter interface. |
| You use an <tt>@ServiceProvider</tt> annotation to specify that TextFilter is a service |
| that is registered at compile time.</p> |
| <div class="indent"> |
| <ol> |
| <li>Right-click the <b>MyFilter</b> module and choose New > 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><p>Modify the class to add the following code.</p> |
| <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">To use the <tt><a href="http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/lookup/ServiceProvider.html">@ServiceProvider</a></tt> annotation, you need |
| to set a dependency in your module on the NetBeans Lookup API. When |
| the module is compiled, a NetBeans annotation processor will create |
| a META-INF/services folder with a file named after the interface, containing |
| the name of the implementing class. Via <tt>Lookup.getDefault</tt>, in the |
| next section in this tutorial, the file will be found and, from there, the class |
| implementing the requested interface will be loaded.</p> |
| </li> |
| </ol> |
| </div> |
| |
| <h3><a name="05d"></a>Loading Implementations of the TextFilter Interface</h3> |
| <p>In this exercise, you modify the event handler in the JButton in the WordEngine to load implementations of the |
| TextFilter interface. |
| Before adding the code that uses the TextFilter interface in the WordEngine, you need to declare a dependency on the |
| TextFilter module.</p> |
| <div class="indent"> |
| <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 |
| the <tt>TextTopComponent</tt> in the source editor. |
| Modify the <tt>jButton1ActionPerformed</tt> button handler method to add the following code. |
| |
| <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> |
| </div> |
| <p>At this point you can run your application. It should look and behave exactly as before, with the |
| difference that the user interface is now decoupled from the business logic.</p> |
| <p><img src="../images/tutorials/maven-quickstart/72/try-2.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> |
| <p>In the next sections, you publish content whenever the "Filter!" button is clicked. |
| You will then create another new NetBeans module with a window that listens to the selection |
| and displays 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 publish the contents of the text area when |
| the "Filter!" button is clicked.</p> |
| <div class="indent"> |
| <ol> |
| <li><p>In the <strong>MavenWordEngine</strong> module, add an <tt><a href="http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/lookup/InstanceContent.html">InstanceContent</a></tt> object |
| to the <tt><a href="http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/Lookup.html">Lookup</a></tt> of the TopComponent.</p> |
| |
| <p class="tips">For details on <tt><a href="http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/Lookup.html">Lookup</a></tt>, <tt><a href="http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/lookup/InstanceContent.html">InstanceContent</a></tt>, and |
| related concepts, see <a href="http://wiki.netbeans.org/NetBeansDeveloperFAQ#Lookup">NetBeans Lookup</a> in the <a href="http://wiki.netbeans.org/NetBeansDeveloperFAQ">NetBeans Developer Wiki</a>.</p> |
| |
| <pre class="examplecode">public final class TextTopComponent extends TopComponent { |
| |
| <b>private InstanceContent content;</b> |
| |
| public TextTopComponent() { |
| |
| initComponents(); |
| |
| setName(Bundle.CTL_TextTopComponent()); |
| setToolTipText(Bundle.HINT_TextTopComponent()); |
| |
| <b>content = new InstanceContent(); |
| associateLookup(new AbstractLookup(content));</b> |
| |
| } |
| ... |
| ... |
| ...</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> |
| </div> |
| <p>Now a String is 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 is available throughout the |
| application.</p> |
| <p>In the next section, we create a new module that listens to the |
| Lookup and displays objects found there.</p> |
| |
| <!-- =================================================================== --> |
| <!-- ++++++++++++++ Creating the History Module ++++++++++++++++++++++++ --> |
| |
| <h2><a name="07"></a>Listening to the Selection</h2> |
| <p>In this section, you create a module named <strong>History</strong> that |
| displays Strings |
| found in the Lookup of the selected TopComponent. |
| To create the module, you perform the same steps that you performed when |
| you created the TextFilter and MyFilter modules.</p> |
| |
| <div class="indent"> |
| <h3><a name="07p"></a>Setting Up the Module</h3> |
| <div class="indent"> |
| |
| <ol> |
| <li>As done in previous sections, choose File > New Project (Ctrl-Shift-N). |
| Select NetBeans Module from the Maven category. Click Next. |
| Type <strong>History</strong> for the Project Name. |
| Click Browse to set the Project Location to the parent directory. Click Finish.</li> |
| <li>As done in previous sections, add the History module as a dependency of the <b>app</b> project.</li> |
| <li>As done in previous sections, right-click the <b>History</b> module in the Projects window and |
| choose New > Window. Select <b>editor</b> in Window Position. Click Next. |
| Type <b>History</b> in Class Name Prefix. Click Finish.</li> |
| </ol> |
| </div> |
| <p>You now have a new NetBeans module, registered in the <b>app</b> project, |
| with a new TopComponent named HistoryTopComponent.</p> |
| <h3><a name="07b"></a>Using the History Window to Listen to the Selection</h3> |
| |
| <p>You now add a text area element to the window component that displays the filtered strings.</p> |
| <div class="indent"> |
| <ol> |
| <li>Click the Design tab of <tt>HistoryTopComponent.java</tt> in the editor. |
| 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>. |
| The window 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<String> 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> |
| </div> |
| |
| <h2><a name="08"></a>Running the Application</h2> |
| <p>You can now try out the application.</p> |
| <div class="indent"> |
| <ol> |
| <li>Right-click the project node of the <strong>parent</strong> and choose Clean.</li> |
| <li>Right-click the project node of the <strong>app</strong> and choose Build with Dependencies.</li> |
| <li>Right-click the project node of the <strong>app</strong> and choose Run.</li> |
| </ol> |
| </div> |
| |
| <p>When you click Run, the IDE launches the NetBeans Platform application. |
| You can open the Text window and the History window from the Window menu. You should |
| see the following:</p> |
| |
| <p><img src="../images/tutorials/maven-quickstart/72/result-1.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&subject=Feedback:%20NetBeans%20Platform%20Quick%20Start%207.2%20Using%20Maven"> |
| Send Us Your Feedback</a></div> |
| |
| <p> </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> |