maven常用命令
打包命令
# 清理打包
mvn clean package
# 打包跳过单元测试,但是编译测试用例类生成相应的class文件至target/test-classes下
mvn clean package -DskipTests=true
# 打包跳过单元测试,并且不编译测试用例
mvn clean package -Dmaven.test.skip=true
常用插件
maven-resources-plugin
设置resources的编码
项目属性设置resources编码:
<!--设置项目构建的源码编码-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
或者通过显示依赖maven-resources-plugin,修改encoding属性:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
...
<encoding>UTF-8</encoding>
...
</configuration>
</plugin>
更改项目资源路径
<resources>
<resource>
<directory>resource1</directory>
</resource>
<resource>
<directory>resource2</directory>
</resource>
</resources>
注意,如果你想引入
com.xxx
下的所有xml文件到classpath的com/xxx/**/*.xml
下,需要指定targetpath属性,默认会将配置文件放到classpath的根路径下。
include/exclude
<resource>
<directory>src/my-resources</directory>
<includes>
<include>**/*.txt</include>
</includes>
<excludes>
<exclude>**/*test*.*</exclude>
</excludes>
</resource>
Filtering
使用
我们都知道Maven有个 mvn resources:resources
命令,用来将resources资源处理到target目录下;假设,在src/main/resources/hello.txt
中包含以下字符串:
Hello ${name}
在配置为:
<resource>
<directory>src/main/resources</directory>
<!--<filtering>true</filtering>-->
</resource>
即<filtering>
为false
的情况下,maven在处理src/main/resources
路径下的资源时,不会进行filter
处理。
如果配置<filtering>true</filtering>
的情况下,maven就会对src/main/resources
路径下的文件进行处理,找出所有的${xxx}
将其替换为maven变量。
这些变量的来源可以是 项目属性、系统属性、 filter resources、命令行。
- 系统属性:比如
${name}
就是指代项目名称 - 项目属性:即
<properties>
下定义的属性 - filter resources: 使用
<filters>
标签,指定filter resource
路径,如下:my-filter-values.properties - 命令行:通过
mvn resources:resources -Dname="world"
指定属性值
${xx}
字符串转义过滤
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<escapeString>\</escapeString>
</configuration>
</plugin>
此时,配置文件中的 \${name}
就不会呗Filter处理
二进制文件问题
禁止将二进制放入到
<filtering>true</filtering>
的resources
中,这可能导致二进制数据损坏
或者通过以下几种方式对二进制文件进行排除
单独建立文件夹,用来存放需要进行filter处理的资源:
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources-filtered</directory>
<filtering>true</filtering>
</resource>
</resources>
使用exclude剔除二进制资源:
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>src/main/resources/webapp/**.*</exclude>
</excludes>
</resource>
使用插件的nonFilteredFileExtensions属性:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
...
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
<nonFilteredFileExtension>swf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
...
</configuration>
</plugin>
自定义Filter
maven-compile-plugin
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>lib</extdirs><!--指定外部lib-->
</compilerArguments>
</configuration>
</plugin>
多模块项目打包
打包所有模块:
mvn clean install package
打包指定的模块:
mvn clean install -pl web/eweb,web/mweb
打包指定的模块,,并同时打包目标包依赖的模块:
mvn clean install -pl web/eweb -am
打包指定的模块,并同时打包目标包依赖模块 以及 依赖目标包的模块:
mvn clean install -pl web/eweb -am -amd
常用的settings.xml配置
阿里云仓库
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>D:/alirepository</localRepository>
<mirrors>
<!-- 阿里云仓库 -->
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
<!-- 中央仓库1 -->
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo1.maven.org/maven2/</url>
</mirror>
<!-- 中央仓库2 -->
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
<!-- 阿里云的maven路径, -->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
</settings>