Tinu is a DZone Zone Leader and has posted 338 posts at DZone. You can read more from them at their website. View Full User Profile

Maven 3 Support in NetBeans IDE 7.0

11.18.2010
| 23420 views |
  • submit to reddit

The new Beta build of NetBeans IDE 7.0 offers complete support for Maven 3. (Apache Maven 3 release was announced in October, and focused on performance and usability improvements.) In this interview, NetBeans engineer Jesse Glick speaks about what new and existing Maven users should know about Maven integration in the NetBeans IDE.

What types of Maven projects can users work with in the NetBeans IDE?

Any valid Maven project can be at least opened in NetBeans. Java source roots can be opened with the usual code completion and similar features. General Maven capabilities such as dependency management and starting builds can be done from the IDE's UI (as well as directly in pom.xml, with various forms of code completion). The IDE also provides special support for certain "packagings"--Java EE applications, OSGi bundles, and NetBeans modules.

File > New Project also offers a Maven category so you can create new applications using Maven. Wizards are provided for some common project types such as Java EE 6 WARs. You can also pick from among the hundreds of "archetypes" (project templates) available to support popular technologies--everything from JIRA plugins to Scala/lift apps to Jini services. This is a great way to get your feet wet with a new framework.

With the release of Maven 3, what has changed about Maven integration in the NetBeans IDE?

Opening projects should be somewhat faster. Because the IDE is using the official Maven 3.0 code to parse your project, a lot of corner cases are now handled correctly that previously were not. And builds run from the IDE by default use the bundled Maven 3.0, which is generally faster than 2.x, and emits a number of warnings about configuration mistakes that ought to be fixed.

What other improvements in Maven support are included in NetBeans 7.0?

There have been numerous fixes and improvements to the Maven support in 7.0 beyond what was needed to integrate Maven 3.0. The NetBeans 7.0 New & Noteworthy page lists some of the more visible changes.

What is the level of compatibility for existing Maven 2 projects and with new Maven 3 projects?

Actual builds use the regular apache-maven-3.0/bin/mvn executable, so they should behave exactly like builds not involving the IDE. Maven 3.0 is generally compatible with projects created for Maven 2.x. There were a few listed incompatible changes which are typically easily fixed, and in many cases beneficial to fix even for 2.x users. From that list, the most common sources of problems I have encountered are:

  1. Projects which relied on profiles.xml now cannot move relevant profiles into pom.xml or settings.xml according to whether they are user-specific or sharable.
  2. "Legacy" repositories such as http://download.java.net/maven/1/ can no longer be used. Fortunately, most content is already in proper repositories.
  3. You need a new version of the Site plugin to work under Maven 3. The old version can still be used by Maven 2 builds if you define a profile.
  4. <parent> links need to use the actual version of the parent, and the actual <relativePath> if not ../pom.xml. Maven 2.x failed to warn about this kind of mistake.

As far as the IDE's parsing of Maven projects and display of their metadata (such as dependencies), full compatibility with the 3.0 command line is intended. Field testing should turn up any outstanding issues.

For those on the fence about Maven, who have had challenges with it in the past, or who use other build tools, how will their experience in the NetBeans IDE be different?

First, Maven differs from all other popular build tools in that the POM declares what the project contains and uses, rather than being a script in some sort of procedural language. (You can still insert bits of script using Ant, Groovy, etc. for unusual build tasks, but this is the exception rather than the rule.) This means that the IDE can effectively read the POM and know what to present to the user: where source roots are, what the dependencies are, what build steps ("plugins") are in use, how plugins are configured. IDE support for a build tool like Ant is mostly limited to running the script and offering basic syntax completion when editing it, so NetBeans defines proprietary metadata like the "nbproject" directory so it can open projects with some kind of structure. By contrast, NetBeans can at least open any existing Maven project, and important metadata is picked up directly from the POM. For example, the IDE's Java editor will automatically use the same classpath for showing live errors and hints as maven-compiler-plugin uses when actually building the project. IDE features to manipulate the POM, like dependency conflict resolution, work directly with Maven. There are plenty of littler features that make everyday development easier, too; for example, if your POM declares a popular license, File > New File will try to insert the appropriate copyright header.

Furthermore, NetBeans requires no "import" step to work with Maven projects the way some IDEs do: if you have the project on disk, the project - or any file in it - can be opened right away, with no need for any IDE-specific control files. You can also use Team > Create Build Job to ask the Hudson continuous integration server to begin building and testing your project with no configuration up front.

Second, Maven's repository and dependency system is very powerful and helps you maintain comprehensible, properly layered software. Libraries are cached in a repository, not checked in to source control. You can require that all components of your application depend on official releases of libraries declared in a single place - and this can even apply to libraries you wrote, so every component can be built in isolation. You do not need to check out a library project from version control just to get Javadoc and sources for debugging - but you can check it out and start making changes with a single step from the IDE. Most popular free/open-source Java libraries are already available in public Maven repositories for immediate use. There is no need to manually download and manage JARs and track down Javadoc, and standard Maven tools like maven-dependency-plugin can tell you if newer releases of any dependencies are available. maven-release-plugin provides a standard way to publish discrete releases of your own software, and repository managers like Nexus can index everything and enforce consistency rules.

NetBeans helps you work with repositories and dependencies with ease. Perhaps you found a neat code snippet on a developers' site using some API you had never heard of before. Paste it into the Java editor, and a hint will appear suggesting you add that library as a dependency, based on the class name appearing in a public repository index.

NetBeans Maven - Search Dependencies

NetBeans Maven - Picking Dependency

Browse the library under the Dependencies node beneath the project and open Javadoc, or pick a different version.

NetBeans Maven - Using Dependency

Right-click the project and select Show Dependency Graph to see if the library has any upstream dependencies you don't want, or which conflict with your preferred version.

NetBeans Maven - Dependency Graph

Open the Maven Repository Browser window to see the universe of software in a searchable format (also integrated with the Quick Search toolbar); and open the metadata viewer for a library of interest so you can visit the project's bug tracker site, check out sources, or even find projects already using it.

NetBeans Maven - Repository Browser

Comparing the performance of Maven to other build tools may be missing the point, since the development models can be very different. Compiling and packing classes into a JAR runs the javac and jar commands regardless of the build tool, and the "Compile on Save" mode is available in NetBeans for Maven projects (including Java EE) just as for any other. What is more interesting is that the IDE's classpath scanning runs faster against a JAR than a source tree, and the up-to-date check is far faster for a JAR (i.e. a project-to-binary dependency rather than a project-to-project dependency). Most dependencies in a Maven project are against binary releases, so the IDE's Java editor does not need to constantly check for API changes in your classpath. This also works between projects you wrote, if you use "release" dependencies - making development of a very large Java application potentially much more efficient than when using one big Ant-based project or a tree of interdependent Ant projects. "Snapshot" dependencies can be used when you actually wish to work on parallel changes in two or more projects, in which case you pay the cost of the up-to-date checks just among those projects, and you can change your mind at any time just by switching dependency versions.

In parting, Maven 3.0 is a long-awaited release and I am proud to work on one of the first IDE integrations. I hope to see plenty of people trying it out in NetBeans 7.0 Beta and the nightly builds, and filing bugs early so the 7.0 release can be really polished in this area.

Published at DZone with permission of its author, Tinu Awopetu.

Comments

Max Mohave replied on Wed, 2011/10/19 - 5:54pm

Maven and Netbeans works well with handling multimodule dependency but seems to have a problem handling class shadowing (a bad practice by the way). Suppose you have two modules: CoreCode CoreCodeOverrides (which is dependent on CoreCode) CoreCode has the following package and class: CoreCode com.mycompany.property.BuildProperty Also I want to override some class (I know we should extend rather than override but I'll continue) So I have the identical packagein my override module CoreCodeOverrides com.mycompany.property.BuildProperty com.mycompany.property.BuildCustomProperty (plus an extra class) Now the class BuildCustomProperty calls a method located in com.mycompany.property.BuildProperty but this method does not exist in module CoreCode. Netbeans keeps trying to go to the dependent module, rather than search first in the same package. That is, BuildCustomProperty uses the dependent module rather than first looking in its same package for resolution of an class or artifact. Don't know how to change this either!

Jesse Glick replied on Tue, 2011/10/25 - 3:29pm in response to: Max Mohave

NetBeans is not behaving any worse than the Java runtime itself would in the same situation, at least assuming that you simply place both CoreCode.jar and CoreCodeOverrides.jar on the classpath (rather than using a specialized container where ClassLoader.loadClass is overridden): which version of BuildProperty will actually be loaded at runtime depends on the order of JARs in the -classpath argument, which is arbitrary as far as Maven is concerned. If the two classes have different signatures, you can in fact get linkage errors at runtime.

If you are sure you have to do what you describe (generally a bad idea), the best setup would be to factor the original BuildProperty out of CoreCode into a separate project which CoreCoreOverrides does not depend on, so that CoreCodeOverrides is not shadowing any class as far as either maven-compiler-plugin or the IDE are concerned. At runtime it is then up to you to manipulate the classpath or class loaders to ensure that the desired variant of the class is found.

Sirikant Noori replied on Fri, 2012/03/30 - 1:05pm

The NetBeans IDE is an award-winning integrated development environment available for Windows, Mac, Linux, and Solaris. The NetBeans project consists of an open source IDE and an application platform that enable developers to rapidly create web, enterprise, desktop, and mobile applications using the Java platform, as well as PHP, JavaScript and Ajax, Groovy and Grails, and C/C++.

Java Exam

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.