Jenkins持续集成Web项目

发表于:2018-2-05 13:12

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:阿叮339    来源:博客园

  功能:用户开发完maven构建的web项目后,从本地提交代码到gogs远程仓库中,在执行 git commit 命令之前会先执行 maven配置的 findbugs插件,来检测项目是否有明显bug,如果有就让项目构建失败,git commit 失败。 如果没有明显bug,则提交成功。 gogs配置web钩子,对 push 命令有效。 当用户从本地成功push代码到gogs仓库中时,会触发jenkins项目的构建,jenkins中也会使用findbugs(checkstyle,pmd)再检测一次,设置容错bug数目,如果小于配置bug数则构建成功,自动完成部署。 用户在本地push代码后,可以直接在浏览器中访问项目。
  主要工具以及技术:  eclipse ,java,maven,Git,gogs,jenkins,Git钩子,web钩子
  maven的pom.xml中的主要插件: findbugs plugin
  (1)、新建maven项目,写个简单的类,然后写出该类对应的测试
  编写jsp页面(用于访问部署后的项目,直接使用index.jsp也行)
  pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.demo</groupId>
<artifactId>jenkins_webtest</artifactId> <!--这是我的项目名-->
<packaging>war</packaging> <!--web项目的打包方式-->
<version>0.0.1-SNAPSHOT</version>
<name>jenkins_webtest Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<finalName>jenkins_webtest</finalName>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${compiler.source}</source>
<target>${compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<compilerArguments>
<extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
<!-- findbugs插件 :静态检查代码的错误-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.4</version>
<configuration>
<!-- 设置分析工作的等级,可以为Min、Default和Max -->
<effort>Low</effort>
<!-- Low、Medium和High (Low最严格) -->
<threshold>Medium</threshold>
<failOnError>true</failOnError>
<includeTests>true</includeTests>
<!--findbugs需要忽略的错误的配置文件-->
<!--  <excludeFilterFile>compile.bat</excludeFilterFile> -->
</configuration>
<executions>
<execution>
<id>run-findbugs</id>
<!-- 在install 阶段触发执行findbugs检查,比如执行 mvn clean package-->
<phase>install</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<!--将脚本文件件放在项目war包以外一起打包的插件  这个配置主要用户java项目执行时打包脚本的,web项目可以不用配置此插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>${project.version}</id><!--名字任意 -->
<phase>package</phase>   <!-- 绑定到package生命周期阶段上 -->
<goals>
<goal>single</goal>   <!-- 只运行一次 -->
</goals>
<configuration>
<descriptors>   <!--描述文件路径-->
<descriptor>script.xml</descriptor>
</descriptors>
<!--这样配置后,mvn deploy不会把assembly打的zip包上传到nexus-->
<attach>false</attach>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compiler.source>1.7</compiler.source>
<compiler.target>1.7</compiler.target>
<!-- servlet/jsp/EL (2.4/2.0/?)(2.5/2.1/2.1),(3.0/2.2/2.2),(3.1/2.3/3.0) -->
<servlet.version>3.1.0</servlet.version>
<jsp.version>2.3.1</jsp.version>
<jstl.version>1.2</jstl.version>
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
</dependencies>
</project>
  (2)、新建gogs仓库,仓库名为 jenkins_webtest ,然后进入git bush here ,通过 git clone  http://localhost:3000/test/jenkins_webtest.git 命令,将仓库克隆到本地
  使用 cd 命令 ,进入克隆下来的项目   $ cd jenkins_webtest
  里面有个 .git文件夹 ,打开.git文件夹后 里面有个文件名为 hooks的文件夹 在hooks目录下新建 pre-commit 文件 (这个文件没有后缀名,在用户使用 git  commit 命令时会自动先调用该文件 ,脚本命令就写在该文件(pre-commit)中)    $ cd .git/hooks     $ touch pre-commit   然后编辑该文件  $ vi pre-commit
  将这段内容复制进去:  (整个脚本的返回值为0时,才能commit 成功;当脚本返回值为非0时,git commit 失败)
#!/bin/sh
#execute shell before commit,check the code
mvn clean install   #这里执行install命令时,根据前面pom.xml的配置,会执行findbugs:findbugs命令
#recieve the  execute result   接收执行结果
result=$?
#output the result ,if the result more than or equal 0 ,it proves this project has bugs,otherwise don't.
#如果执行结果为0 ,表示没有bug,maven项目build成功
#如果执行结果为非0,表示有bug,maven项目 build 失败
echo $result
if [ $result -ne 0 ] #判断执行结果是否等于0     (-ne  : 不等于)
then
mvn findbugs:gui  #弹出findbugs的gui,里面会显示bug的具体位置以及原因
echo "REGRETFUL! BUILD FAILURE"
exit 1  #有bug时,让Git commit失败,无法提交上去
else
echo "CONGRATURATION! BUILD SUCCESS"
exit 0  #没有bug时,以 0 退出,是的commit成功,可以继续push
fi
  (3)在gogs仓库中配置 web钩子
  进入项目仓库 --》 点击仓库设置 --》 管理web钩子 --》 添加web钩子 --》gogs --》 推送地址:http://172.150.16.53:8080/gogs-webhook/?job=jenkins_webtest
  (4)在jenkins上构建maven风格的项目,参照jenkins项目的基本配置就行了
  主要配置   源码管理  : Git
  构建触发器: 触发远程构建
  Build: pom.xml  ,后面那行写 clean package findbugs:findbugs  checkstyle:checkstyle  (前提:在可选插件中下载好了 findbugs,checkstyle插件)
  在Post Step 下面 选择 --》 add post-build step :  选择 --》send files or execute command over SSH (前提已经下载了 Publish over  SSH插件,并且测试连接主机)
  在这里的配置
  Name:是在系统设置里面配置的
  Source Files: 这里填写你需要传输的文件  jenkins工作区间中的文件
  Remove prefix: 传输到远程主机后 需要移除的文件前缀 ,这里写了 target
  Remote directory:传输过去到远程主机的什么位置
  Exec Command :这里的命令很简单 只要将war包移到 远程主机的 Tomcat的webapps目录下就行了
  在往下的配置就是 构建设置
  在 Publish Checkstyle analysis results 和  Publish  findbugs analysis results 打钩,就是选中就行了。。点击高级配置 具体看关于静态检测的那篇博文
  然后就可以构建了
  (web部署到本机的Tomcat中,不需要 在Post Step 下面 选择 --》 add post-build step :  选择 --》send files or execute command over SSH 这一步,直接在构建后操作中  选中 Deploy war/ear to a container 进行简单配置就行了 )

上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号