Maven之nexus相关配置及Jar包实现CICD

前言

公司项目使用spring boot微服务,有很多公共依赖,有一个父公共模块,所有子公共模块依赖父模块。以下说明mvn setting.xm和pom.xml中nexus相关配置,及实现通过Jenkins CICD将公共服务打成jar包发布到nexus仓库中,并实现钉钉告警。

Maven相关配置

Maven

Maven setting.xml配置

注意配置中Nexus的用户名和密码,下面的pom文件中distributionManagement里面的snapshot快照库和release发布库的地址根据对应的ID关联起来

 cat /opt/maven3.8.6/conf/settings.xml
 cat /opt/maven3.8.6/conf/settings.xml
<?xml version="1.0" encoding="UTF-8"?>

<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">

    <localRepository>${user.home}/.m2/repository</localRepository>

    <pluginGroups></pluginGroups>

    <proxies></proxies>

    <servers>
        <server>
            <id>maven-release</id>
            <username>admin</username>
            <password>fafdasfasdfsd</password>
        </server>
        <server>
            <id>maven-snapshots</id>
            <username>admin</username>
            <password>fafdasfasdfsd</password>
        </server>
        <server>
            <id>thirdparty</id>
            <username>admin</username>
            <password>fafdasfasdfsd</password>
        </server>
    </servers>

    <mirrors>
        <!-- 阿里云私服-->
        <!--
                     <mirror>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
         -->

        <!-- 复睿智行私服 -->
        <mirror>
            <id>fr_maven</id>
            <name>ride_maven</name>
            <url>http://10.1.51.126:8081/repository/maven-public/</url>
            <mirrorOf>*</mirrorOf>
        </mirror>
    </mirrors>

    <profiles>
        <profile>
            <id>ride_nexus</id>
            <repositories>
                <repository>
                    <id>ride-repo</id>
                    <name>Repository for ride</name>
                    <url>http://10.1.51.126:8081/repository/maven-public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                    <layout>default</layout>
                </repository>
            </repositories>
        </profile>

        <profile>
            <id>JDK-1.8</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
            </activation>
            <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>ride_nexus</activeProfile>
    </activeProfiles>
</settings>

模块pom.xml配置

项目中有个父公共模块,重点在distributionManagement这里。 父项目dependencies中的所有的依赖项都会被子项目继承,
如下所示

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.aproject</groupId>
    <artifactId>company</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <description>
        XX - XXX 依赖管理
    </description>

    <packaging>pom</packaging>

    <properties>
        <vccp.version>0.0.1-SNAPSHOT</vccp.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-boot.version>2.6.6</spring-boot.version>
        <spring-cloud.version>2021.0.1</spring-cloud.version>
        <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
        <alibaba.nacos.version>2.0.4</alibaba.nacos.version>
        <spring-boot-admin.version>2.6.6</spring-boot-admin.version>
        <spring-boot.mybatis>2.2.2</spring-boot.mybatis>
        <swagger.fox.version>3.0.0</swagger.fox.version>
        <swagger.core.version>1.6.2</swagger.core.version>
        <tobato.version>1.27.2</tobato.version>
        <kaptcha.version>2.3.2</kaptcha.version>
        <pagehelper.boot.version>1.4.1</pagehelper.boot.version>
        <druid.version>1.2.8</druid.version>
        <dynamic-ds.version>3.5.0</dynamic-ds.version>
        <commons.io.version>2.11.0</commons.io.version>
        <commons.fileupload.version>1.4</commons.fileupload.version>
        <velocity.version>2.3</velocity.version>
        <fastjson.version>1.2.80</fastjson.version>
        <jjwt.version>0.9.1</jjwt.version>
        <minio.version>7.1.4</minio.version>
        <poi.version>4.1.2</poi.version>
        <commons-collections.version>3.2.2</commons-collections.version>
        <transmittable-thread-local.version>2.12.2</transmittable-thread-local.version>
        <integration.mqtt.version>5.5.14</integration.mqtt.version>
        <integration.core.version>5.5.14</integration.core.version>
        <mqtt.client.version>1.2.5</mqtt.client.version>
        <lombok.version>1.18.22</lombok.version>
        <test-version>111</test-version>
        <tdengin.jdbc.driver.version>2.0.38</tdengin.jdbc.driver.version>
        <disruptor.version>3.4.2</disruptor.version>
        <hutool.version>5.3.0</hutool.version>
    </properties>

    <!-- 依赖声明 -->
    <dependencyManagement>
        <dependencies>

            <!-- SpringCloud 微服务 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- SpringCloud Alibaba 微服务 -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- Alibaba Nacos 配置 -->
            <dependency>
                <groupId>com.alibaba.nacos</groupId>
                <artifactId>nacos-client</artifactId>
                <version>${alibaba.nacos.version}</version>
            </dependency>

            <!-- SpringBoot 依赖配置 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- FastDFS 分布式文件系统 -->
            <dependency>
                <groupId>com.github.tobato</groupId>
                <artifactId>fastdfs-client</artifactId>
                <version>${tobato.version}</version>
            </dependency>

            <!-- Mybatis 依赖配置 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${spring-boot.mybatis}</version>
            </dependency>

            <!-- Swagger 依赖配置 -->
            <dependency>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-models</artifactId>
                <version>${swagger.core.version}</version>
            </dependency>
            <dependency>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-annotations</artifactId>
                <version>${swagger.core.version}</version>
            </dependency>

            <!-- 验证码 -->
            <dependency>
                <groupId>com.github.penggle</groupId>
                <artifactId>kaptcha</artifactId>
                <version>${kaptcha.version}</version>
            </dependency>

            <!-- pagehelper 分页插件 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>${pagehelper.boot.version}</version>
            </dependency>

            <!-- io常用工具类 -->
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>${commons.io.version}</version>
            </dependency>

            <!-- excel工具 -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi.version}</version>
            </dependency>

            <!-- 文件上传工具类 -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>${commons.fileupload.version}</version>
            </dependency>

            <!-- 代码生成使用模板 -->
            <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity-engine-core</artifactId>
                <version>${velocity.version}</version>
            </dependency>

            <!-- Collection 增强Java集合框架 -->
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>${commons-collections.version}</version>
            </dependency>

            <!-- TDengin jdbc驱动-->
            <dependency>
                <groupId>com.taosdata.jdbc</groupId>
                <artifactId>taos-jdbcdriver</artifactId>
                <version>${tdengin.jdbc.driver.version}</version>
            </dependency>

            <!-- JSON 解析器和生成器 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <!-- JWT -->
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>${jjwt.version}</version>
            </dependency>

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>

            <!-- 并发编程依赖 -->
            <dependency>
                <groupId>com.lmax</groupId>
                <artifactId>disruptor</artifactId>
                <version>${disruptor.version}</version>
            </dependency>

            <!-- mqtt -->
            <dependency>
                <groupId>org.springframework.integration</groupId>
                <artifactId>spring-integration-core</artifactId>
                <version>${integration.core.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.integration</groupId>
                <artifactId>spring-integration-mqtt</artifactId>
                <version>${integration.mqtt.version}</version>
            </dependency>

            <dependency>
                <groupId>org.eclipse.paho</groupId>
                <artifactId>org.eclipse.paho.mqttv5.client</artifactId>
                <version>${mqtt.client.version}</version>
            </dependency>

            <!-- 线程传递值 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>transmittable-thread-local</artifactId>
                <version>${transmittable-thread-local.version}</version>
            </dependency>

            <!-- 核心模块 -->
            <dependency>
                <groupId>com.aproject</groupId>
                <artifactId>company-common-core</artifactId>
                <version>${vccp.version}</version>
            </dependency>

            <!-- 接口模块 -->
            <dependency>
                <groupId>com.aproject</groupId>
                <artifactId>company-common-swagger</artifactId>
                <version>${vccp.version}</version>
            </dependency>

            <!-- 安全模块 -->
            <dependency>
                <groupId>com.aproject</groupId>
                <artifactId>company-common-security</artifactId>
                <version>${vccp.version}</version>
            </dependency>

            <!-- 权限范围 -->
            <dependency>
                <groupId>com.aproject</groupId>
                <artifactId>company-common-datascope</artifactId>
                <version>${vccp.version}</version>
            </dependency>

            <!-- 多数据源 -->
            <dependency>
                <groupId>com.aproject</groupId>
                <artifactId>company-common-datasource</artifactId>
                <version>${vccp.version}</version>
            </dependency>

            <!-- 日志记录 -->
            <dependency>
                <groupId>com.aproject</groupId>
                <artifactId>company-common-log</artifactId>
                <version>${vccp.version}</version>
            </dependency>

            <!-- 缓存服务 -->
            <dependency>
                <groupId>com.aproject</groupId>
                <artifactId>company-common-redis</artifactId>
                <version>${vccp.version}</version>
            </dependency>

            <!-- 协议解析类 -->
            <dependency>
                <groupId>com.aproject.asn1</groupId>
                <artifactId>company-common-asn1</artifactId>
                <version>${vccp.version}</version>
            </dependency>

            <!-- 系统接口 -->
            <dependency>
                <groupId>com.aproject</groupId>
                <artifactId>company-api-system</artifactId>
                <version>${vccp.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- bootstrap 启动器 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
    </dependencies>

    <distributionManagement>
        <repository>
            <id>maven-release</id>
            <name>vccp-release</name>
            <url>http://Nexus私网IP:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>maven-snapshots</id>
            <name>vccp-snapshots</name>
            <url>http://Nexus私网IP:8081/repository/maven-snapshot/</url>
        </snapshotRepository>
    </distributionManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
            </plugin>
        </plugins>
    </build>
</project>

子模块配置

其中compa-common-datascope子模块的pom.xml配置案例如下,注意parent配置部分。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>com.aproject</groupId>
        <artifactId>company</artifactId>
        <version>1.0.0</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.aproject</groupId>
    <artifactId>company-common-datascope</artifactId>
    <version>1.0.0</version>

    <description>
        company-common-datascope 权限范围
    </description>

    <dependencies>
        <!-- vccp Common Security -->
        <dependency>
            <groupId>com.aproject</groupId>
            <artifactId>company-common-security</artifactId>
        </dependency>
    </dependencies>
</project>

Jenkins CICD

Pipeline

以上配置完成后就可以通过mvn deploy命令进行发布了,现在配置,通过Jenkins Pipeline来发布上传公共依赖,并钉钉告警,下面用的agent节点名字为agent1,需要在此节点上提前安装好Java、对应版本的mvn环境,打包动作都是在此节点上完成。
效果如下图所示
Jenkins cicddeclarative pipeline配置如下所示

pipeline {
    agent {
       node {
           label 'agent1'
       }
    }
    //agent any
    parameters {
        choice choices: ['cloud', 'common-asn1', 'common-core', 'api-system', 'common-redis', 'common-security', 'common-datascope', 'common-log', 'common-datasource', 'common-swagger'], description: '请选择要发布的微服务', name: 'Microsvc'
        choice choices: ['common'], description: 'common分支', name: 'Branch'
    }
    stages {      
        stage('Git Clone') {
            steps {
                script {
                    sh 'sh backend-scripts/git-util.sh ${Microsvc} ${Branch}'
                    git_commit = sh (script: 'git rev-parse --short HEAD', returnStdout: true).trim()
                    currentBuild.displayName = "${git_commit}"
                    currentBuild.description = "${Microsvc} - ${Branch}"
                }
            }
        }

        stage('Build') {
            steps {
                script {
                    sh 'sh backend-scripts/mvn_deploy_nexus.sh ${Microsvc}' 
                    //projectId = sh (script: 'backend-scripts/repo-util.sh ${orgRepo}', returnStdout: true).trim()
                    //writeFile(file: "backend-scripts/${orgRepo}-${branch}.txt",text: "${projectId}")
                }
            }
        }
 
    }

    post {
        success {
            dingtalk (
                robot: 'Jenkins-Dingtalk',
                type: 'MARKDOWN',
                title: "success: ${JOB_NAME}",
                text: [
                    "### [${env.JOB_NAME}](${env.JOB_URL}) ",
                    '---',
                    "- 任务:[${currentBuild.displayName}](${env.BUILD_URL})",
                    '- 状态:**<font>成功</font>**',
                    "- 持续时间:${currentBuild.durationString}".split("and counting")[0],
                    "- ${currentBuild.buildCauses.shortDescription}",
                ]
            )
        }
    }
}

Nexus仓库效果展示

如下所示:
Nexus

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值