美文网首页程序员小天地程序员技术干货
利用Jenkins与profile管理自动集成中的配置文件

利用Jenkins与profile管理自动集成中的配置文件

作者: 小草莓子桑 | 来源:发表于2017-06-19 12:55 被阅读0次

    当利用Jenkins进行自动集成的时候,有时候会出现一个比较麻烦的问题,就是配置文件的书写,当配置文件涉及到ip、端口等的时候,服务器环境不一样,值可能也会变,比如说配置文件中一个接口的地址,在自己的开发环境可能是一个外网地址,但是,到了部署的服务器上,很可能会变成一个内网地址,这样在提交代码,构建的时候,可能会出现忘了修改配置文件的情况,为了避免这种情况,可能每个人都有自己的解决方法,在这我介绍一下我使用的一种解决方法,利用maven的profile去管理配置文件。

    在这里Jenkins就不做过多的概述,它是一个自动集成的工具。本文直接说说利用Jenkins与profile管理自动集成中的配置文件。

    1.profile配置

    在maven中可以使用profiles可以配置出各个环境的profile,通过激活这些profile来获取各个环境的构建信息,在项目的pom.xml文件中配置profiles:

        <!-- profiles属性,一般写在pom.xml文件的上面的部分,方便查看 -->
        <profiles>
            <!-- 配置开发环境dev profile-->
            <profile>
                <id>dev</id>
                <properties>
                    <!-- dev profile对应的环境版本为dev-->
                    <env>dev</env>
                </properties>
                <!-- 设置默认激活,也就是说项目的默认环境为开发环境 dev profile-->
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
            </profile>
    
            <!-- 测试环境 test profile -->
            <profile>
                <id>test</id>
                <properties>
                    <!-- test profile对应的环境版本为test-->
                    <env>test</env>
                </properties>
            </profile>
    
            <!--生产环境 release profile-->
            <profile>
                <id>release</id>
                <properties>
                    <!-- release profile对应的环境版本为release-->
                    <env>release</env>
                </properties>
            </profile>
        </profiles>
    

    同时在项目中创建配置文件文件夹deployEnv,目录结构如下:

    项目的目录结构

    把配置文件文件夹deployEnv放到和src文件夹同级,在下面创建三个环境对应的文件夹,要以环境版本做为文件夹名称,在各个环境的文件夹下面书写各自所需的配置文件。

    2.pom.xml中resource属性配置

    需要在pom.xml文件中的build属性,加上资源文件拷贝的配置

     <!-- 为了方便读者定位,把整段的build属性粘过来了-->
     <build>
            <finalName>****</finalName>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>true</filtering>
                </resource>
                <!-- 在resources中加上这个resource属性-->
                <resource>
                    <!-- 拷贝资源的原始路径,为deployEnv文件夹下面的-->
                    <!-- ${env}为当前激活的profile对应的环境版本-->
                    <directory>deployEnv/${env}</directory>
                    <targetPath>${project.build.directory}/classes</targetPath>
                    <filtering>true</filtering>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>2.5</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.2</version>
                </plugin>
                <!-- 资源文件拷贝插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.7</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <!-- java编译插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <configuration>
                        <port>9003</port>
                        <path>/</path>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

    配置完毕,在idea开发工具中可以快速设置激活profile,

    在Maven Projects选项卡中选择激活profile

    在本机开发环境中选择激活dev profile,那么在本机maven构建项目的时候,使用的是deployEnv文件夹下dev下的配置文件。

    3.Jenkins配置

    Jenkins相关配置

    在Jenkins中,进入项目的配置,找到Build标签,在Goals and options文本框中输入: clean install -P test,这是maven的命令,作用是,先清理项目,然后构建项目, -P test的意思是使用 test profile 进行项目的构建,这样在构建测试环境的时候,使用的是deployEnv文件夹下test下的配置文件。

    这样的管理配置文件的方式是不是很简单呢?欢迎大家来沟通交流~
    谢谢大家!

    相关文章

      网友评论

        本文标题:利用Jenkins与profile管理自动集成中的配置文件

        本文链接:https://www.haomeiwen.com/subject/hadgqxtx.html