Maven进阶知识
1. ${prop…} 可以通过XML的路径引用,比如 ${project.build.finalName}
– 来自 http://maven.apache.org/guides/getting-started/index.html
2. maven的标准目录包括: src/main/java, src/main/resources,还有 src/main/filters
– 来自 http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
一般情况下,maven会原封不动地复制resources,但是如果一些resources依赖构建时提供的值,此时可以引入filtering.
示例:
<project>
<!--....-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
src/main/resources/META-INF/application.properties文件,如下引用生成文件。
# application.properties
application.name=${project.name}
application.version=${project.version}
通过调用mvn process-resources
可以将上述文件生成到target/classes/META-INF/application.properties:
# application.properties
application.name=Maven Quick Start Archetype
application.version=1.0-SNAPSHOT
为了引入外部的属性,可以在src/main/filters/filter.properties
中定义属性:
# filter.properties
my.filter.value=hello!
然后引用:
<project>
<!--....-->
<build>
<!--注: build.filters标签用于引入properties文件-->
<filters>
<filter>src/main/filters/filter.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
当然,也可以通过properties标签定义:
<project>
<!--....-->
<properties>
<my.filter.value>hello</my.filter.value>
</properties>
</project>
3.maven model
– model来自:http://maven.apache.org/ref/3.6.3/maven-model/maven.htm
– 构建品来自:http://maven.apache.org/ref/3.6.3/maven-core/artifact-handlers.html
– 依赖描述来自:https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
每一个XML标签实际上都对应一个java对象的域以及类型。
maven的依赖scope可选值:
- compile:默认, 1.所有运行情况,编译和运行,添加到classpath 2.传递依赖
- provided:仅编译和测试时添加到classpath,运行时假定容器已经提供, 不传递依赖。例如servlet api
- runtime:仅运行时需要,编译时不需要,并且假定容器已经提供;非传递性依赖
- test:仅测试阶段的编译和运行需要,非传递性依赖
- system:如同provided,但编译、测试和运行时都会假定已经添加了该jar包,它不会在maven仓库中进行查找;比如tomcat的依赖包
- import:仅用于dependencyManagement中的pom类型引入,它会用目标pom中的dependencyManagement加入当前pom的依赖管理
注:运行时包括测试和部署
4.maven上传仓库配置
1.在project中设置发布管理并引用仓库id
<project>
<!---->
<distributionManagement>
<repository>
<id>mycompany-repository</id>
<name>MyCompany Repository</name>
<url>scp://repository.mycompany.com/repository/maven2</url>
</repository>
</distributionManagement>
</project>
2.在settings.xml中设置仓库相关的认证,比如ssh认证:
<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">
...
<servers>
<server>
<id>mycompany-repository</id>
<username>jvanzyl</username>
<!-- Default value is ~/.ssh/id_dsa -->
<privateKey>/path/to/identity</privateKey> (default is ~/.ssh/id_dsa)
<passphrase>my_key_passphrase</passphrase>
</server>
</servers>
...
</settings>
5.基于maven的其他命令
基于pom获取所有的classpath
dependency:build-classpath https://maven.apache.org/plugins/maven-dependency-plugin/build-classpath-mojo.html
基于pom获取所有的源代码
source:jar-no-fork
source:test-jar-no-for
指定:outputDirectory属性输出jar文件目录, finalName输出jar文件名称
为了获取最后的文件列表,读取jar包即可。
当然,更加优雅的方式是开发一个maven插件来做这件事情。下面的代码就是核心
(FileSet)((DefaultFileSet)((DefaultFileSet)DefaultFileSet.fileSet(directory).prefixed("")).includeExclude(includes, excludes)).includeEmptyDirs(this.includeEmptyDirs)
6.classifier
参考:https://maven.apache.org/plugins/maven-deploy-plugin/examples/deploying-with-classifiers.html
maven打包的文件可以包含不同的特性包,这些特性用classifier区分。从实际效果来说,就是打包文件分为main和其他不同的classifier类型,比如xyz.jar和xyz-debug.jar, 后者的classifier是debug。
在maven依赖引入时指定classifier即可
<dependency>
...
<classifier>debug</classifier>
</dependency>
7.外部版本管理
升级某个依赖到某个版本:
mvn versions:use-dep-version -Dincludes=com.ab:test -DexcludeReactor=false -DdepVersion=2.0