Open DongShi opened 7 years ago
about POM
Maven uses a more declarative approach, meaning that you specify in the Maven POM file what to build, but now how to build it. The POM file describes your project resources - not how to build it. Contrarily, an Ant file describes how to build your project. In Maven, how to build your project is predefined in the
POM example
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>名.司.公</groupId>
<artifactId>工程名</artifactId>
<version>1.0.0</version>
</project>
All Maven POM files inherit from a super POM. If no super POM is specified, the POM file inherits from the base POM. Here is a diagram illustrating that:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<relativePath>../my-parent</relativePath>
</parent>
<artifactId>my-project</artifactId>
...
</project>
mvn help:effective-pom
Maven Settings File Maven has two settings files. In the settings files you can configure settings for Maven across all Maven POM files. For instance, you can configure:
Location of local repository Active build profile Etc. The settings files are called settings.xml. The two settings files are located at:
The Maven installation directory:
$M2_HOME/conf/settings.xml
The user's home directory:
${user.home}/.m2/settings.xml
Both files are optional. If both files are present, the values in the user home settings file overrides the values in the Maven installation settings file.
example to change local repo
<settings>
<localRepository>
d:\data\java\products\maven\repository
</localRepository>
</settings>
Maven Directory Structure
- src
- main
- java
- resources
- webapp
- test
- java
- resources
- target
The webapp directory contains your Java web application, if your project is a web application. The webapp directory will then be the root directory of the web application. Thus the webapp directory contains the WEB-INF directory etc.
When this POM file is executed by Maven, the two dependencies will be downloaded from a central Maven repository and put into your local Maven repository.
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
An external dependency in Maven is a dependency (JAR file) which is not located in a Maven repository (neither local, central or remote repository).
@
${basedir}\war\WEB-INF\lib\mydependency.jar The ${basedir} points to the directory where the POM is located. The rest of the path is relative from that directory. The groupId and artifactId are both set to the name of the dependency.
<dependency>
<groupId>mydependency</groupId>
<artifactId>mydependency</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${basedir}\war\WEB-INF\lib\mydependency.jar</systemPath>
</dependency>
Snapshot Dependencies
Snapshot dependencies are dependencies (JAR files) which are under development. Instead of constantly updating the version numbers to get the latest version, you can depend on a snapshot version of the project.
<version>1.0-SNAPSHOT</version>
Maven has three types of repository:
Local repository Central repository Remote repository
Build Life Cycles Maven has 3 built-in build life cycles. These are:
default clean site
LC > phases > goals
Build Phase Description validate Validates that the project is correct and all necessary information is available. This also makes sure the dependencies are downloaded. compile Compiles the source code of the project. test Runs the tests against the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed. package Packs the compiled code in its distributable format, such as a JAR. install Install the package into the local repository, for use as a dependency in other projects locally. deploy Copies the final package to the remote repository for sharing with other developers and projects.
Maven Unit Test Report
mvn surefire-report:report
mvn surefire-report:report-only
How to skip test in maven build: see here http://www.mkyong.com/maven/how-to-skip-maven-unit-test/ Simple way is
$ mvn package -DskipTests
reference is here: http://tutorials.jenkov.com/maven/maven-tutorial.html