Study of Maven

Table of Contents

1 Start Maven Example

Maven projects are defined with an XML file named pom.xml. This file gives the project's name, version, and dependencies that it has on external libraries.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
  • <modelVerion> POM model version
  • <groupId> Domain name of the group or organization.
  • <artifactId> Name to the project's library artifact (name of JAR file)
  • <version> Version of the project
  • <packaging> How it should be packaged (in JAR or WAR file)
mvn compile
mvn package
mvn install

The first command compiles the project, and the .classfiles shold be in the target/classes directory. The second command compile the code, run tests and finish by packaging the code up in a JAR file in the target directory. Tht third command does the same thing as the second command, then copy the JAR file into the local dependency repository, under the directories with name of groupId, artifactId and version. So on my machine, its location is ~/.m2/repository/org/springframework/gs-maven/0.1.0/gs-maven-0.1.0.jar.

Add dependencies of project into the pom.xml file, in the <project> element.


This description will tell the maven to get the joda-time package as external library. You can specify a <scope> element to specify if the dependencies are required for compiling the code but will be provided during runtime by provided; or decalre the dependencies are only necessary for compiling and running tests.

When I compile the command, maven downloads the joda-time pakcage from Does the maven use its domain when the group id does not contain the domain by default?

You can also create a project from scratch

mvn archetype:generate -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

1.1 Directory layout

            the classes with the same structure as src/main
            the directories in src/main/resources
            the classes with the same structure as src/mainproject

2 POM Configuration

Fully qualified name of project is "<groupId>:<artifactId>:<version>". If groupId is a domain, it will be transformed into directory path while downloading. Download something with groupId=org.X.A from will download things from in the end.

2.1 Inheritance

When we have two projects, A and B, A is supposed to be parent project of B. You have to specify the pom.xml of project B to indicate that the parent of B is A with the groupId, artifactId and version of A. You can remove the groupId and version of B to make A and B have the same /groupId and version. One can also indicate the <relativePath> for parent project to put the A and B in the same directory level.

B inherite the dependencies from A.所以parent用来继承POM,dependency 用来解决代码依赖. parent的项目在代码上没有关系.

The packages downloaded by maven are put under HOME.m2/repository/.

所有 POM.xml 里面的元素都可以用 ${project.A.B.C} 的方式进行引用.比 方说如果在 XML 根路径下有这样的结构 <A><B>lalala<C>blabla<C/></B></A>, ${project.A.B.C} 就等于 blabla.

Author: Xiao LIU

Created: 2015-02-28 Sat 22:37

Emacs 24.4.1 (Org mode 8.2.10)