maven

Maven build tool. Like ant, but also keeps versions of 'artifacts' or jars.

Getting started

1. Using Maven 3 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

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 pick from a list of templates, you can pick defaults by pressing enter. If you accept the defaults you'll end up with a standard jar project.

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

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. The exec plugin can accomplish this.

2. 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.

3. Goals


    mvn clean install
    

4. Transitive Dependencies

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

5. 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>
  ..
  

refer to http://maven.apache.org/ref/2.0.8/maven-settings/settings.html

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

6. 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
    

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