多模块的 maven 项目,抽象了通用的代码逻辑作为单独的 maven 模块,这样,不仅自己项目可以用,也可以提供依赖给其他项目用,那么这个时候需要将这个模块上传到 maven 私服,发布 maven 私服时,release 版本不支持覆盖,所以需要集成 ci 工具,给 maven 模块自动加上版本号,并自动完成 deploy 操作。本文方案依赖 maven 打包插件 flatten-maven-plugin,maven 版本要求大于等于 3.5.0
maven 配置1、将 root 模块的 version 变量化,如新增如下的版本号 properties 参数
<modelVersion>4.0.0modelVersion><groupId>com.github.klgroupId><artifactId>demoartifactId><packaging>pompackaging><version>${revision}version> <name>${project.artifactId}name> <modules> <module>coremodule> <module>commonmodule> <module>adminmodule>modules> <properties> <revision>1.0revision>properties>
2、子模块依赖父模块,依然使用 {revision} 占位符代替,依赖子模块使用revision占位符代替,依赖子模块使用{project.version},如:
<modelVersion>4.0.0modelVersion><parent> <groupId>com.github.klgroupId> <artifactId>demoartifactId> <version>${revision}version>parent><artifactId>coreartifactId><packaging>jarpackaging><name>${project.artifactId}name><dependencies> <dependency> <groupId>com.github.klgroupId> <artifactId>commonartifactId> <version>${project.version}version> dependency>dependencies>
3、不纳入自动版本号的模块,指定 version,同时,这种模块也不需要 deploy 到私服,配置跳过,如:
<modelVersion>4.0.0modelVersion> <parent> <groupId>com.github.klgroupId> <artifactId>demoartifactId> <version>1.0version> parent> <artifactId>adminartifactId> <packaging>jarpackaging> <name>${project.artifactId}name> <properties> <maven.deploy.skip>truemaven.deploy.skip> properties> <dependencies> <dependency> <groupId>com.github.klgroupId> <artifactId>commonartifactId> <version>${project.version}version> dependency> dependencies>
4、root 模块添加支持外部传入版本号参数的构建插件 flatten-maven-plugin,添加私服仓库地址
<build> <plugins> <plugin> <groupId>org.codehaus.mojogroupId> <artifactId>flatten-maven-pluginartifactId> <version>1.1.0version> <configuration> <updatePomFile>trueupdatePomFile> <flattenMode>resolveCiFriendliesOnlyflattenMode> configuration> <executions> <execution> <id>flattenid> <phase>process-resourcesphase> <goals> <goal>flattengoal> goals> execution> <execution> <id>flatten.cleanid> <phase>cleanphase> <goals> <goal>cleangoal> goals> execution> executions> plugin> plugins>build><distributionManagement> <repository> <id>repoid> <url>https://nexus.dev.com/repository/maven-releases/url> repository>distributionManagement>
完成如上步骤后,deploy 时,就可以通过传入系统参数的方式,动态指定版本号,如:mvn deploy -Drevision=xxx
gitlab ci 配置1、在项目根目录创建文件 .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>repoid> <username>${env.NEXUS_REPO_USERNAME}username> <password>${env.NEXUS_REPO_PASSWORD}password> server> servers>settings>
这个文件配置了 repo 的 maven 私服仓库 server,因为这个配置跟随项目的 git 走的,为了防止用户名和密码泄露,从环境变量中获取(提前在 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