【Jenkins教程三】基于Linux实现Jenkins+maven+git的自动化部署

前言

  • 上一篇教程详细介绍了Jenkins+maven+svn+tomcat的自动化构建部署项目教程,这篇将介绍使用git为版本控制系统的教程。

安装Git

配置tomcat用户

  • 在tomcat-user.xml配置用户
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="password" roles="manager-gui,manager-script,manager-jmx,manager-status"/>
  • 这个用户在咱们构建好项目war包须要发布到tomcat,而这个用户就是咱们能够发布到tomcat的凭证。

配置pom.xml构建命令

  • 须要在pom.xml里面配置项目构建命令,不然该Jenkins启动不会报错也不会发布到tomcat上
<packaging>war</packaging>

    <!--项目构建插件-->
    <build>
        <plugins>
            <!--编译插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- 修改webapp目录为web -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <!-- 设置WebContent目录为Web目录 -->
                    <webappDirectory>${basedir}/web</webappDirectory>
                    <warSourceDirectory>${basedir}/web</warSourceDirectory>
                    <warName>demo</warName>
                </configuration>
            </plugin>
        </plugins>
        <!--资源-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <!-- 是否替换资源中的属性-->
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

插件安装

  • 【发布插件】Deploy to container Plugin
  • 【Git插件】Git plugin
  • 【构建Maven项目插件】Maven Integration plugin
  • 【本地化插件】Locale,这个须要到【系统设置】-【Default Language】输入【zh-CN】并勾选Ignore browser preference and force this language to all users这个选项才能转化为中文

配置Git

  • 点击【系统管理】-【全局工具配置】
  • 选择Git-【新增Git】
  • 【Name】输入自定义Git名称
  • 【Path to Git executable】输入Git安装路径 这里写图片描述
不知道Git安装路径,执行 whereis git

详细步骤

  • 在首页点击【新建】
  • 输入项目名称且选中【构建一个Maven项目】,点击下方【肯定】 这里写图片描述
  • 勾选【use Svn-Partial Release Manager】和【use Subversion Release Manager】,【丢弃旧的构建】 这里写图片描述
  • 选择【源码管理】,勾选Git,【Repository URL】输入Git项目的Https连接,不要ssh。【Credentials】输入Git帐号密码,没有点击右边添加。 这里写图片描述
  • 在【Build】,输入【Root POM】不输默认是根目录下的pom.xml,若是pom.xml不在根目录下面,则须要指定相对目录,输入【Goals and options】输入构建须要执行的mvn命令。
  • 在【Post Step】,选择【run regardless of build result】不管构建结果怎么都执行 这里写图片描述
  • 在【构建设置】勾选【E-mail Notification】,输入通知邮件和相关选项
  • 在【构建后操做】,选择【Deploy war/ear to a container】
  • 【WAR/EAR files】这里根据你maven生成war包的名称填写,可是路径前面必须加上target不然会构建不成功,格式为【**/*.war】。而且不会产生任何错误,也不会部署项目到tomcat,这里是须要进行注意的
  • 【Context path】是生成war包的名称,若是是 /那么就是ROOT.war 这里写图片描述
  • 点击【应用】,进入项目点击【当即构建】
  • 查看【Console Output】,构建成功。 这里写图片描述

后语

  • Git的教程和Svn的教程相差无几,区别在于要配置Git和安装相关插件以及和发布配置那边【Root Path】的配置区别。只要动手作一遍,就会发现该软件的便利之处了。
相关文章
相关标签/搜索