| // |
| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you under the Apache License, Version 2.0 (the |
| // "License"); you may not use this file except in compliance |
| // with the License. You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, |
| // software distributed under the License is distributed on an |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| // KIND, either express or implied. See the License for the |
| // specific language governing permissions and limitations |
| // under the License. |
| // |
| |
| = Creating NetBeans Platform Applications from Maven Archetypes |
| :jbake-type: platform-tutorial |
| :jbake-tags: tutorials |
| :jbake-status: published |
| :syntax: true |
| :source-highlighter: pygments |
| :toc: left |
| :toc-title: |
| :icons: font |
| :experimental: |
| :description: Creating NetBeans Platform Applications from Maven Archetypes - Apache NetBeans |
| :keywords: Apache NetBeans Platform, Platform Tutorials, Creating NetBeans Platform Applications from Maven Archetypes |
| |
| Welcome to the link:https://netbeans.apache.org/platform/[*NetBeans Platform*]! |
| |
| This document demonstrates how to create a NetBeans Platform application and module from a command line prompt using the Maven build framework. To create this application you do not need to install or use the NetBeans IDE. You will need to create and edit some files, but you can use a simple text editor. However, an IDE can be very helpful when performing tasks such as editing the POM and managing dependencies. |
| |
| In this tutorial you will see that after you install Maven, you can specify goals and parameters and execute Maven goals from the command line prompt to create and build projects. The IDE performs the same task, but provides an interface that simplifies the task of specifying the parameters for creating projects. |
| |
| If you are new to the NetBeans Platform, you might want to watch the screencast series link:https://netbeans.apache.org/tutorials/nbm-10-top-apis.html[Top 10 NetBeans APIs]. |
| |
| |
| |
| |
| |
| *Notes:* |
| |
| * You do not need to download a separate version of the NetBeans Platform to develop applications for the NetBeans Platform. In this tutorial you do not need to install NetBeans IDE. You can use a simple text editor to create classes and modify files. |
| |
| You can download link:https://netbeans.org/projects/samples/downloads/download/Samples/Platform/mavenPlatformApp.zip[a zip archive of the finished project]. |
| |
| Before starting this tutorial you may want to familiarize yourself with the following documentation. |
| |
| * link:http://maven.apache.org/guides/mini/guide-configuring-maven.html[Maven - Guide to Configuring Maven] |
| * link:http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html[Maven in 5 Minutes] |
| * link:http://www.sonatype.com/books/maven-book/reference/introduction.html[Chapter 1. Introducing Apache Maven] (from link:http://www.sonatype.com/books/maven-book/reference/public-book.html[Maven: The Definitive Guide]) |
| |
| |
| == Creating the NetBeans Platform Application |
| |
| In this exercise you will execute the Maven goal ``generate`` from the command line to generate a new NetBeans Platform application from an archetype in the codehaus.org repository. In the command you will specify the parameters that will be used when Maven generates the application. |
| |
| |
| [start=1] |
| 1. Open a command line terminal. |
| |
| [start=2] |
| 1. Test your Maven installation by typing the following command. |
| |
| [source,java] |
| ---- |
| |
| mvn -version |
| ---- |
| |
| After you type the command you will see details about the Maven installation, including the version of Maven and the JDK. |
| |
| |
| [source,java] |
| ---- |
| |
| Apache Maven 2.2.1 (r801777; 2009-08-06 21:16:01+0200) |
| Java version: 1.6.0_20 |
| |
| ---- |
| |
| You need to have Maven version 2.0.9 or newer and JDK 1.6. |
| |
| |
| [start=3] |
| 1. Navigate to the directory where you want to create the NetBeans Platform application. |
| |
| [start=4] |
| 1. Type the following command in the terminal to create a project named *mavenPlatformApp*. |
| |
| [source,java] |
| ---- |
| |
| mvn -DarchetypeVersion=1.5 -Darchetype.interactive=false -DgroupId=com.mycompany -DarchetypeArtifactId=*netbeans-platform-app-archetype* -DarchetypeRepository=http://repository.codehaus.org/ -Dversion=1.0-SNAPSHOT -DarchetypeGroupId=org.codehaus.mojo.archetypes -Dpackage=com.mycompany.mavenplatformapp -DartifactId=mavenPlatformApp --batch-mode org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate |
| ---- |
| |
| You can see that the command specifies the archetype that Maven will use to create the NetBeans Platform application ( link:http://mvnrepository.com/artifact/org.codehaus.mojo.archetypes/netbeans-platform-app-archetype[ ``netbeans-platform-app-archetype`` ]). The command also specifies the location of the repository for the archetype ( ``http://repository.codehaus.org/`` ) and the version ( ``1.5`` ) and GroupId ( ``org.codehaus.mojo.archetypes`` ) of the archetype. |
| |
| When you run the command, Maven will automatically retrieve the archetype and create the project according to the details specified in the command (artifact id, version, package). |
| |
| *Note.* When you use NetBeans IDE to create a NetBeans platform application, the IDE essentially generates and executes the Maven goal with similar parameters. |
| |
| For more information about the plugin that is used to generate the artifacts, see the documentation on the link:http://bits.netbeans.org/mavenutilities/nbm-maven-plugin/[nbm-maven-plugin] at link:http://mojo.codehaus.org/[mojo.codehaus.org]. |
| |
| When you run the command, you will see output similar to the following in the terminal window. |
| |
| |
| [source,java] |
| ---- |
| |
| [INFO] ------------------------------------------------------------------------ |
| [INFO] BUILD SUCCESSFUL |
| [INFO] ------------------------------------------------------------------------ |
| [INFO] Total time: 8 seconds |
| [INFO] Finished at: Thu Sep 02 17:47:03 CEST 2010 |
| [INFO] Final Memory: 13M/81M |
| [INFO] ------------------------------------------------------------------------ |
| |
| ---- |
| |
| If you look in the root directory ``mavenPlatformApp`` that was created, you can see that the new project contains a ``pom.xml`` file and a directory for each of the default sub-modules of the archetype: application and branding. |
| |
| image::images/maven-platform-archetype-default.png[title="Screenshot of structure of mavenPlatformApp"] |
| |
| If you open ``pom.xml`` for mavenPlatformApp in an editor, you will see that the POM specifies all the details required for building the application and modules. |
| |
| |
| [source,xml] |
| ---- |
| |
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| <modelVersion>4.0.0</modelVersion> |
| |
| <groupId>com.mycompany</groupId> |
| <artifactId>mavenPlatformApp</artifactId> |
| <version>1.0-SNAPSHOT</version> |
| <packaging>pom</packaging> |
| |
| <name>mavenPlatformApp - NetBeans Platform Application</name> |
| |
| <repositories> |
| <!-- this is a remote repository hosting the netbeans api artifacts. |
| the versions of the artifacts are netbeans IDE release based, eg. RELEASE65 |
| You might want to use your own repository. To create one, use the nbm:populate-repository goal. |
| --> |
| <repository> |
| <id>netbeans</id> |
| <name>repository hosting netbeans.org api artifacts</name> |
| <url>http://bits.netbeans.org/maven2</url> |
| </repository> |
| </repositories> |
| |
| <dependencies> |
| <dependency> |
| <groupId>junit</groupId> |
| <artifactId>junit</artifactId> |
| <version>4.8.1</version> |
| <scope>test</scope> |
| </dependency> |
| </dependencies> |
| |
| <build> |
| <pluginManagement> |
| <plugins> |
| <plugin> |
| <groupId>org.codehaus.mojo</groupId> |
| <artifactId>nbm-maven-plugin</artifactId> |
| <version>3.2</version> |
| <extensions>true</extensions> |
| <configuration> |
| <brandingToken>${brandingToken}</brandingToken> |
| <cluster>foobar</cluster> |
| </configuration> |
| </plugin> |
| |
| <plugin> |
| <!-- netbeans modules in 6.9+ are 1.6 compatible --> |
| <groupId>org.apache.maven.plugins</groupId> |
| <artifactId>maven-compiler-plugin</artifactId> |
| <version>2.3</version> |
| <configuration> |
| <source>1.6</source> |
| <target>1.6</target> |
| </configuration> |
| </plugin> |
| </plugins> |
| </pluginManagement> |
| </build> |
| |
| <modules> |
| <module>branding</module> |
| <module>application</module> |
| </modules> |
| |
| <properties> |
| <netbeans.version>RELEASE691</netbeans.version> |
| <brandingToken>foo</brandingToken> |
| </properties> |
| </project> |
| ---- |
| |
| If you look at the ``<properties>`` element, you can see that the version of the NetBeans platform is specified as ``RELEASE691`` . In the POM of your sub-modules, you can specify ``${netbeans.version}`` as the version and the module will use the version specified in the parent POM project. The enables you to easily update the NetBeans platform version for all the project's sub-modules. Later in this tutorial you will modify the default version generated in the POM by the archetype to specify ``${netbeans.version}`` . |
| |
| |
| == Installing and Running the NetBeans Platform Application |
| |
| In this exercise you will run the Maven ``install`` goal from the command line to build the application and copy the artifacts in your local repository. The NetBeans Platform application is a multi-module application. You will use the ``reactor`` mechanism in Maven to build the application project and the project's dependencies. After you build the application you will execute the ``run-platform`` goal to launch the application. |
| |
| |
| [start=1] |
| 1. Navigate to the ``mavenPlatformApp`` directory and type the following command to build the project. |
| |
| [source,java] |
| ---- |
| |
| mvn --projects com.mycompany:application --also-make install |
| ---- |
| |
| |
| [start=2] |
| 1. After you build the application, navigate to the ``application`` directory and type the following command to launch the application. |
| |
| [source,java] |
| ---- |
| |
| mvn nbm:run-platform |
| ---- |
| |
| When you run the ``nbm-maven-plugin`` with the ``run-platform`` goal, Maven will launch the branded NetBeans platform application. |
| |
| image::images/maven-command-platform-default.png[title="Screenshot of default application"] |
| |
| For more information of how to create and build multi-module projects, see the link:http://maven.apache.org/guides/mini/guide-multiple-modules.html[Maven - Guide to Working with Multiple Modules] at link:http://maven.apache.org/[maven.apache.org]. |
| |
| For more information about the ``nbm-maven-plugin`` , see the documentation on the `` link:http://bits.netbeans.org/mavenutilities/nbm-maven-plugin/[nbm-maven-plugin]`` and the `` link:http://bits.netbeans.org/mavenutilities/nbm-maven-plugin/run-platform-mojo.html[run-platform]`` goal at link:http://mojo.codehaus.org[mojo.codehaus.org]. |
| |
| You have now successfully created and launched your NetBeans Platform application. The default branding elements are located in the ``branding`` sub-module that was generated according to the archetype. The NetBeans IDE provides tools and wizards that can help you to modify the branding elements. |
| |
| In the next section you will create a new module for the application. |
| |
| |
| == Creating a Module from an Archetype |
| |
| You will now create a new module that is named mavenPlatformModuleA in the ``mavenPlatformApp`` directory. In the same way that you created the NetBeans Platform application, you will run a Maven command from the command line and use the module archetype ``nbm-archetype`` . |
| |
| After you create the module, you will use a text editor to create a simple class in the module and then modify the module's ``layer.xml`` and ``bundle.properties`` files. You will then build the module to install the module in the local repository. |
| |
| |
| [start=1] |
| 1. Navigate to the ``mavenPlatformApp`` directory in the command line terminal and type and run the following command: |
| |
| [source,java] |
| ---- |
| |
| mvn -DarchetypeVersion=1.4 -Darchetype.interactive=false -DgroupId=com.mycompany -DarchetypeArtifactId=*nbm-archetype* -DarchetypeRepository=http://repository.codehaus.org/ -Dversion=1.0-SNAPSHOT -DarchetypeGroupId=org.codehaus.mojo.archetypes -Dpackage=com.mycompany.mavenplatformmodulea -DartifactId=mavenPlatformModuleA --batch-mode org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate |
| ---- |
| |
| You can see that the command specifies ``nbm-archetype`` . The archetype will be retrieved from the codehaus remote repository. |
| |
| When you run the command, you will see output similar to the following in the terminal window. |
| |
| |
| [source,java] |
| ---- |
| |
| [INFO] Scanning for projects... |
| [INFO] Reactor build order: |
| [INFO] mavenPlatformApp - NetBeans Platform Application |
| [INFO] mavenPlatformApp - Platform application branding resources |
| [INFO] mavenPlatformApp - NetBeans Platform based application |
| [INFO] ------------------------------------------------------------------------ |
| [INFO] Building mavenPlatformApp - NetBeans Platform Application |
| [INFO] task-segment: [org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate] (aggregator-style) |
| [INFO] ------------------------------------------------------------------------ |
| [INFO] Preparing archetype:generate |
| [INFO] No goals needed for project - skipping |
| [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'. |
| [INFO] Setting property: velocimacro.messages.on => 'false'. |
| [INFO] Setting property: resource.loader => 'classpath'. |
| [INFO] Setting property: resource.manager.logwhenfound => 'false'. |
| [INFO] [archetype:generate {execution: default-cli}] |
| [INFO] Generating project in Batch mode |
| [INFO] Archetype defined by properties |
| [INFO] ------------------------------------------------------------------------ |
| [INFO] BUILD SUCCESSFUL |
| [INFO] ------------------------------------------------------------------------ |
| [INFO] Total time: 5 seconds |
| [INFO] Finished at: Fri Sep 03 15:01:42 CEST 2010 |
| [INFO] Final Memory: 21M/81M |
| [INFO] ------------------------------------------------------------------------ |
| |
| ---- |
| |
| If you look at the contents of the ``mavenPlatformApp`` directory, you will see that the directory contains a new directory that is named ``mavenPlatformModuleA`` . The new directory contains a ``src`` directory and ``pom.xml`` . The files ``layer.xml`` and ``bundle.properties`` were generated in the ``resources/com/mycompany/mavenplatformmodulea`` directory. |
| |
| |
| image::images/maven-command-modulea.png[title="Screenshot of structure of mavenplatformmodulea"] |
| |
| In the next step you will create a class in the ``java/com/mycompany/mavenplatformmodulea`` directory. |
| |
| |
| [start=2] |
| 1. Use a text editor to create the following class ``HelloWorldAction.java`` in the ``java/com/mycompany/mavenplatformmodulea`` directory. |
| |
| [source,java] |
| ---- |
| |
| package com.mycompany.mavenplatformmodulea; |
| |
| import java.awt.event.ActionEvent; |
| import java.awt.event.ActionListener; |
| import org.openide.DialogDisplayer; |
| import org.openide.NotifyDescriptor; |
| |
| public class HelloWorldAction implements ActionListener { |
| |
| @Override |
| public void actionPerformed(ActionEvent e) { |
| NotifyDescriptor d = new NotifyDescriptor.Message("hello..."); |
| DialogDisplayer.getDefault().notify(d); |
| } |
| |
| } |
| ---- |
| |
| |
| [start=3] |
| 1. Use a text editor to edit the ``layer.xml`` file in the ``resources/com/mycompany/mavenplatformmodulea`` directory to modify the version of the filesystem DTD to version 1.2. |
| |
| [source,xml] |
| ---- |
| |
| <?xml version="1.0" encoding="UTF-8"?> |
| <!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem *1.2//EN*" "https://netbeans.org/dtds/*filesystem-1_2.dtd*"> |
| <filesystem> |
| |
| </filesystem> |
| ---- |
| |
| |
| [start=4] |
| 1. Edit the ``layer.xml`` file to add the following elements (in bold). |
| |
| [source,xml] |
| ---- |
| |
| <?xml version="1.0" encoding="UTF-8"?> |
| <!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN" "https://netbeans.org/dtds/filesystem-1_2.dtd"> |
| <filesystem> |
| *<folder name="Actions"> |
| <folder name="Window"> |
| <file name="com-mycompany-mavenplatformmodulea-HelloWorldAction.instance"> |
| <attr name="delegate" newvalue="com.mycompany.mavenplatformmodulea.HelloWorldAction"/> |
| <attr name="displayName" bundlevalue="com.mycompany.mavenplatformmodulea.Bundle#CTL_HelloWorldAction"/> |
| <attr name="instanceCreate" methodvalue="org.openide.awt.Actions.alwaysEnabled"/> |
| </file> |
| </folder> |
| </folder> |
| <folder name="Menu"> |
| <folder name="Window"> |
| <file name="HelloWorldAction.shadow"> |
| <attr name="originalFile" stringvalue="Actions/Window/com-mycompany-mavenplatformmodulea-HelloWorldAction.instance"/> |
| </file> |
| </folder> |
| </folder>* |
| </filesystem> |
| ---- |
| |
| In this step you added a new item to the Window menu that will invoke ``HelloWorldAction`` . You also specify the ``bundle.properties`` file that specifies the display name for the new item. |
| |
| |
| [start=5] |
| 1. Edit the ``bundle.properties`` file in the ``resources/com/mycompany/mavenplatformmodulea`` directory to add the following item (in bold) that specifies the name for the new Window menu item: |
| |
| [source,java] |
| ---- |
| |
| #OpenIDE-Module-Long-Description= |
| #OpenIDE-Module-Short-Description= |
| #OpenIDE-Module-Name= |
| *CTL_HelloWorldAction=Hello World* |
| ---- |
| |
| |
| [start=6] |
| 1. Edit ``pom.xml`` to make the following changes (in bold) to add a dependency on ``org-openide-dialogs`` and modify the version of the ``org-openide-util`` artifact. |
| |
| [source,xml] |
| ---- |
| |
| <dependencies> |
| <dependency> |
| <groupId>org.netbeans.api</groupId> |
| <artifactId>org-openide-util</artifactId> |
| *<version>${netbeans.version}</version>* |
| </dependency> |
| *<dependency> |
| <groupId>org.netbeans.api</groupId> |
| <artifactId>org-openide-dialogs</artifactId> |
| <version>${netbeans.version}</version> |
| </dependency>* |
| </dependencies> |
| ---- |
| |
| You can use ``${netbeans.version}`` to ensure that the versions of artifacts are consistent and can be easily updated by updating the ``<netbeans.version>`` element in the POM of the parent project. |
| |
| |
| [start=7] |
| 1. In the command line terminal, navigate to the ``mavenPlatformModuleA`` directory and type and run the following command to build and install the module in the local repository. |
| |
| [source,java] |
| ---- |
| |
| mvn install |
| ---- |
| |
| When you run the ``install`` command, Maven builds the module and copies the JAR and NBM artifacts to the local repository. |
| |
| Now that the artifact is in the local repository, you can add the artifact as a dependency of the platform application. |
| |
| |
| == Adding the New Module as a Dependency |
| |
| In this section you will add the module as a dependency of the NetBeans Platform application by declaring the dependency in the POM of the application project. |
| |
| |
| [start=1] |
| 1. Open the ``pom.xml`` file in the ``application`` directory in a text editor. |
| |
| [start=2] |
| 1. Declare the mavenplatformmodulea module as a dependency of the application by making the following changes (in bold) to the POM. |
| |
| [source,xml] |
| ---- |
| |
| <dependencies> |
| <dependency> |
| <groupId>org.netbeans.cluster</groupId> |
| <artifactId>platform</artifactId> |
| <version>${netbeans.version}</version> |
| <type>pom</type> |
| </dependency> |
| |
| <dependency> |
| <groupId>com.mycompany</groupId> |
| <artifactId>branding</artifactId> |
| <version>1.0-SNAPSHOT</version> |
| </dependency> |
| *<dependency> |
| <groupId>com.mycompany</groupId> |
| <artifactId>mavenPlatformModuleA</artifactId> |
| <version>1.0-SNAPSHOT</version> |
| </dependency>* |
| </dependencies> |
| ---- |
| |
| |
| == Building the Application With the New Module |
| |
| You are now almost ready to launch the NetBeans Platform application with the new module installed. You first need to make sure that the new module is specified as a module of the parent POM project. You can then repeat the steps to build and launch the application. |
| |
| |
| [start=1] |
| 1. Open the ``pom.xml`` in the ``mavenPlatformApp`` directory in a text editor. |
| |
| [start=2] |
| 1. Confirm that the new module is specified in the list of modules that are included with the application. |
| |
| [source,xml] |
| ---- |
| |
| <modules> |
| <module>branding</module> |
| <module>application</module> |
| *<module>mavenPlatformModuleA</module>* |
| </modules> |
| ---- |
| |
| |
| [start=3] |
| 1. Navigate to the ``mavenPlatformApp`` directory in the command line terminal and type the following command to build the project. |
| |
| [source,java] |
| ---- |
| |
| mvn --projects com.mycompany:application --also-make install |
| ---- |
| |
| When you run the command, you can see that the new module is included in the list of modules. |
| |
| |
| [source,java] |
| ---- |
| |
| [INFO] Scanning for projects... |
| [INFO] Reactor build order: |
| [INFO] mavenPlatformApp - NetBeans Platform Application |
| [INFO] mavenPlatformApp - Platform application branding resources |
| [INFO] mavenPlatformModuleA NetBeans Module |
| [INFO] mavenPlatformApp - NetBeans Platform based application |
| ---- |
| |
| |
| [start=4] |
| 1. Navigate to the ``application`` directory and type the following command to launch the application. |
| |
| [source,java] |
| ---- |
| |
| mvn nbm:run-platform |
| ---- |
| |
| When you run the command, Maven will launch the application with the new module. |
| |
| You have now successfully added a new module to the application and launched the application. To confirm that the module was loaded when the application launched, choose Window > Hello World from the main menu of the application to open the new Message dialog box. |
| |
| image::images/maven-command-hello-dialog.png[title="Screenshot of Message Dialog"] |
| |
| You can download an archive containing the sources of this application from the following link: |
| |
| * link:https://netbeans.org/projects/samples/downloads/download/Samples/Platform/mavenPlatformApp.zip[mavenPlatformApp.zip] |
| |
| *Note.* To build and run the application from the archive, expand the archive and then execute the ``install`` and ``run-platform`` goals that are described above. Maven will automatically download any required artifacts to your local Maven repository. |
| |
| This tutorial 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 link:https://netbeans.apache.org/kb/docs/platform.html[NetBeans Platform Learning Trail]. |
| |
| link:http://netbeans.apache.org/community/mailing-lists.html[ Send Us Your Feedback] |
| |
| |
| |
| |
| == See Also |
| |
| This document described how to use the Maven build framework to create a new NetBeans Platform application without using NetBeans IDE. For more information about creating and developing applications, see the following resources. |
| |
| * link:https://netbeans.apache.org/tutorials/nbm-maven-quickstart.html[NetBeans Platform Quick Start Using Maven] |
| * link:https://netbeans.apache.org/kb/docs/platform.html[NetBeans Platform Learning Trail] |
| * link:http://bits.netbeans.org/dev/javadoc/[NetBeans API Javadoc] |
| |
| If you have any questions about the NetBeans Platform, feel free to write to the mailing list, dev@platform.netbeans.org, or view the link:https://netbeans.org/projects/platform/lists/dev/archive[NetBeans Platform mailing list archive]. |
| |