2. Agenda
▪ What is Maven
▪ Getting and installing
▪ Basics: POM, GAV, Archetype
▪ pom.xml contents
▪ Some useful plugins
3. What is Maven
▪Maven is a build automation tool used
primarily for Java projects.
▪ Maven addresses two aspects of building
software:
1. it describes how software is built
2. it describes its dependencies
5. History
– Maven 1 (2003)
– Maven 2 (2005)
▪ Not backwards Compatible
– Maven 3 (2010)
▪ Same as Maven 2 but more stable and with some
additional features
9. Arche-who? O_o
An archetype is defined as an original pattern or model
from which all other things of the same kind are made.
Archetype is a Maven project templating toolkit.
10. Maven + IDE
mvn idea:idea mvn eclipse:eclipse
Generate project files for the most popular IDEs
12. Maven Project Object Model (POM)
▪ Describes a project
– Name and Version
– Artifact Type
– Source Code Locations
– Dependencies
– Plugins
– Profiles (Alternate build config.)
▪ Uses XML by default
13. Project Name (GAV)
▪ Maven uniquely identifies a project using:
– groupID: project grouping identifier (no spaces
or colons)
▪ Usually loosely based on Java package
– artfiactId: name of project (no spaces or
colons)
– version: Version of project
▪ Format {Major}.{Minor}.{Maintanence}
▪ Add ‘-SNAPSHOT ‘ to identify in development
14. Project Name (GAV)
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.lds.training</groupId>
<artifactId>maven-training</artifactId>
<version>1.0</version>
<name>Windows 8</name>
<description>The best OS ever!</description>
<packaging>jar</packaging>
<properties>
<java.version>1.6</java.version>
<properties>
</project>
16. Dependency scope
• compile (default)
• provided (by JDK or a container at runtime)
• runtime (not required for compilation)
• test (used only during tests)
• system
• import (only available in Maven 2.0.9 or later)
Build automation is the act of scripting or automating a wide variety of tasks that software developers do in their day-to-day activities including things like:compiling computer source code into binary codepackaging binary coderunning testsdeployment to production systemscreating documentation and/or release notes
mvn installInvokes generate* and compile, test, package, integration-test, installmvn clean Invokes just cleanmvn clean compileClean old builds and execute generate*, compilemvn compile installInvokes generate*, compile, test, integration-test, package, installmvn test cleanInvokes generate*, compile, test then cleans
1. Install JDK, set JAVA_HOME2. Download and extract Apache Maven (http://maven.apache.org/)3. Add MAVEN_HOME system variable4. Add MAVEN_HOME to PATH5.“mvn –version“.
Maven is opinionated about project structuretarget: Default work directorysrc: All project source files go in this directorysrc/main: All sources that go into primary artifactsrc/test: All sources contributing to testing projectsrc/main/java: All java source filessrc/main/webapp: All web source filessrc/main/resources: All non compiled source filessrc/test/java: All java test source filessrc/test/resources: All non compiled test source files
<plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.6</source> <target>1.6</target> <compilerArgument>-proc:none</compilerArgument> </configuration> <executions> <execution> <id>run-annotation-processors-only</id> <phase>generate-sources</phase> <configuration> <compilerArgument>-proc:only</compilerArgument> <!-- If your app has multiple packages, use this include filter to execute the processor only on the package containing your entities --> <!-- <includes> <include>**/model/*.java</include> </includes> --> </configuration> <goals> <goal>compile</goal> </goals> </execution> </executions></plugin>
For Spring apps use onejar-maven-plugin<plugin><groupId>com.jolira</groupId><artifactId>onejar-maven-plugin</artifactId><version>1.4.4</version><executions><execution><configuration><mainClass>com.package.core.Main</mainClass><!-- Optional, default is false --><attachToBuild>true</attachToBuild><!-- Optional, default is "one jar" --><classifier>onejar</classifier></configuration><goals><goal>one-jar</goal></goals></execution></executions></plugin>