背景
成熟的ci工具都是将源码编译成jar/war包。由于开发人员在hotfix时实际对代码的改动量不大,甚至是1行代码,但是在大仓的场景下,整个构建时间会超过3分钟。因此Maven Build Cache Extension是一个非常好用的构建缓存插件。
需求
- host nexus3的row格式的host制品库:每次构建会先将代码与nexus的archive进行比对,无增量构建就直接拿archive的缓存数据,有增量构建,那么就执行对应模块的构建任务
优点
- 增量构建只针对修改代码的部分
- 避免重复的代码生成工作
操作步骤
1.在根目录新增.mvn文件夹,文件夹下存放extensions.xml(1.1.0以前的版本会存在target下没有产物的bug)和maven-build-cache-config.xml。
- maven-build-cache-config.xml中的<remote>的id需要与maven settings中配的servers一致
- maven-build-cache-config.xml中的<url>替换成自己的nexus3地址
<?xml version="1.0" encoding="UTF-8"?>
<extensions>
<extension>
<groupId>org.apache.maven.extensions</groupId>
<artifactId>maven-build-cache-extension</artifactId>
<version>1.2.0</version>
</extension>
</extensions>
<?xml version="1.0" encoding="UTF-8"?>
<cache xmlns="http://maven.apache.org/BUILD-CACHE-CONFIG/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/BUILD-CACHE-CONFIG/1.0.0 https://maven.apache.org/xsd/build-cache-config-1.0.0.xsd">
<configuration>
<enabled>true</enabled>
<remote enabled="true" id="remote-cache" saveToRemote="true">
<url>https://${younexus3 domain}/repository/maven-build-cache</url>
</remote>
</configuration>
<input>
<global>
<!-- If not defined, default glob is "*" -->
<glob> {*.java,*.groovy,*.yaml,*.svcd,*.proto,*assembly.xml,assembly*.xml,*logback.xml,*.vm,*.ini,*.jks,*.properties,*.sh,*.bat} </glob>
<includes>
<!-- By default, project sources and resources directories are included (src/main/java and src/main/resources) -->
<!-- In this example, the goal is to include a wider range of src directories (like src/main/assembly or src/main/groovy) -->
<include>src/</include>
<include>target/generated-sources/protobuf/java/</include>
</includes>
<excludes>
<!-- We don't want a static "hash" pom resolution (it would conflict the will to adjust the version in the manifest), -->
<!-- we exclude this specific file (as it is already by default since it is not in an include folder -->
<!-- The need to rebuild a project based on the pom is already computed with some intelligence by the extension. -->
<exclude>pom.xml</exclude>
<!-- Also excluding everything located in this project specific folder -->
<exclude>src/main/javagen/**</exclude>
</excludes>
</global>
</input>
</cache>
2.配置~/.m2/settings.xml
<settings>
<!-- other configs -->
<servers>
<server>
<id>remote-cache</id>
<username>nexus3-username</username>
<password>nexus3-password</password>
</server>
</servers>
</settings>
3.在ci工具执行mvn install后面加参数
mvn clean install -Dmaven.build.cache.remote.save.enabled=true
结论
在命中缓存的情况下,基本上几秒钟完成一个模块的构建。
参考文档: