Skip to content

maven

Maven is a build automation tool, like ant. It also keeps versions of ‘artifacts’ or jars in a repository.

See the Getting started guide

I used the following maven in 5 minutes guide.

After installing, run it using mvn instead of maven.

Verify that the installation is OK by running mvn --version

Determine your own group and artifact id, and run the following

mvn archetype:generate -DgroupId=com.magicmonster -DartifactId=test-app

It will prompt you to choose from a list of templates, you can choose defaults by pressing enter. If you accept the defaults you’ll end up with a standard jar project.

Archetype choices now include over 1000 items. To create an empty webapp you can specify -DarchetypeArtifactId=maven-archetype-webapp.

The archetype plugin will create a ‘test-app’ directory. It won’t work if you already have an existing ‘test-app’ directory.

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.

Run mvn clean package

Move your java code to src/main/java otherwise it will not be compiled.

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

pom.xml
<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. Check the compiler plugin documentation for the default source and target settings.

This is much like maven 1. To download spring framework add the following dependency into the pom.xml

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.0.6</version>
</dependency>

Also see Dependency scope is specified in the pom.xml. You have more option now. Dependencies of dependencies are automatically imported.

As part of my build I have a Java program that needs executing. The exec plugin can accomplish this.

Exception in thread “main” java.lang.NoClassDefFoundError: com/werken/forehead/Forehead

Section titled “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 the maven1 installation instead.

mvn clean install

If your project uses another project, dependencies maybe be automatically visible and packaged into your war file

settings.xml is found in the .m2 directory of your home directory.

You can set a different repo at by specifying a different localRepository.

settings.xml
<settings>
<localRepository>e:/.m2/repository</localRepository>
..

Use the -s option to override the location of settings.xml.

To add JVM options you can set the environment variable MAVEN_OPTS

MAVEN_OPTS="-Djava.endorsed.dirs=E:\jurn\endorsed" mvn clean install

You can set properties that can be referred to elsewhere in your pom using the el notation ${}

<properties>
<spring.version>4.1.1.RELEASE</spring.version>
</properties>
...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>