views:

1392

answers:

4

Our software is written in Java and comprise many (7) projects.

These projects are Netbeans ant projects. I'm considering to converting them to maven2.

Where can I find some hints for doing such thing?

A: 

This won't be an easy task since Maven2 expects the files to be organized in a specific way. Anyway Better Builds with Maven is a free book that should get you started. It will help you understand Maven and it also has a chapter on migration.

kgiannakakis
+7  A: 

Don't read that book. It will only make you confused. Read this book instead: "Maven - The definitive guide" http://www.sonatype.com/books/maven-book/reference/ .

Also, the maven site has a lot of information, but the structure is terrible so you'll need to use google to navigate in it.

Here is my suggestion:

  1. Do this by hand, not with "automagic" "help" from the IDE. Maven integration doesn't work that good yet, not in any IDE.

  2. Make sure you program project is divided into modules under a common umbrella module, so that each module produces a single binary artifact (jar, war,...) possibly accompanied by the javadoc of the source code behind that artifact, a zip with the source code etc. The basic principle is that each module produces a single artifact, containing all the non-test-code under that module. You can do this while the project is still built by ant.

  3. Each module should conform to the standard maven directory layout. The build destination is under [module]/target/[output-type, e.g. "classes"]. The source code is under [module]/src/main/[src-type e.g. "java"] and [module]/test/[src-type]. The artifact consists of all the code under src/main, and none of the code under src/test, as it built to the target directories. You can do this while the is still built by ant.

  4. Start by transforming the sub-module that has no dependencies on other modules in the project.

  5. Now you can create the parent maven module pom.xml with artifact type "pom", consisting of one of the modules below. Make a child module for the first submodule (the one with only external dependencies), using the umbrella module as "parent". Remember that you need to specify version for the parent. Remember to add the child module as a "module" in the parent too. Always use ${project.version} as version in the child modules when you create multi-module projects like this. All modules under a parent must be released simultaneously in a single operation, and if you use this setting maven will make sure the version fields stay the same across all modules and gets updated everywhere during the release. This may make it difficult to re-use the existing numbering scheme, but that doesn't matter. You are never going to run out of version numbers anyway.

  6. Add the necessary dependencies, and make sure you can build the parent and the child module together using the command "mvn clean install" from the parent module.

  7. Proceed with the rest of the modules the same way. Dependencies to other modules under the same parent project should also use ${project.version} as the "version" they are depending on, meaning "the same version as this". NOTE THAT in order to build, the module you are depending on must be built using "mvn install", so that it gets deployed to you local (computer) repository. Otherwise the depending module will not be able to find the classes. There are NO source-code dependencies between modules in maven, only dependencies to built and packed versions installed in local and remote repositories. This can be very confusing if you come from ant-projects. Build from the root module until you get comfortable with this. It takes two days.

  8. Don't use maven integration in IDEs. It is a bad idea. Use "mvn idea:idea" or "mvn eclipse:eclipse" to set up your workspace as a non-maven ordinary IDE project. The inter-module dependencies mechanisms in maven and the IDE aren't identical and will never be. Also, if you have several mavenized projects with dependencies in between, you want to have several of these in your workspace with dependencies set up between. You can do this with mvn idea:idea / eclipse:eclipse if you create a separate maven project file called "workspace.xml" (or whatever) in the same directory as parent module, set up as a multi-module project containing modules "." and "../otherproject" (only one-way reference here, no parent ref back). If you run "mvn idea:idea / eclipse:eclipse -f workspace.xml" you get a workspace with all these modules linked together. No IDE integration lets you do that. This sound like a lot of extra work, but the workspace.xml-file is really small. It doesn't have to contain all that dependency stuff and all that, only the reference to the modules you want to bind together in your IDE.

eirikma
There is no svn netbeans:netbeans. But the NetBeans Maven plugin understands the POM and handles dependency management nicely: http://wiki.netbeans.org/MavenBestPractices
mjustin
On point 2, it can be helpful to keep the parts of the ant script that builds the deployment and replace that at the very end.On point 8, I find that both eclipse and idea do a better job of importing maven poms than maven does at creating projects. YMMV
sal
A: 

I have built a script to migrate Ant builds to Maven. You can find more information here:

http://erichauser.net/2009/10/26/ant2maven-easy-migration-from-ant-to-maven-with-nexus/

It won't help you with fixing your directory structure and or any additional Ant tasks, but it removes a lot of the tedious steps to get started.

There is a link to the GitHub repository at the bottom which contains the script.

Eric Hauser
+1  A: 

I discovered that the migration is not necessary. The real requirements that I need was automatic download of dependencies (libraries).

This is also achieved by Ivy which nonetheless uses maven repositories.

I solved converting project from ant to ant+ivy with IvyBeans.

Andrea Francia