参考文章
使用Jenkins配置Git+Maven的自动化构建
jenkins git插件安装
手动下载Jenkins插件网址
开发人员对代码做了一些修改,在本地运行构建并确认无误之后,将更改提交到代码库(git、svn等)。具有高配置硬件的持续集成服务器每隔30分钟查询代码库一次,发现更新之后,签出所有最新的源代码,然后调用自动化构建工具(maven、ant等)构建项目,该过程包括编译、测试、审查、打包和部署等。然而不幸的是,另外一名开发人员在这一时间段也提交了代码更改,两处更改导致了某些测试的失败,持续集成服务器基于这些失败的测试创建一个报告,并自动发送给相关开发人员。开发人员收到报告后,立即着手调查选用,并尽快修复。
Jenkins是一款持续集成工具,它的前身是Hudson。使用jenkins还不能够完成持续集成工作,还需要版本控制工具(git、svn等)和项目构建工具(maven、ant等)配合才可以完成。
在Jenkins一开始会让你选择安装插件,但是由于网络原因会下载失败,有的插件安装失败,所以需要手动下载在上传上传。
案例:上面错误有两个(这是我解决了部分后还剩下的),先解决pipeline:Stage View plugin v2.4插件安装失败。根据提示发现失败原因是需要先安装pipeline-rest-api v2.4插件。
根据错误提示去插件网站搜索(Ctr+f)需要下载的pipeline-rest-api v2.4插件。
下载的是一个一.hpi结尾的文件。
系统管理–管理插件–高级Tab->上传插件
有的插件安装完成需要重启才能生效(会有提示),重启就是重启tomcat就可以。重启完后已安装的插件就不会出现错误提示了。
构建
让jenkins选择用什么命令构建项目,在前面我们配置maven所以会选择[invoke top-level Maven targets]进行配置。
构建后操作
该步骤配置构建完后的一些任务,有哪些可以在这里配置呢?任务测试报告,邮件反馈等。
其中邮件反馈最重要,下一篇文件介绍maven—11配置jenkins的邮件反馈。
git clone https://github.com/zlcook/cidemo.git
<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.0modelVersion>
<groupId>com.zlcook.studymvngroupId>
<artifactId>hellowordartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>maven helloword projectname>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.10version>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-shade-pluginartifactId>
<version>2.4.3version>
<executions>
<execution>
<phase>packagephase>
<goals>
<goal>shadegoal>
goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.zlcook.studymvn.helloword.HelloWordmainClass>
transformer>
transformers>
configuration>
execution>
executions>
plugin>
plugins>
build>
project>
package com.zlcook.studymvn.helloword;
public class HelloWord
{
public String say(){
return "hello maven";
}
public static void main(String[] args){
System.out.print(new HelloWord().say());
}
}
package com.zlcook.studymvn.helloword;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class HelloWordTest
{
@Test
public void testSay(){
HelloWord helloWord = new HelloWord();
String result = helloWord.say();
assertEquals("hello maven",result);
}
}
在cidemo目录下进行如下操作
* 查看变动文件
git status
* 添加所有文件到缓存中
git add .
* 提交文件到本地仓库
git commit -m “第一次提交”
Started by user zlcook
Building in workspace C:\Windows\system32\config\systemprofile\.jenkins\workspace\helloword
> D:\Soft\git\Git\bin\git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> D:\Soft\git\Git\bin\git.exe config remote.origin.url https://github.com/zlcook/cidemo.git # timeout=10
Fetching upstream changes from https://github.com/zlcook/cidemo.git
> D:\Soft\git\Git\bin\git.exe --version # timeout=10
using GIT_SSH to set credentials
> D:\Soft\git\Git\bin\git.exe fetch --tags --progress https://github.com/zlcook/cidemo.git +refs/heads/*:refs/remotes/origin/*
> D:\Soft\git\Git\bin\git.exe rev-parse "refs/remotes/origin/master^{commit}" # timeout=10
> D:\Soft\git\Git\bin\git.exe rev-parse "refs/remotes/origin/origin/master^{commit}" # timeout=10
Checking out Revision e75623ce6b045f7db3667c1dccfb3e3cc61e21c7 (refs/remotes/origin/master)
> D:\Soft\git\Git\bin\git.exe config core.sparsecheckout # timeout=10
> D:\Soft\git\Git\bin\git.exe checkout -f e75623ce6b045f7db3667c1dccfb3e3cc61e21c7
> D:\Soft\git\Git\bin\git.exe rev-list e75623ce6b045f7db3667c1dccfb3e3cc61e21c7 # timeout=10
[helloword] $ cmd.exe /C "D:\Soft\maven\apache-maven-3.3.3\bin\mvn.cmd -s D:\Soft\maven\apache-maven-3.3.3\conf\settings.xml clean deploy && exit %%ERRORLEVEL%%"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven helloword project 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for org.apache.maven.plugins:maven-resources-plugin:jar:2.6 is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.109 s
[INFO] Finished at: 2016-11-30T15:07:16+08:00
[INFO] Final Memory: 5M/123M
[INFO] ------------------------------------------------------------------------
[ERROR] Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved: Failure to find org.apache.maven.plugins:maven-resources-plugin:jar:2.6 in http://172.19.201.155:8081/repository/maven-public/ was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException
Build step 'Invoke top-level Maven targets' marked build as failure
Finished: FAILURE
失败原因部署私服的服务器没有网,本地缓存了错误,解决方法删掉本地的maven-resources-plugin插件,让私服连接上网络。
详细一点的原因分析maven—9使用Nexus创建私服—>3.4.2代理仓库配置信息下面的案例。
Started by user zlcook
Building in workspace C:\Windows\system32\config\systemprofile\.jenkins\workspace\helloword
> D:\Soft\git\Git\bin\git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> D:\Soft\git\Git\bin\git.exe config remote.origin.url https://github.com/zlcook/cidemo.git # timeout=10
Fetching upstream changes from https://github.com/zlcook/cidemo.git
> D:\Soft\git\Git\bin\git.exe --version # timeout=10
using GIT_SSH to set credentials
> D:\Soft\git\Git\bin\git.exe fetch --tags --progress https://github.com/zlcook/cidemo.git +refs/heads/*:refs/remotes/origin/*
> D:\Soft\git\Git\bin\git.exe rev-parse "refs/remotes/origin/master^{commit}" # timeout=10
> D:\Soft\git\Git\bin\git.exe rev-parse "refs/remotes/origin/origin/master^{commit}" # timeout=10
Checking out Revision e75623ce6b045f7db3667c1dccfb3e3cc61e21c7 (refs/remotes/origin/master)
> D:\Soft\git\Git\bin\git.exe config core.sparsecheckout # timeout=10
> D:\Soft\git\Git\bin\git.exe checkout -f e75623ce6b045f7db3667c1dccfb3e3cc61e21c7
> D:\Soft\git\Git\bin\git.exe rev-list e75623ce6b045f7db3667c1dccfb3e3cc61e21c7 # timeout=10
[helloword] $ cmd.exe /C "D:\Soft\maven\apache-maven-3.3.3\bin\mvn.cmd -s D:\Soft\maven\apache-maven-3.3.3\conf\settings.xml clean deploy && exit %%ERRORLEVEL%%"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven helloword project 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://172.19.201.155:8081/repository/maven-public/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.pom
2/8 KB
6/8 KB
8/8 KB
Downloaded: http://172.19.201.155:8081/repository/maven-public/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.pom (8 KB at 2.9 KB/sec)
Downloading: http://172.19.201.155:8081/repository/maven-public/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.jar
2/29 KB
6/29 KB
10/29 KB
14/29 KB
18/29 KB
22/29 KB
26/29 KB
29/29 KB
Downloaded: http://172.19.201.155:8081/repository/maven-public/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.jar (29 KB at 18.0 KB/sec)
Downloading: http://172.19.201.155:8081/repository/maven-public/org/apache/maven/plugins/maven-compiler-plugin/3.1/maven-compiler-plugin-3.1.pom
Downloaded: http://172.19.201.155:8081/repository/maven-public/org/apache/maven/plugins/maven-compiler-plugin/3.1/maven-compiler-plugin-3.1.pom (0 B at 0.0 KB/sec)
Downloading: http://172.19.201.155:8081/repository/maven-public/org/apache/maven/plugins/maven-plugins/24/maven-plugins-24.pom
Downloaded: http://172.19.201.155:8081/repository/maven-public/org/apache/maven/plugins/maven-plugins/24/maven-plugins-24.pom (0 B at 0.0 KB/sec)
Downloading: http://172.19.201.155:8081/repository/maven-public/org/apache/maven/plugins/maven-compiler-plugin/3.1/maven-compiler-plugin-3.1.jar
....省略下载各种构件
Downloaded: http://172.19.201.155:8081/repository/maven-public/org/codehaus/plexus/plexus-utils/3.0.22/plexus-utils-3.0.22.jar (0 B at 0.0 KB/sec)
Downloaded: http://172.19.201.155:8081/repository/maven-public/com/google/guava/guava/11.0.2/guava-11.0.2.jar (0 B at 0.0 KB/sec)
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing C:\Windows\System32\config\systemprofile\.jenkins\workspace\helloword\target\helloword-0.0.1-SNAPSHOT.jar with C:\Windows\System32\config\systemprofile\.jenkins\workspace\helloword\target\helloword-0.0.1-SNAPSHOT-shaded.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ helloword ---
[INFO] Installing C:\Windows\System32\config\systemprofile\.jenkins\workspace\helloword\target\helloword-0.0.1-SNAPSHOT.jar to D:\Soft\maven\maven_jar\repository\com\zlcook\studymvn\helloword\0.0.1-SNAPSHOT\helloword-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Windows\System32\config\systemprofile\.jenkins\workspace\helloword\pom.xml to D:\Soft\maven\maven_jar\repository\com\zlcook\studymvn\helloword\0.0.1-SNAPSHOT\helloword-0.0.1-SNAPSHOT.pom
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ helloword ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:11 min
[INFO] Finished at: 2016-11-30T15:17:39+08:00
[INFO] Final Memory: 19M/159M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project helloword: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Build step 'Invoke top-level Maven targets' marked build as failure
Finished: FAILURE
Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project helloword: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
<project>
...
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
...
<distributionManagement>
<repository>
<id>nexus-releasesid>
<name>Nexus ReleaseRepositoryname>
<url>http://172.19.201.155:8081/repository/maven-releases/url>
repository>
<snapshotRepository>
<id>nexus-snapshotsid>
<name>Nexus SnapshotsRepositoryname>
<url>http://172.19.201.155:8081/repository/maven-snapshots/url>
snapshotRepository>
distributionManagement>
project>
c.提交代码到github上
修改了代码则需要将变动信息提交到github上,操作步骤参考上面。
d.再次构建任务成功
因为配置任务的时候配置了【每隔15分钟轮询源码库,如果发现有更新的时候构建本项目】和【发现github上有变化时构建本项目】,所以我这次没有手动构建,让jenkins通过构建触发器来自动构建项目。
构建成功后任务的console输出
Started by an SCM change
Building in workspace C:\Windows\system32\config\systemprofile\.jenkins\workspace\helloword
> D:\Soft\git\Git\bin\git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> D:\Soft\git\Git\bin\git.exe config remote.origin.url https://github.com/zlcook/cidemo.git # timeout=10
Fetching upstream changes from https://github.com/zlcook/cidemo.git
> D:\Soft\git\Git\bin\git.exe --version # timeout=10
using GIT_SSH to set credentials
> D:\Soft\git\Git\bin\git.exe fetch --tags --progress https://github.com/zlcook/cidemo.git +refs/heads/*:refs/remotes/origin/*
> D:\Soft\git\Git\bin\git.exe rev-parse "refs/remotes/origin/master^{commit}" # timeout=10
> D:\Soft\git\Git\bin\git.exe rev-parse "refs/remotes/origin/origin/master^{commit}" # timeout=10
Checking out Revision ae2a6a18ebfd933e4fa81c3f7f691d8fb870a240 (refs/remotes/origin/master)
> D:\Soft\git\Git\bin\git.exe config core.sparsecheckout # timeout=10
> D:\Soft\git\Git\bin\git.exe checkout -f ae2a6a18ebfd933e4fa81c3f7f691d8fb870a240
> D:\Soft\git\Git\bin\git.exe rev-list 15e935f15d8ac3190d7423eb5132b2e847a518ee # timeout=10
[helloword] $ cmd.exe /C "D:\Soft\maven\apache-maven-3.3.3\bin\mvn.cmd -s D:\Soft\maven\apache-maven-3.3.3\conf\settings.xml clean deploy && exit %%ERRORLEVEL%%"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven helloword project 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ helloword ---
[INFO] Deleting C:\Windows\System32\config\systemprofile\.jenkins\workspace\helloword\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloword ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Windows\System32\config\systemprofile\.jenkins\workspace\helloword\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ helloword ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Windows\System32\config\systemprofile\.jenkins\workspace\helloword\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ helloword ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Windows\System32\config\systemprofile\.jenkins\workspace\helloword\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ helloword ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Windows\System32\config\systemprofile\.jenkins\workspace\helloword\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ helloword ---
[INFO] Surefire report directory: C:\Windows\System32\config\systemprofile\.jenkins\workspace\helloword\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.zlcook.studymvn.helloword.HelloWordTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.038 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ helloword ---
[INFO] Building jar: C:\Windows\System32\config\systemprofile\.jenkins\workspace\helloword\target\helloword-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-shade-plugin:2.4.3:shade (default) @ helloword ---
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing C:\Windows\System32\config\systemprofile\.jenkins\workspace\helloword\target\helloword-0.0.1-SNAPSHOT.jar with C:\Windows\System32\config\systemprofile\.jenkins\workspace\helloword\target\helloword-0.0.1-SNAPSHOT-shaded.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ helloword ---
[INFO] Installing C:\Windows\System32\config\systemprofile\.jenkins\workspace\helloword\target\helloword-0.0.1-SNAPSHOT.jar to D:\Soft\maven\maven_jar\repository\com\zlcook\studymvn\helloword\0.0.1-SNAPSHOT\helloword-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Windows\System32\config\systemprofile\.jenkins\workspace\helloword\pom.xml to D:\Soft\maven\maven_jar\repository\com\zlcook\studymvn\helloword\0.0.1-SNAPSHOT\helloword-0.0.1-SNAPSHOT.pom
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ helloword ---
Downloading: http://172.19.201.155:8081/repository/maven-snapshots/com/zlcook/studymvn/helloword/0.0.1-SNAPSHOT/maven-metadata.xml
778/778 B
Downloaded: http://172.19.201.155:8081/repository/maven-snapshots/com/zlcook/studymvn/helloword/0.0.1-SNAPSHOT/maven-metadata.xml (778 B at 2.4 KB/sec)
Uploading: http://172.19.201.155:8081/repository/maven-snapshots/com/zlcook/studymvn/helloword/0.0.1-SNAPSHOT/helloword-0.0.1-20161201.015626-2.jar
2/4 KB
4/4 KB
Uploaded: http://172.19.201.155:8081/repository/maven-snapshots/com/zlcook/studymvn/helloword/0.0.1-SNAPSHOT/helloword-0.0.1-20161201.015626-2.jar (4 KB at 28.1 KB/sec)
Uploading: http://172.19.201.155:8081/repository/maven-snapshots/com/zlcook/studymvn/helloword/0.0.1-SNAPSHOT/helloword-0.0.1-20161201.015626-2.pom
2/3 KB
3/3 KB
Uploaded: http://172.19.201.155:8081/repository/maven-snapshots/com/zlcook/studymvn/helloword/0.0.1-SNAPSHOT/helloword-0.0.1-20161201.015626-2.pom (3 KB at 21.7 KB/sec)
Downloading: http://172.19.201.155:8081/repository/maven-snapshots/com/zlcook/studymvn/helloword/maven-metadata.xml
288/288 B
Downloaded: http://172.19.201.155:8081/repository/maven-snapshots/com/zlcook/studymvn/helloword/maven-metadata.xml (288 B at 5.6 KB/sec)
Uploading: http://172.19.201.155:8081/repository/maven-snapshots/com/zlcook/studymvn/helloword/0.0.1-SNAPSHOT/maven-metadata.xml
778/778 B
Uploaded: http://172.19.201.155:8081/repository/maven-snapshots/com/zlcook/studymvn/helloword/0.0.1-SNAPSHOT/maven-metadata.xml (778 B at 7.4 KB/sec)
Uploading: http://172.19.201.155:8081/repository/maven-snapshots/com/zlcook/studymvn/helloword/maven-metadata.xml
288/288 B
Uploaded: http://172.19.201.155:8081/repository/maven-snapshots/com/zlcook/studymvn/helloword/maven-metadata.xml (288 B at 2.8 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.865 s
[INFO] Finished at: 2016-12-01T09:56:27+08:00
[INFO] Final Memory: 20M/198M
[INFO] ------------------------------------------------------------------------
Finished: SUCCESS
cidemo项目不是一个javaEE项目,目前没有涉及到tomcat,后面会做一个javaEE项目,通过持续集成把项目自动部署到tomcat上。当然在这之前我要学会编写shell脚本。
这篇文章没有写持续集成的邮件反馈,一个项目在持续集成过程中如果出错了,这个错误信息要及时反馈给项目经理及造成这次错误的开发人员,以便尽快修复。下一篇文章我会介绍maven—11配置jenkins的邮件反馈
有什么不懂的一起探讨一下吧,我也是在学习的路上。喜欢给我点个赞吧(哈哈),我会继续努力的。