Creating a Java Project using Maven support

Apache Maven is a build automation and project management tool hosted by the Apache foundation.

It uses a XML file called Project Object Model file (POM) to describe tasks, dependencies and goals that will be used to package, compile and even deploy out application in servers. Recently, Apache Maven has had a surge of interest since it has become an industry standard; it simplifies development, facilitates a test-driven approach, can be used for continuous integration, and simplifies cooperation between developers in a team who are working with different tools. For these reasons, the NetBeans team decided that the IDE should have built-in support for it. Since version 6.7, this is bundled together with the IDE, making the usage of Maven as easy as possible.

Maven can also be used by the command line, its primary usage, but a handful of commands must be memorized, such as package and clean. By using NetBeans instead, one can rely on a list of commands that are already pre-loaded with the IDE. This will ensure that the developer is just one click away from finding out the command that keeps on slipping their mind.

Getting ready

Maven is already built-in to the NetBeans IDE, but if you want to use Maven in your everyday work, you should also have Maven installed in your system. Although using the bundled Maven in NetBeans is not a good practice for production environments, it can be used as a temporary solution.

Apache Maven can be found at: http://maven.apache.org

The latest release of NetBeans supports Maven 3.0, which is also the latest release from Apache.

How to do it...

  1. To create a New Project either click on File and then New Project or press Ctrl+Shift+N. On the New Project window, two panes will be shown. Choose Maven at the Categories side, Java Application on the Projects side, then click Next.
  2. At the Name and Location step, Project Name should be filled with HelloMaven.
  3. Leave all the other options, meaning: Project Location, Group Id, Version, and Package, with their default settings, and click Finish.

How it works...

The first time a Maven project is created with NetBeans, the IDE will automatically handle all Maven configurations and generate the necessary files for future project creations.

How it works...

Maven archetypes are the models where projects are defined, maintained, and developed. Selecting the Maven Archetype will create a standard Java project. NetBeans will do the following (you can confirm this in the Project View):

  1. Create the package structure.
  2. Copy the necessary libraries to the Libraries folder.
  3. Copy the necessary testing libraries to the Test Libraries folder.
  4. Automatically generate a main class and place it under Source Packages.
  5. Automatically generate a test class and place it under Test packages.
  6. Place the POM file under Project files.

There's more...

Learn why the IDE needs to download a repository locally and how to edit the POM file.

Speeding Maven development

The IDE will also download a repository index from the central repository. This local index of the commonly-used archetypes will speed up the creation process when new archetypes are necessary.

Editing POM

Under the Projects tab, expand the Project Files node and double-click on pom.xml. This will open the XML editor, making it possible to edit this file. Feel free to explore what each tag means and edit it.

See also

For importing an existing Maven Project, try our Importing existing Maven Projects recipe in the NetBeans Head First chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.162.76