maven

Published: Sunday, 24 June 2007
Last modified: Monday, 14 February 2011

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

See the Getting started guide

Using Maven 3 to build a new project

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

Auto generate your pom

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.

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

Missing dependencies

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.

Executing Java

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

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

Goals

mvn clean install

Transitive Dependencies

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

settings.xml

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>
  <localRepository>e:/.m2/repository</localRepository>
  ..

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

JVM options

To add JVM options you can set the environment variable MAVEN_OPTS

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

properties

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>
maven