多模塊maven的deploy集成gitlab?ci自動發(fā)版配置
背景
多模塊的 maven 項目,抽象了通用的代碼邏輯作為單獨的 maven 模塊,這樣,不僅自己項目可以用,也可以提供依賴給其他項目用,那么這個時候需要將這個模塊上傳到 maven 私服,發(fā)布 maven 私服時,release 版本不支持覆蓋,所以需要集成 ci 工具,給 maven 模塊自動加上版本號,并自動完成 deploy 操作。本文方案依賴 maven 打包插件 flatten-maven-plugin,maven 版本要求大于等于 3.5.0
maven 配置
1、將 root 模塊的 version 變量化,如新增如下的版本號 properties 參數
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.kl</groupId>
<artifactId>demo</artifactId>
<packaging>pom</packaging>
<version>${revision}</version>
<name>${project.artifactId}</name>
<modules>
<module>core</module>
<module>common</module>
<module>admin</module>
</modules>
<properties>
<revision>1.0</revision>
</properties>2、子模塊依賴父模塊,依然使用 {revision} 占位符代替,依賴子模塊使用revision占位符代替,依賴子模塊使用{project.version},如:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.kl</groupId>
<artifactId>demo</artifactId>
<version>${revision}</version>
</parent>
<artifactId>core</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<dependencies>
<dependency>
<groupId>com.github.kl</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>3、不納入自動版本號的模塊,指定 version,同時,這種模塊也不需要 deploy 到私服,配置提過,如:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.kl</groupId>
<artifactId>demo</artifactId>
<version>1.0</version>
</parent>
<artifactId>admin</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
<dependencies>
<dependency>
<groupId>com.github.kl</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>4、root 模塊添加支持外部傳入版本號參數的構建插件 flatten-maven-plugin,添加私服倉庫地址
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>1.1.0</version>
<configuration>
<updatePomFile>true</updatePomFile>
<flattenMode>resolveCiFriendliesOnly</flattenMode>
</configuration>
<executions>
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>repo</id>
<url>https://nexus.dev.com/repository/maven-releases/</url>
</repository>
</distributionManagement>完成如上步驟后,deploy 時,就可以通過傳入系統(tǒng)參數的方式,動態(tài)指定版本號,如:mvn deploy -Drevision=xxx
gitlab ci 配置
1、在項目根目錄創(chuàng)建文件 .ci/settings.xml , 內容如下:
<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>repo</id>
<username>${env.NEXUS_REPO_USERNAME}</username>
<password>${env.NEXUS_REPO_PASSWORD}</password>
</server>
</servers>
</settings>這個文件配置了 repo 的 maven 私服倉庫 server,因為這個配置跟隨項目的 git 走的,為了防止用戶名和密碼泄露,從環(huán)境變量中獲取(提前在 gitlab 里配置好)
2、在.gitlab-ci.yml 中新增 build-deploy 流程
build-deploy:
stage: build-deploy
image: maven:3.6.3-openjdk-8-slim
only:
- master
- dev
variables:
MAVEN_OPTS: "-Xmx512m -Xms512m -Dmaven.repo.local=$CI_PROJECT_DIR/repository"
script:
- mvn -s .ci/settings.xml --batch-mode clean deploy -Drevision=1.0-${CI_PIPELINE_IID}
artifacts:
paths:
- admin/target
expire_in: 1 week
cache:
paths:
- repository以上就是多模塊maven的deploy集成gitlab ci自動發(fā)版配置的詳細內容,更多關于maven deploy集成gitlab ci配置的資料請關注腳本之家其它相關文章!
相關文章
解決Maven本地倉庫明明有對應的jar包但還是報找不到的問題
這篇文章主要介紹了解決Maven本地倉庫明明有對應的jar包但還是報找不到的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
Spring Cloud Nacos 和 Eureka區(qū)別解析
Spring Cloud Nacos 和 Spring Cloud Eureka 都是 Spring Cloud 微服務框架中的服務注冊和發(fā)現組件,用于幫助開發(fā)者輕松地構建和管理微服務應用,這篇文章主要介紹了Spring Cloud Nacos 和 Eureka區(qū)別,需要的朋友可以參考下2023-08-08
java中extends與implements的區(qū)別淺談
java中extends與implements的區(qū)別淺談,需要的朋友可以參考一下2013-03-03

