maven
Maven build tool. Like ant, but also keeps versions of 'artifacts' or jars.
Using Maven 2 to build a new project
I am following the maven in 5 minutes guide.
After installing use 'mvn' instead of 'maven' to to use it.
Verify the installation is OK by running 'mvn --version'
Auto generate your pom
mvn archetype:create -DgroupId=com.magicmonster -DartifactId=test-app
This will create a 'test-app' directory IN the direct you run this from. It won't work if you have an existing 'test-app' directory
This is annoying if you have already created your files, so I moved the pom.xml up a level and will remove the src and test directories later.
You can use:
mvn archetype:generate -DgroupId=com.magicmonster -DartifactId=test-app
instead, and it will prompt you about the type of artifact you want to create
mvn package
I ran 'mvn package' but this is wrong because I wanted to create a webapp not a jar.
I have opened up pom.xml, and changed the packaging element to a 'war' instead of 'jar'. The very short guide to webapps says when using archetype:create we could have specified -DarchetypeArtifactId=maven-archetype-webapp
add web.xml for webapp
maven complained about a missing web.xml. By default maven will look for it in src/main/webapp/WEB-INF/web.xml so I have moved it there.
Move all jsp and static files to src/main/webapp too, then they will appear in the war file.
cleaning build
run "maven clean package"
src code dir
Move your java code to src/main/java otherwise it will not be compiled.
1.5 not supported
I was receiving errors for
generics are not supported in -source 1.3
for-each loops are not supported in -source 1.3
To fix add
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
to pom.xml. I'm not sure why I need to configure the plugin this way.. wouldn't the compiler come with my project by default?
Missing dependencies
This is much like maven 1. To download spring frame work add the following dependency into the pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.0.6</version>
</dependency>
Dependency scope is specified in the pom.xml. You have more options now
Dependencies of dependencies are automatically used now.
Executing java
As part of my build I have a java program that needs executing.
I found a plugin called antrun . The antrun usage guide helps if you already know ant. I have set this to execute during the 'generate-sources' phase. A list of phases can be found at the lifecycle guide. I am not sure which one is good to use.
classpaths can be 'exported' the the ant builder using instructions found under Referencing the maven classpaths .
Troubleshooting
Exception in thread "main" java.lang.NoClassDefFoundError: com/werken/forehead/Forehead
This happens when running maven 1 (maven executable) and environment variable MAVEN_HOME is pointing to the maven2 installation. Fix the MAVEN_HOME environment variable to point to maven1 installation instead.

