Working with Maven in NetBeans 6.7.1
If you are a NetBeans user working with Maven, you're in luck with NetBeans 6.7.1! This latest release comes with a swathe of cool features to help you work with your Maven projects pretty much out-of-the-box.
Maven is an excellent tool, much appreciated for its ability to help harmonize and standardize build practices within an organization. Maven offers powerful features such as standard build lifecycles and directory structures, easy code quality metrics reporting, and sophisticated transitive dependency management, just to name a few. However, Maven build scripts can be as large and cumbersome as any other self-respecting XML file, and so IDE support can come in handy for larger scale projects. Your IDE also needs to understand the Maven lifecycle phases and the Maven dependency mechanism to be able to let you work efficiently from within your IDE.
Eclipse has traditionally had rich, though sometimes brittle, Maven support in the form of the m2eclipse plugin. NetBeans Maven support, on the other hand, is a more recent innovation, but that has been getting better and better with each release. In this article, we will look at the support for Maven in the latest NetBeans release, NetBeans 6.7.1.
Creating a Maven project in NetBeans
A new Maven project is created using a simple Wizard-style dialog. First of all, you select the archetype (or "project template", in layman's terms) of your choosing from the large and ever-growing list of public archetypes, and then you provide a
artifactId, and version number for your project. Nothing very suprising here. However, you do also have the option of creating more sophisticated Maven project structures using a few very useful shortcuts. For example, you can directly create a Maven Web Application or a Maven EJB Module, without having to manually pick the right archetype. And the Enterprise project option lets you create a multi-module Maven project containing three modules: an EJB module, a WAR module, and an EAR module.
Working with existing Maven projects is a breeze, too. NetBeans opens Maven projects natively - all you need do is show it the folder containing your pom.xml file. This works both for simple Maven projects and for nested, multi-module structures.
Working with your Maven project
Once your Maven project has been created or opened, you will see a Maven-friendly project layout, containing separate folders for source code and unit tests, separate folders for 'Libraries' (for your compile-scope dependencies), 'Test Libraries' (for the test-scope dependencies), and so on. For multi-module projects, you can see the nested modules, and open them into full projects as required.
Figure 1. NetBeans understands the Maven project structure
NetBeans also understands the Maven build lifecycle: in fact, it automatically binds its menu commands to the underlying Maven lifecycle. For example, when you run 'build' in Netbeans, it does a 'mvn install' under the hood. When you run 'test', Netbeans runs a 'mvn test' and displays the results in a nice green-bar/red-bar format. This approach also avoids synchronization issues between your IDE project and your Maven project, which you get in IDEs like IntelliJ and, to a lesser extent, Eclipse, which have their own custom project format.
Managing the Pom file
The pom.xml file is the heart of the Maven build process, so it makes sense that a Maven-friendly IDE should treat it as a first-class citizen. In m2eclipse, you get a sophisticated graphical editor that lets you manage virtually all aspects of your pom file. In particular this is a real bonus for newer Maven developers as they learn the ropes of the pom file structure. NetBeans does not have this level of integration yet, but its understanding of the Maven pom file structure has improved massively with this latest version.
Figure 2. Creating a new Maven project in NetBeans 6.7.1
For starters, NetBeans provides a Maven-savvy tree-view structure of the pom.xml file, called the Pom model Navigator. This view displays the various sections and parts of the pom.xml file, and can even distinguish which components of the pom file come from your pom file, and which are inherited from parent poms. This can come in handy, especially for more complicated project structures. The Pom model Navigator view is however very much a real-only view of the pom.xml file, and still lacks the forms-based editing of the pom.xml file you get in m2eclipse.
Figure 3. The POM navigator lets you view the pom.xml file in a Maven-aware tree structure.
Within the pom editor itself, Maven-specific support includes usual auto-completion on the
artifactId, and versions in dependencies, but also lets you insert plugins, profiles, dependencies, and dependency exclusions via the "Insert Code" contextual menu. While you are still pretty much on your own when it comes to configuring the plugins themselves (unlike m2eclipse, which has templates for the more common plugin configurations), it is still very useful to be able to look them up via a full-text index.
Figure 4. Adding a new plugin.
Dependency Management is one of the most important features of Maven, and support for Maven Dependency management has greatly improved in NetBeans 6.7.1.You can now add both dependencies and plugins using a convenient search dialog similar to the one provided in m2eclipse. In practice, this means that you no longer have to remember the
artifactId, and version of the dependency (or plugin) you need.
Figure 5. Adding a new dependency.
Another very useful feature, particularly in an Enterprise context, is the 'Dependency Management' tab. In Maven 2, you can define a set of "optional" dependencies in a parent project, in order to centralize and standardize the versions and configurations of common dependencies used across an organization. If a project requires one of these libraries, it just needs to include a dependency containing only the group and artifact IDs - the version number will be inherited from the DependencyManagement section in the parent project. The 'Dependency Management' tab lets you pick and choose which dependencies you need in your project, from amongst those defined in the inherited DependencyManagment sections. This also makes users aware of what dependencies have been defined in the DependencyManagment sections.
Figure 6. Excluding dependencies.
One of the nicer features in the m2eclipse plugin in Eclipse the various graphical dependency views. With this new release, Netbeans has something like this too. You can study the project dependencies using either a list of resolved dependencies by scope, or a graph-like view. The graph view provide some help in identifying and resolving dependency conflicts, though not so comprehensively as m2eclipse. Overall, the graph view is not as mature or as informative as the one in m2eclipse, and a little messy for my liking, but it's certainly a welcome new feature.
Figure 7. The Dependencies Graph shows you a graphical view of your project dependencies.
The bottom line is, if you are a NetBeans fan, the latest NetBeans release comes packed with features that can make it easier to work with Maven projects. The integration is seamless, and the interface well designed and ergonomic. If you are a hardened Eclipse user, the Maven integration offered by m2eclipse is still top-class, though it does suffer from some of the intrinsic limitations of Eclipse (lack of support for nested projects, for example). Indeed, most of the features described here have been available in m2eclipse for some time, and are more mature in many cases. Eclipse users should definitely keep an eye on m2eclipse and on the upcoming Sonatype Studio (a commercial version of m2eclipse with extra features). However the NetBeans Maven integration blends very smoothly into the IDE. And in any case it's great to see the quality of Maven IDE support growing strongly in at least two of the three major Java IDEs.
|width="1" height="1" border="0" alt=" " />|