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
Using Maven 3 to build a new project
Section titled “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
Section titled “Auto generate your pom”Determine your own group and artifact id, and run the following
mvn archetype:generate -DgroupId=com.magicmonster -DartifactId=test-appIt 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
Section titled “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
Section titled “cleaning build”Run mvn clean package
src code dir
Section titled “src code dir”Move your java code to src/main/java otherwise it will not be compiled.
1.5 not supported
Section titled “1.5 not supported”I was receiving errors for
generics are not supported in -source 1.3for-each loops are not supported in -source 1.3To 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
Section titled “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
Section titled “Executing Java”As part of my build I have a Java program that needs executing. The exec plugin can accomplish this.
Troubleshooting
Section titled “Troubleshooting”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 installTransitive Dependencies
Section titled “Transitive Dependencies”If your project uses another project, dependencies maybe be automatically visible and packaged into your war file
settings.xml
Section titled “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
Section titled “JVM options”To add JVM options you can set the environment variable MAVEN_OPTS
MAVEN_OPTS="-Djava.endorsed.dirs=E:\jurn\endorsed" mvn clean installproperties
Section titled “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>