温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Maven、Jenkins实现自动化部署

发布时间:2020-06-14 21:16:25 来源:网络 阅读:3399 作者:巧克力黒 栏目:系统运维

工程实例代码:https://github.com/chocolateBlack/multi_env_folder_maven

利用Maven、Jenkins实现项目自动化部署,能够按照bin、conf、lib文件进行打包,并能够通过Jenkins按照环境一键发不到服务器

  1. 首先通过Maven插件实现按照不同环境,生成配置文件
    为了应对环境切换,在配置文件conf.yaml中使用Maven属性,在项目打包过程制定是按照id为dev的profile进行构建,还是以id为test的profile进行构建。其中conf.yaml和profle的配置如下:
    conf.yaml:
    path=${hdfs.path}

profile的配置如下:

<profiles>
<profile>
    <id>dev</id>
    <properties>
            <active.profile>dev</active.profile>
            <!-- 业务配置信息 -->
            <hdfs.path>/dev/wh/source/tp</hdfs.path>
    </properties>
    <activation>
            <activeByDefault>true</activeByDefault>
    </activation>
</profile>
<profile>
    <id>test</id>
    <properties>
            <active.profile>dev</active.profile>
            <!-- 业务配置信息 -->
            <hdfs.path>/test/wh/source/tp</hdfs.path>
    </properties>
</profile>
</profiles>

接下来就需要让maven能够解析conf文件夹中Maven属性,这就是maven-resources-plugin要做的事情。
开启资源过滤,并指定过滤的目录。以下配置指定src/main/conf目录下所有类型的文件开启资源过滤,并将解析后的文件指定输出到target/class/conf目录下

<resources>
        <resource>
                <directory>src/main/conf/</directory>
                <filtering>true</filtering>
                <includes>
                        <include>**/*.*</include>
                </includes>
                <targetPath>conf</targetPath><!-- 最终打包的目录是target/class/conf,用户assembly插件从这个目录中读取编译好的配置文件 -->
        </resource>
</resources>

2.其次,利用maven-assembly-plugin插件,将编译好的项目分成bin、conf、lib目录。

  • maven-assembly-plugin的配置中,指定一个主类这样可以使用java -jar方式或者java -cp的方式运行该项目jar包;
  • 以jar-with-dependencies的方式,将项目依赖的jar包一并打包到一个jar中
  • 指定assembly.xml文件,主要用于编译好的文件分发到bin、conf、lib目录中
    maven-assembly-plugin插件的配置:
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.6</version>
        <executions><execution><!-- 配置执行器 -->
                <id>make-assembly</id>
                <phase>package</phase><!-- 绑定到package生命周期阶段上 -->
                <goals>
                        <goal>single</goal><!-- 只运行一次 -->
                </goals>
                <configuration>
                        <archive>
                                <manifest>
                                        <!-- 此处指定main方法入口的class -->
                                        <mainClass>com.test.HelloMaven</mainClass>
                                </manifest>
                        </archive>
                        <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <finalName>${project.name}</finalName>
                        <descriptor>src/main/assembly/assembly.xml</descriptor><!--配置描述文件路径-->
                </configuration>
        </execution>
        </executions>
    </plugin>

关于文件分发的assembly.xml配置:

<fileSets>
        <!-- 将src/main/bin下的文件,打包到目标目录的bin文件夹下 -->
        <fileSet>
                <directory>src/main/bin</directory>
                <outputDirectory>bin</outputDirectory>
                <fileMode>755</fileMode>
        </fileSet>
        <!-- 经过maven-resources-plugin插件生成的配置文件位于target/classes/conf/中 -->
        <fileSet>
                <directory>target/classes/conf/</directory>
                <outputDirectory>conf</outputDirectory>
                <fileMode>755</fileMode>
                <lineEnding>unix</lineEnding>
                <excludes>
                        <exclude>*.formatted</exclude>
                </excludes>
        </fileSet>
</fileSets>
<dependencySets>
        <!-- 工程单独的jar,分发到lib目录中 -->
        <dependencySet>
                <fileMode>755</fileMode>
                <outputFileNameMapping>${project.artifactId}-${project.version}.jar</outputFileNameMapping>
                <outputDirectory>/lib/</outputDirectory>
                <scope>runtime</scope>
                <includes>
                        <include>${project.groupId}:${project.artifactId}</include>
                </includes>
        </dependencySet>
        <!-- jar-with-dependencies方式,包含依赖jar的大包,放到lib目录下 -->
        <dependencySet>
                <fileMode>755</fileMode>
                <outputFileNameMapping>${project.name}-jar-with-dependencies.jar</outputFileNameMapping>
                <outputDirectory>/lib/</outputDirectory>
                <scope>runtime</scope>
        </dependencySet>
</dependencySets>

项目结构图:
Maven、Jenkins实现自动化部署

通过mvn package -Pdev命令打包后conf中的配置文件是按照dev环境生成的结果
解析后的conf.yaml
path=/dev/wh/source/tp
target目录下的结构图,红框标记的就是期待的结构目录
Maven、Jenkins实现自动化部署

3.项目按bin、conf、lib生成部署结构,剩下的事情就交给Jenkins,通过Jenkins以及其插件,将打包结果上传目标服务器
如下主要描述Jenkins配置的主要几个位置

  • Build标签下,要在package命令中指令 -P参数
    Maven、Jenkins实现自动化部署
  • 构建后的操作中,要将编译后的bin、conf、lib目录,通过插件Send build artifact over SSH插件上传到服务器目标目录中
    Maven、Jenkins实现自动化部署
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI