– 来自 http://maven.apache.org/guides/getting-started/index.html
– 来自 http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
一般情况下,maven会原封不动地复制resources,但是如果一些resources依赖构建时提供的值,此时可以引入filtering.
示例:
<project>
<build>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<filtering>truefiltering>
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>
<filters>
<filter>src/main/filters/filter.propertiesfilter>
filters>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<filtering>truefiltering>
resource>
resources>
build>
project>
当然,也可以通过properties标签定义:
<project>
<properties>
<my.filter.value>hellomy.filter.value>
properties>
project>
– 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可选值:
1.在project中设置发布管理并引用仓库id
<project>
<distributionManagement>
<repository>
<id>mycompany-repositoryid>
<name>MyCompany Repositoryname>
<url>scp://repository.mycompany.com/repository/maven2url>
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-repositoryid>
<username>jvanzylusername>
<privateKey>/path/to/identityprivateKey> (default is ~/.ssh/id_dsa)
<passphrase>my_key_passphrasepassphrase>
server>
servers>
...
settings>
基于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)
参考: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>debugclassifier>
dependency>
升级某个依赖到某个版本:
mvn versions:use-dep-version -Dincludes=com.ab:test -DexcludeReactor=false -DdepVersion=2.0