Wednesday, September 25, 2013

Quick Start Guide: Creating a Java Maven Project in NetBeans IDE 7.4, 8.0 or Higher

I will show you how to create a Java Maven project in NetBeans IDE 7.4, 8.0 or higher and configure it to create a complete, executable JAR assembly with dependencies.

I am assuming you are already familiar with setting up a default Java Ant project in NetBeans. Unlike a Java Ant project, when creating a Java Maven project, you will not get a project configuration that creates an executable JAR when you build the project. If you follow these steps, you will learn how to create an executable JAR with dependencies, and all will be well with the world again. :-)

You may want to download the sample project to follow along with what I am describing.

  • First, create a new Maven Java Application via File >> New Project >> Categories: Java, Projects: Java Application. Name your project and choose where to place it, or just use the default values if you like.

NetBeans IDE 7.4 New Project Dialog

NetBeans IDE 7.4 New Java Application Dialog for Maven

  • Once you create the project, you will see it in the Projects window on the left, with the directory structure shown in the image below.

NetBeans IDE 7.4 Projects Window

  • If this is the first Maven project you have created on your system, NetBeans will download the index from the Maven Central Repository. It will take a while for it to finish. This folder structure can be viewed in the Services tab under "Maven Repositories."

  • Now you can create whatever new packages and source code files you desire for the project. To add a Library to the project or as it is called in Maven nomenclature—adding a Dependency to your project. You may do this manually, or in the Maven world, you can use the Central Repository to find and download a library.

  • Assuming you have some source code already added to the project and have the the Source/Binary format set to the correct JDK version in the project's properties, add the JTattoo library dependency to the project so you can can choose a new Look & Feel. You can do this two different ways: the first being to right click the Dependencies package and choose "Add Dependency" and the second, to search the Central Repository in the Services tab. To use the Central Repository method, open up the Maven Central Repository node and start typing the package name you are looking for. A search bar will automatically pop up and the tree view will scroll to match your search. Once found, choose a version of the JAR you want and then right click with on the name and choose "Add as Dependency". Screenshots of this method is shown below.

NetBeans IDE 7.4 Choosing JAR from Maven Central Repository


NetBeans IDE 7.4 Choosing Project to Add Dependency

  • Once the Dependency is added, you will see a new folder in the Projects Window called Dependencies.

NetBeans IDE 7.4 Project Window Showing New Dependency

  • If you have any resources, like images or data files, you should add them to your Maven project where Maven expects them to be. This is in the "Other Sources" project folder and the system folder structure is "<ProjectName>/src/main/resources" folder. You may have to create this folder structure under "Other Sources" if the folder does not already exist. You may create the new folder structure using the Files Window. Assuming you created a folder named "images", an image resource is accessible from your code in the form "/images/grumpy cat.png". Below is a screenshot of what this folder structure looks like.

NetBeans IDE 7.4 Resources Directory Structure

  • At this point, your project will build and run in NetBeans, but no executable JAR with dependencies is created. You have to take care of that manually, by adding a plug-in entry to the projects "pom.xml" file. There is more than one plugin that will accomplish building an executable JAR with dependencies, but it is easiest to add the maven-assembly-plugin with it's built-in jar-with-dependencies assembly descriptor. Open the "pom.xml" file located in the "Project File" folder in the Projects Windows and add the plugin entry after the maven-compiler-plugin's closing tag (</plugin>). Below is the XML code for the maven-assembly-plugin. Don't forget to modify the <mainClass> entry to reflect the package and class your main method resides in.

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
       <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
       </descriptorRefs>
       <archive>
          <manifest>                                                                           <mainClass>com.areaofthoughts.grumpycat.GrumpyCat</mainClass>
          </manifest>
       </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>                        
                <goal>single</goal>
            </goals>
         </execution>
     </executions>
</plugin>

Now when you build your project, a full JAR assembly will be created in the target folder of your project and the JAR will run from the command line (java -jar filename.jar) or if you double-click on it. The name of the JAR file will be in the form of "GrumpyCat-SNAPSHOT-1.0-jar-with-dependencies.jar".

NetBeans IDE 7.4 Files Window Showing Target Folder

Now you are up and running!

One last thing, to save you some trouble, if you ever run your project from within NetBeans and choose to set the Main Class permanently (apposed to the current session) an nbactions.xml file is created along side the pom.xml. This is where NetBeans finds the class your main method resides in. There will be up to 3 entries—one each for Run, Debug and Profile. If you ever change the main package name or Main Class name, the entries for <exec.args>, as well as the <mainClass> entry in the pom.xml will not be updated automatically by NetBeans. Wrong entries in the nbactions.xml will prevent the project from running within NetBeans and wrong entries in the pom.xml will prevent the project from running outside of NetBeans. If this is the case, you will have to edit them manually and update the package or class name to reflect the new names.

No comments:

Post a Comment