0%

Maven

Maven

  • 插件编写

  • 构建模板

phase and goal

Introduction to the Build Lifecycle

一个lifecycle有多个phase,一个phase可以有多个goal

Mojo

三种内置的lifecycle:default、clean和site

列出所有的plugin、phase、id、goal

1
mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:list
1
2
# 打印插件的所有命令详情
mvn help:describe -Dplugin=javafx -Ddetail
1
2
3
4
5
6
7
8
9
10
11
12
13
mvn clean install -DskipTests -Dfast -Drat.skip=true -Dhaoop.version=2.6.0-cdh5.15.1 -Pvendor-repos -Dinclude-hadoop -Dscala-2.11 -T2C

# -Dfast #在flink根目录下pom.xml文件中fast配置项目中含快速设置,其中包含了多项构建时的跳过参数. #例如apache的文件头(rat)合法校验,代码风格检查,javadoc生成的跳过等,详细可阅读pom.xml
# install maven的安装命令
# -T2C #支持多处理器或者处理器核数参数,加快构建速度,推荐Maven3.3及以上
# -Pinclude-hadoop 将hadoop的 jar包,打入到lib/中
# -Pvendor-repos # 如果需要指定hadoop的发行商,如CDH,需要使用-Pvendor-repos
# -Dscala-2.11 # 指定scala的版本为2.11
# -Dhadoop.version=2.6.0-cdh5.15.1 指定 hadoop 的版本

————————————————
版权声明:本文为CSDN博主「实力不允许偷懒」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_17310871/article/details/106677165
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.5</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<!-- 默认绑定阶段initialize -->
<phase>initialize</phase>
<goals>
<!-- 目标:revision -->
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 检查的仓库根目录,${project.basedir}:项目根目录,即包含pom.xml文件的目录 -->
<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
<!-- false:扫描路径时不打印更多信息,默认值false,可以不配置 -->
<verbose>false</verbose>
<!-- 定义插件中所有时间格式,默认值:yyyy-MM-dd’T’HH:mm:ssZ -->
<dateFormat>yyyy-MM-dd HH:mm:ss</dateFormat>
<!-- git属性文件中各属性前缀,默认值git,可以不配置 -->
<prefix>git</prefix>
<!-- 生成git属性文件,默认false:不生成 -->
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<!-- 生成git属性文件路径及文件名,默认${project.build.outputDirectory}/git.properties -->
<generateGitPropertiesFilename>${project.build.outputDirectory}/conf/git.properties
</generateGitPropertiesFilename>
<!-- 生成git属性文件格式,默认值properties -->
<format>json</format>
<!-- 配置git-describe命令 -->
<gitDescribe>
<skip>false</skip>
<always>false</always>
<dirty>-dirty</dirty>
</gitDescribe>
</configuration>
</plugin>

shade

  1. 打包文件增加时间戳,并指定时间戳格式
  2. 排除代码路径与排除文件路径, 排除整个目录需要增加**
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<maven.build.timestamp.format>yyyyMMdd-HHmmss</maven.build.timestamp.format>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>shade</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>

<configuration>
<finalName>
${project.artifactId}-${project.version}-${maven.build.timestamp}-shaded
</finalName>
<!-- <shadeTestJar>true</shadeTestJar>-->
<!-- <shadedArtifactAttached>false</shadedArtifactAttached>-->
<createDependencyReducedPom>true</createDependencyReducedPom>
<transformers combine.children="append">
<!-- The service transformer is needed to merge META-INF/services files -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />

</transformers>
<minimizeJar>true</minimizeJar>
<shadeTestJar>false</shadeTestJar>
<shadedArtifactAttached>false</shadedArtifactAttached>
<createDependencyReducedPom>false</createDependencyReducedPom>
<!-- <dependencyReducedPomLocation>-->
<!-- ${project.basedir}/target/dependency-reduced-pom.xml-->
<!-- </dependencyReducedPomLocation>-->

<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>log4j.properties</exclude>
<exclude>version-info.properties</exclude>
<exclude>org/slf4j/**</exclude>
<exclude>flake/**</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>org/apache/commons/**</exclude>
</excludes>
</filter>
</filters>
<relocations>
<relocation>
<pattern>com.tencent.oceanus.common</pattern>
<shadedPattern>shaded.flake.com.tencent.oceanus.common
</shadedPattern>
</relocation>
<relocation>
<pattern>com.tencent.oceanus.util</pattern>
<shadedPattern>shaded.flake.com.tencent.oceanus.util
</shadedPattern>
</relocation>
<relocation>
<pattern>com.tencent.oceanus.exception</pattern>
<shadedPattern>shaded.flake.com.tencent.oceanus.exception
</shadedPattern>
</relocation>
<relocation>
<pattern>com.tencent.oceanus.dto</pattern>
<shadedPattern>shaded.flake.com.tencent.oceanus.dto
</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
<configuration>
<!-- <createSourcesJar>true</createSourcesJar>-->
</configuration>
</plugin>

发布test jar

对那些有着良好设计,能够重复使用在项目的不同模块中、甚至不同项目中的测试代码,也需要打包成构件重复使用,从而减少编写测试代码的工作量。而 mvn package 只会对主代码和资源文件进行打包安装与部署,对测试代码和资源文件是不会处理的

发布

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

使用

maven-jar-plugin 有两个目标:一个是 jar;另一个是 test-jar。
jar 目标有内置绑定到 Maven 的 default 生命周期的 package 阶段,会在 Maven 工程进行构建的时候自动执行,将项目的主代码和资源文件进行打包。

test-jar 目标没有内置绑定,所以需要用户在插件配置中声明该目标,从而达到在 Maven 工程构建的时候将测试代码和资源文件打包。
test-jar 目标是默认绑定到 default 生命周期的 package 阶段,
所以当运行 mvn clean package 命令的时候,能同时将主代码和测试代码分别打包。

1
2
3
4
5
6
7
8
<dependency>
<groupId>com.dalong</groupId>
<artifactId>testpacakge</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>tests</classifier>
<type>test-jar</type>
<scope>test</scope>
</dependency>

module-dependency-tree

https://github.com/ferstl/depgraph-maven-plugin
https://ferstl.github.io/depgraph-maven-plugin/plugin-info.html

https://ferstl.github.io/depgraph-maven-plugin/graph-mojo.html#graphFormat

1
2
3
4
mvn com.github.ferstl:depgraph-maven-plugin:3.0.1:aggregate \
-DcreateImage=true \
-DreduceEdges=false \
-Dscope=compile \

archetype

resources-filtered

在 Maven 项目中,resources-filtered 是一个配置选项,用于指示 Maven 是否应该对资源文件(如 *.properties 或 *.xml 文件)中的变量进行过滤和替换

资源过滤的作用

资源过滤的主要目的是允许开发者在构建过程中动态地替换资源文件中的占位符(例如 ${project.version})为实际的值。这在以下场景中非常有用:

  1. 版本管理:可以在资源文件中使用 ${project.version} 占位符,Maven 会在构建时自动将其替换为项目的实际版本号。
  2. 环境特定配置:可以为不同的部署环境(如开发、测试、生产)创建不同的配置文件,并在构建时选择合适的文件进行替换。
  3. 避免硬编码:通过使用占位符而不是硬编码的值,可以使项目更加灵活和可维护。

如何启用资源过滤

在 Maven 的 pom.xml 文件中,可以通过以下方式启用资源过滤:

1
2
3
4
5
6
7
8
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

在这个例子中,<filtering> 元素被设置为 true,表示 Maven 应该对 src/main/resources 目录下的所有资源文件进行过滤。

注意事项

  • 资源过滤可能会导致构建过程中的性能开销,特别是在处理大量资源文件时。
  • 如果资源文件中的某些内容不应该被过滤(例如,包含敏感信息的密码字段),可以使用 Maven 的 delimiters 配置来指定自定义的分隔符,以避免不必要的替换。

样例

可以替换的属性,包含maven properties和git属性

1
2
3
4
5
6
7
project.version=${project.version}  
scala.binary.version=${scala.binary.version}

git.commit.id=${git.commit.id}
git.commit.id.abbrev=${git.commit.id.abbrev}
git.commit.time=${git.commit.time}
git.build.time=${git.build.time}