构建(build)一个项目包括:下载依赖,编译源代码,执行单元测试,以及打包编译后的源代码等一系列子任务。手工的执行这些任务是一项很是繁琐,且容易出错的事情。Maven封装了这些子任务,并提供给用户执行这些子任务的命令。简而言之,Maven是Java项目的一个管理和构建(build)工具。html
项目对象模型(如下简称POM)是Maven的基本组成单元,它是位于项目主目录下的一个xml文件(pom.xml),Maven使用pom.xml文件中的信息来构建(build)项目。spring
一个简单的pom.xml的结构看起来以下:apache
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>my-app</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <properties> <maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.target>1.6</maven.compiler.target> </properties> <build> <plugins> <plugin> //... </plugin> </plugins> </build> </project>
本小结将介绍pom.xml中经常使用的一些元素。编程
groupId
- 建立项目的公司或者组织的名称artifactId
- 项目的名称version
- 项目的版本号packaging
- 项目的打包方式(jar/war/pom)GAV (groupId:artifactId:version)是Maven项目的惟一表示符。packaging决定Maven生成包的类型,默认值是jar。app
pom.xml文件经过元素<dependency>
来声明外部依赖,例如:maven
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies>
声明项目依赖3.8.1版本的junit。默认状况下,Maven将在构建项目的时候从Maven的中央仓库(Central Repository)下载依赖到本地仓库(${USER_HOME/.m2/repository/})。不过用户也能够经过元素<repository>
来声明备选仓库(alternate repository)。编程语言
能够像在Java等编程语言中定义变量同样,在pom.xml文件中定义属性,这样就能够达到一处定义,多处使用的目的,使用和维护起来也更加容易。ide
pom.xml中经过元素<properties>
定义属性,经过占位符${property_name}
使用属性。以下:工具
<properties> <spring.version>4.3.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
若是在未来须要升级spring-core和spring-context的版本,那时将只须要修改属性spring.version
的值。单元测试
元素<build>
中能够指定默认的Maven Goal的信息,编译后的文件的名称,名称等信息。以下:
<build> <defaultGoal>install<defaultGoal> <directory>${basedir}/target</directory> <finalName>${artifactId}-${version}</finalName> ... </build>
在实际的平常开发中,在不一样的环境(test/prod) 一般会有不一样的构建(build)逻辑或者配置。元素<profile>
能够用来实现这个目的。以下:
<profiles> <profile> <id>prod</id> <build> <plugins> <plugin> //... </plugin> </plugins> </build> </profile> <profile> <id>test</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <plugins> <plugin> //... </plugin> </plugins> </build> </profile> </profiles>
若是想触发某个profile,可使用命令:mvn clean install -P<profile id>
。
Maven的构建生命周期定义了如何构建和发布Java项目,Maven内建了三种构建生命周期:clean,default,site。构建生命周期由一系列的阶段(phase)构成:
具体各个阶段的定义能够参考Maven文档。命令 mvn <phase>
能够用来执行<phase>以及<phase>以前的阶段。例如:mvn install
将执行validate ~ install全部的阶段。
......
多模块(multi-module)和 plugin goal,等等。