GitLab CI发布页获取构建已部署/安装构件列表方法咨询
在GitLab CI中获取Maven构建的已部署构件列表
方法1:借助Maven Deploy插件生成构件清单
在项目的pom.xml中配置maven-deploy-plugin,开启部署清单生成功能:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>3.1.1</version> <configuration> <generateDeployManifest>true</generateDeployManifest> <!-- 指定清单输出路径,默认在target/deploy-manifest.properties --> <deployManifestFile>${project.build.directory}/deployed-artifacts.properties</deployManifestFile> </configuration> </plugin> </plugins> </build>
执行mvn clean deploy后,Maven会在指定路径生成包含构件信息的properties文件,示例内容:
groupId=com.example artifactId=demo-project version=1.0.0-SNAPSHOT type=jar file=demo-project-1.0.0-SNAPSHOT.jar url=http://your-repo-url/snapshots/
多模块项目中,每个子模块都会生成对应清单,可统一收集到指定目录。
方法2:自定义脚本生成多模块完整构件列表
如果需要汇总所有子模块的构件信息,可借助gmavenplus-plugin执行Groovy脚本:
<plugin> <groupId>org.codehaus.gmavenplus</groupId> <artifactId>gmavenplus-plugin</artifactId> <version>1.13.1</version> <executions> <execution> <phase>deploy</phase> <goals> <goal>execute</goal> </goals> <configuration> <scripts> <script><![CDATA[ def outputFile = new File(project.build.directory, 'all-deployed-artifacts.txt') def writer = outputFile.newWriter() // 遍历主项目及所有子模块 def projects = [project] + project.modules.collect { project.getChildProject(it) } projects.each { p -> writer.writeLine("${p.groupId}:${p.artifactId}:${p.version}:${p.packaging}") // 附加构件(如sources、javadoc)也一并输出 p.attachedArtifacts.each { artifact -> writer.writeLine("${p.groupId}:${p.artifactId}:${p.version}:${artifact.type}:${artifact.classifier}") } } writer.close() ]]></script> </scripts> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>3.0.17</version> <type>pom</type> <scope>runtime</scope> </dependency> </dependencies> </plugin>
执行mvn clean deploy后,target/all-deployed-artifacts.txt会生成所有已部署构件的清晰列表。
GitLab CI中读取清单的配置示例
在.gitlab-ci.yml中添加步骤读取并保存清单文件,方便在发布页面查看:
deploy_job: stage: deploy script: - mvn clean deploy # 打印构件清单到CI日志 - cat target/deployed-artifacts.properties # 收集多模块清单到统一文件 - find . -name "deployed-artifacts.properties" -exec cat {} \; > all-deployed.txt artifacts: paths: - target/deployed-artifacts.properties - all-deployed.txt expire_in: 30d
内容的提问来源于stack exchange,提问作者JF Meier
相关产品推荐
相关产品推荐

