介绍
Maven是一个构建根据和项目管理根据。默认的文件结构如下:
[图片上传失败...(image-bf8f27-1514799718243)]
下载及安装
maven下载地址:Maven,下载完后解压缩,设置环境变量,maven依赖对应的jdk版本。下面是win10上面的安装步骤:
D:\tools\apache-maven-3.5.2>set JAVA_HOME=D:\tools\Java\jdk1.8.0_20
D:\tools\apache-maven-3.5.2>set M2_HOME=D:\tools\apache-maven-3.5.2
D:\tools\apache-maven-3.5.2>set Path=%PATH%;%M2_HOME%\bin
输入
mvn -v
查看是否安装成功
本地资源库
当你使用mvn install等命令时,maven会将依赖包下载到本地的默认资源库中,目录如下
- Windows: C:\Documents and Settings{your-username}.m2
- Unix/Mac OS X – ~/.m2
当我们想修改资源库的所在目录时,可以通过修改{M2_HOME}\conf\setting.xml来完成。如下:
<settings><!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ~/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
--><localRepository>D:\software\yiibai.com\apache-maven\repository</localRepository>
创建项目
maven创建项目一般使用Archetype来创建。Archetype提供4种创建方式:
- archetype:create 已经过时不用了
- archetype:generate 这是Archetype插件最常用的功能,用以从模板创建一个Maven项目
- archetype:create-from-project 用以从一个Maven项目创建模板
- archetype:crawl 在一个指定的Maven库中查找可以的模板,并更新模板目录
快速建立一个简单的web项目
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp
//根据提示输入对应的信息
Confirm properties configuration:
groupId: test
artifactId: Test
version: 1.0-SNAPSHOT
package: com.test
Y: : y
构建项目
maven使用命令
mvn install
来构建项目,在target目录下会生成构建后的文件。
常用命令详解
- mvn clean :清理项目生产的临时文件,一般是模块下的target目录
- mvn package :项目打包工具,会在模块下的target目录生成jar或war等文件
- mvn test :测试命令,执行src/test/java/下junit的测试用例
- mvn install :模块安装命令,将打包的的jar/war文件复制到你的本地仓库中,供其他模块使用 -Dmaven.test.skip=true 跳过测试(同时会跳过test compile)
- mvn deploy :发布命令,将打包的文件发布到远程仓库
- mvn eclipse:eclipse:生成eclipse配置文件,会在项目文件夹中产生.classpath和.project文件
- mvn eclipse:clean:清除eclipse配置文件
- mvn dependency:analyze:帮助你分析依赖关系, 用来取出无用, 重复依赖的好帮手
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/maven-v4_0_0.xsd">
<!-- maven model version -->
<modelVersion>4.0.0</modelVersion>
<!-- project group id & artifact id -->
<groupId>com.freesoft.mvn-webapp</groupId>
<artifactId>mvnwebapp</artifactId>
<!-- packing type -->
<packaging>war</packaging>
<!-- version -->
<version>1.0-SNAPSHOT</version>
<name>mvnwebapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>mvnwebapp</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<tomcat-url>http://localhost:8080/manager/html</tomcat-url>
<server>tomcat_localtest</server>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<struts.version>2.3.15</struts.version>
<mysql.version>5.1.29</mysql.version>
<hibernate.version>4.3.1.Final</hibernate.version>
</properties>
</project>
基础信息
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<name>...</name>
<url>...</url>
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>
字段 | 解释 |
---|---|
modelVersion | Maven模块版本,目前我们一般都取值4.0.0 |
groupId | 项目或者组织的唯一标志,并且配置时生成路径也是由此生成,如org.myproject.mojo生成的相对路径为:/org/myproject/mojo |
artifactId | 项目的通用名称 |
version | 项目的版本 |
packaging | 打包类型,可取值:jar,war等等,这个配置用于package的phase,具体可以参见package运行的时候启动的plugin |
name | 用户描述项目的名称,可选 |
url | 开发团队的网站,可选 |
dependencies依赖关系
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba.china.shared</groupId>
<artifactId>alibaba.apollo.webx</artifactId>
<version>2.5.0</version>
<exclusions>
<exclusion>
<artifactId>org.slf4j.slf4j-api</artifactId>
<groupId>com.alibaba.external</groupId>
</exclusion>
....
</exclusions>
......
</dependencies>
字段 | 解释 |
---|---|
groupId | 依赖项的groupId |
artifactId | 依赖项的artifactId |
version | 依赖项的版本 |
scope | 依赖项的适用范围:compile,缺省值,适用于所有阶段,会随着项目一起发布。provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。之前例子里的junit就只用在了test中。 |
type | 默认为jar类型,常用的类型有:jar,ejb-client,test-jar...,可设置plugins中的extensions值为true后在增加 新的类型 |
optional | 设置指依赖是否可选,默认为false,即子项目默认都继承,为true,则子项目必需显示的引入,与dependencyManagement里定义的依赖类似 |
exclusions | 如果X需要A,A包含B依赖,那么X可以声明不要B依赖,只要在exclusions中声明exclusion. |
exclusion | 是将B从依赖树中删除,如上配置,alibaba.apollo.webx不想使用com.alibaba.external ,但是alibaba.apollo.webx是集成了com.alibaba.external,r所以就需要排除掉. |
Maven项目的继承:Maven项目之间不仅存在多模块的聚合关系,而且Maven项目之间还可以存在相互继承的关系。Maven项目之间的继承关系通过<parent>表示,在子Maven项目的POM中配置示例如下:
<parent>
<groupId>com.mycompany.jcat</groupId>
<artifactId>jcat-bundle</artifactId>
<version>2.0</version>
<relativePath>../jcat-bundle</relativePath>
</parent>
字段 | 解释 |
---|---|
groupId | 父依赖项的groupId |
artifactId | 父依赖项的artifactId |
version | 父依赖项的版本 |
relativePath | 可选,父项目相对于子项目的路径。 |
dependencyManagement | 是用于帮助管理chidren的dependencies的。例如如果parent使用dependencyManagement定义了一个dependencyon junit:junit4.0,那么 它的children就可以只引用 groupId和artifactId,而version就可以通过parent来设置,这样的好处就是可以集中管理 依赖的详情 |
在子项目中,能够继承父项目的如下配置:
- dependencies
- developers
- contributors
- plugin lists
- reports lists
- plugin executions with matching ids
- plugin configuration
模块,可以通过一个大项目来管理各个小项目
<modules>
<module>my-app</module>
</modules>
properties:是为pom定义一些常量,在pom中的其它地方可以直接引用。
<properties>
<file.encoding>UTF-8</file_encoding>
<java.source.version>1.5</java_source_version>
<java.target.version>1.5</java_target_version>
</properties>
使用方式 如下 :
${file.encoding}
build 构建
build---基础元素
<build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters>
<filter>filters/filter1.properties</filter>
</filters>
...
</build>
字段 | 解释 |
---|---|
defaultGoal | 执行build任务时,如果没有指定目标,将使用的默认值。 如上配置:在命令行中执行mvn,则相当于执行mvn install |
directory | build目标文件的存放目录,默认在${basedir}/target目录 |
finalName | build目标文件的名称,默认情况为${artifactId}-${version} |
filter | 定义*.properties文件,包含一个properties列表,该列表会应用到支持filter的resources中。 |
build---Resources配置
<build>
...
<resources>
<resource>
<targetPath>META-INF/plexus</targetPath>
<filtering>false</filtering>
<directory>${basedir}/src/main/plexus</directory>
<includes>
<include>configuration.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
<testResources>
...
</testResources>
...
</build>
字段 | 解释 |
---|---|
resources | 一个resources元素的列表。每一个都描述与项目关联的文件是什么和在哪里 |
targetPath | 指定build后的resource存放的文件夹,默认是basedir。通常被打包在jar中的resources的目标路径是META-INF |
filtering | true/false,表示为这个resource,filter是否激活 |
directory | 定义resource文件所在的文件夹,默认为${basedir}/src/main/resources |
includes | 指定哪些文件将被匹配,以*作为通配符 |
excludes | 指定哪些文件将被忽略 |
testResources | 定义和resource类似,只不过在test时使用 |
插件
Maven 是一个执行插件的框架,每一个任务实际上是由插件完成的。
Maven 插件通常用于:
- 创建 jar 文件
- 创建 war 文件
- 编译代码文件
- 进行代码单元测试
- 创建项目文档
- 创建项目报告
一个插件通常提供了一组目标,可使用以下语法来执行:
mvn [plugin-name]:[goal-name]
例如,一个 Java 项目可以使用 Maven 编译器插件来编译目标,通过运行以下命令编译
mvn compiler:compile
Nexus搭建maven私服仓库
参考