美文网首页
Jenkins构建maven

Jenkins构建maven

作者: 我已不是少年郎 | 来源:发表于2019-02-28 15:26 被阅读0次

    Jenkins安装
    Jenkins构建android
    Jenkins构建maven
    Jenkins构建nodejs应用发布到nginx
    Jenkins权限管理

    1、环境

    2、目标

    需要达成以下目标:

    • release和debug版本分别替换不同的参数
    • 从SVN拉取代码编译release版后直接发布到tomcat服务器

    3、本地工程改造

    主要修改pom.xml

    <?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.anfa</groupId>
        <artifactId>AnfaSmart</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.7</maven.compiler.source>
            <maven.compiler.target>1.7</maven.compiler.target>
    
            <!-- spring版本号 -->
            <spring.version>4.3.9.RELEASE</spring.version>
            <!-- log4j日志文件管理包版本 -->
            <slf4j.version>1.6.6</slf4j.version>
            <log4j.version>1.2.17</log4j.version>
            <!-- junit版本号 -->
            <junit.version>4.12</junit.version>
            <!-- mybatis版本号 -->
            <mybatis.version>3.4.6</mybatis.version>
            <!-- mybatis spring版本号 -->
            <mybatis.spring.version>1.3.2</mybatis.spring.version>
            <!-- mysql驱动 5.1.38 -->
            <mysql.connect>5.1.38</mysql.connect>
            <!--<mysql.connect>8.0.11</mysql.connect>-->
            <!-- redis -->
            <redis.version>2.8.0</redis.version>
            <jackson-core.version>2.7.2</jackson-core.version>
        </properties>
    
        <dependencies>
            <!-- jpush  -->
            <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty-all</artifactId>
                <version>4.1.6.Final</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>cn.jpush.api</groupId>
                <artifactId>jpush-client</artifactId>
                <version>3.2.7</version>
            </dependency>
            <!-- jpush  -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${jackson-core.version}</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>${jackson-core.version}</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>${jackson-core.version}</version>
            </dependency>
    
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.8.5</version>
            </dependency>
    
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3</version>
            </dependency>
    
            <!-- redis cache related.....start -->
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
                <version>1.6.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>${redis.version}</version>
            </dependency>
            <!-- redis cache related.....end -->
    
            <!-- 添加Spring依赖 -->
            <!-- Spring MVC -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
            <!-- 数据库相关 Start -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
            <!-- mysql驱动包 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.connect}</version>
            </dependency>
            <!-- 数据库连接池 -->
            <dependency>
                <groupId>commons-dbcp</groupId>
                <artifactId>commons-dbcp</artifactId>
                <version>1.4</version>
            </dependency>
            <!-- 数据库相关 End -->
    
            <!-- mybatis/spring包 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>${mybatis.spring.version}</version>
            </dependency>
    
            <!--mybatis核心 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
    
            <!-- Base Start -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
            <!--单元测试依赖 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <!-- Base End -->
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
    
            <!--spring单元测试依赖 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
                <scope>test</scope>
            </dependency>
    
    
            <!-- javaee-api包 注意和项目使用的JDK版本对应 -->
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>8.0</version>
                <scope>provided</scope>
            </dependency>
    
            <!-- javaee-web-api包 注意和项目使用的JDK版本对应 -->
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-web-api</artifactId>
                <version>8.0</version>
                <scope>provided</scope>
            </dependency>
    
            <dependency>
                <groupId>com.glimmer.lucien.connect</groupId>
                <artifactId>connect</artifactId>
                <version>1.0.0</version>
            </dependency>
        </dependencies>
    
        <build>
            <finalName>anfa</finalName>
            <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
                <plugins>
                    <plugin>
                        <artifactId>maven-clean-plugin</artifactId>
                        <version>3.0.0</version>
                    </plugin>
                    <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                    <plugin>
                        <artifactId>maven-resources-plugin</artifactId>
                        <version>3.0.2</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.7.0</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.20.1</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>3.2.0</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-install-plugin</artifactId>
                        <version>2.5.2</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-deploy-plugin</artifactId>
                        <version>2.8.2</version>
                    </plugin>
                </plugins>
            </pluginManagement>
            <resources>
                <!-- 这里主要配置资源文件,详见下面遇到的问题及解决办法-->
                <resource>
                    <filtering>true</filtering>  <!-- 加上filter会过滤该资源路径中的文件 -->
                    <directory>${project.basedir}/src/main/resources</directory>
                    <includes>
                        <include>**/*</include>
                    </includes>
                </resource>
            </resources>
        </build>
        <profiles>
            <!-- 开发环境,默认激活 -->
            <profile>
                <id>dev</id>
                <activation><!--默认启用的是dev环境配置-->
                    <activeByDefault>true</activeByDefault>
                </activation>
                <properties>
                    <!-- 版本配置项 -->
                    <logPath>D:\\logs\\logs.log</logPath>
                    <logLevel>info, ServerDailyRollingFile, stdout</logLevel>
                    <databaseUrl>jdbc:mysql://192.168.1.81:3306/handy?allowMultiQueries=true&amp;characterEncoding=utf8&amp;useUnicode=true</databaseUrl>
                    <mqUrl>tcp://192.168.1.91</mqUrl>
                    <userName>handy_lucien_dev</userName>
                    <clientId>handy_system_dev</clientId>
                    <!-- 版本配置项 -->
                </properties>
            </profile>
            <!-- 生产环境 -->
            <profile>
                <id>release</id>
                <properties>
                    <!-- 版本配置项 -->
                    <logPath>/home/server/logs/handy/business/business.log</logPath>
                    <logLevel>info, ServerDailyRollingFile</logLevel>
                    <databaseUrl>jdbc:mysql://127.0.0.1:3306/handy?allowMultiQueries=true&amp;characterEncoding=utf8&amp;useUnicode=true</databaseUrl>
                    <mqUrl>tcp://192.168.1.91</mqUrl>
                    <userName>handy_lucien_release</userName>
                    <clientId>handy_system_release</clientId>
                    <!-- 版本配置项 -->
                </properties>
            </profile>
        </profiles>
    </project>
    
    

    properties文件里主要配置项

    mqUrl=${mqUrl}
    userName=${userName}
    clientId=${clientId}
    jdbc_url=${databaseUrl}
    
    

    log4j.properties

    log4j.rootLogger=${logLevel}
    log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd
    log4j.appender.ServerDailyRollingFile.File=${logPath}
    log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout
    log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d - %m%n
    log4j.appender.ServerDailyRollingFile.Append=true
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p [%c] %m%n
    
    

    spring-mvc.xml
    <context:property-placeholder location="classpath:properties/*.properties"/>

    代码里引用properties值

        @Value("${mqUrl}")
        private String mqUrl;
        @Value("${userName}")
        private String USER_NAME;
        @Value("${clientId}")
        private String CLIENT_ID;
    

    4、配置Jenkins

    由于前面android构建已经详细描述过,这里只截取关键位置图片。

    新建任务->自由风格任务,填写描述和SVN信息



    Build部分,选择调用顶层maven目标,编译成功后选择前面的deploy war插件选项,其中认证部分填写tomcat的账号密码,详见下文遇到的问题及解决办法。

    一下maven指令代表跳过测试,编译release版本
    clean install -Dmaven.test.skip=true -Prelease

    5、遇到的问题及解决办法

     <role rolename="manager-gui"/>
      <role rolename="manager-script"/>
      <role rolename="manager-jmx"/>
      <role rolename="manager-status"/>
      <role rolename="admin-gui"/>
      <role rolename="admin-script"/>
      <user username="tomcat" password="tomcat" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/>
    </tomcat-users>
    

    ${catalina.home}/conf/Catalina/localhost目录下配置2个访问控制文件
    manager.xml:

    <Context privileged="true" antiResourceLocking="false"
             docBase="${catalina.home}/webapps/manager">
        <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^192.168.*$" />
    </Context>
    

    host-manager.xml:

    <Context privileged="true" antiResourceLocking="false"
             docBase="${catalina.home}/webapps/host-manager">
        <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^192.168.*$" />
    </Context>
    

    相关文章

      网友评论

          本文标题:Jenkins构建maven

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