To simplify the build processes of a project---Maven
Maven概述:
为项目的构建提供了一个标准的方案, 使得项目组成的定义更加清晰,更容易地实现项目的构建。使得jar在多个项目里面的共享。
创建Maven项目:
- 使用命令行创建Maven项目:
mvn archetype:generate
-DgroupId=com.mycompany.app
-DartifactId=my-app
-DarchetypeArtifactId=maven-archetype-quickstart
-DarchetypeVersion=1.4 -DinteractiveMode=false
- 使用IDEA创建Maven项目:
两种方式中相关属性说明:
- groupId:一般是公司或者组织域名的倒序。
- artifactId:项目名称(如果是多个工程构成的项目,这里就指工程名)。
- archetypeArtifactId:Maven提供的archetype(可以通俗地理解为模板,Maven project templating toolkit)。
- (Version)archetypeVersion:该项目(工程)的版本号。
POM(项目对象模型,pom.xml文件)
<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>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
- Is the core of a project's configuration in Maven.
- 项目中用到的其他Jar包通过dependency的方式引入。
- 以dependency方式引入的依赖,所依赖的jar也会自动引入。
- 自己写的代码可以通过Packge打包,下次在其他项目引入即可。
- 依赖先找本地Maven仓库,没有就去远程中央仓库找。
一个POM文件至少包含以下内容:
project root.
modelVersion - should be set to 4.0.0.
groupId - the id of the project's group.
artifactId - the id of the artifact (project)
version - the version of the artifact under the specified group
构建生命周期
Maven的构建生命周期是由阶段组成(阶段又由插件目标(Plugin Goals)组成),默认的构建生命周期包含如下生命周期:
validate- validate the project is correct and all necessary information is available.compile- compile the source code of the project.test- test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed.package- take the compiled code and package it in its distributable format, such as a JAR.verify- run any checks on results of integration tests to ensure quality criteria are met.install- install the package into the local repository, for use as a dependency in other projects locally.deploy- done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.
注意:阶段是由上而下依次执行,可通过命令行跳过一些阶段,如:mvn package -Dmaven.test.skip=true
Super POM:所有的POM文件都继承自SuperPOM,除非有明确的设置。
<project>
<modelVersion>4.0.0</modelVersion>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>
<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
<pluginManagement>
<!-- NOTE: These plugins will be removed from future versions of the super POM -->
<!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<outputDirectory>${project.build.directory}/site</outputDirectory>
</reporting>
<profiles>
<!-- NOTE: The release profile will be removed from future versions of the super POM -->
<profile>
<id>release-profile</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<updateReleaseInfo>true</updateReleaseInfo>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>