一 概述
本文主要介绍jenkins,sonarQube的安装与集成,代码分析有sonarQUbe和sonar-runner构建,或者在maven项目情况下可以sonarQbude和maven的构建。介绍jenkins的编译打包部署,代码检查。最后集成jenkins
二 预装软件
- 软件名称 平台 备注
- jdk1.8.0_60 Win10 操作系统
- apache-tomcat-8.0.20 Win10 Tomcat容器
- Jenkins Win10 端口 8088
- sonarqube-6.2 Win10 端口 9090
- sonar-runner-2.6.1 Win10
- mysql 5.6 Win10
- nexus-3.2.0-01 Linux Maven私有服务
三 配置环境变量
配置各软件的环境变量方法不展开描述。除nexus外,其余都在安装在本地。
四 sonarQube安装
4.1 将sonaeQbude解压到本地。
修改sonar.properties文件.数据库使用本地的mysql,创建一个名为sonar的库。
当sonarQube启动时候会自动创建表。
建库语句
-
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
-
sonar.jdbc.username=root
-
sonar.jdbc.password=11111
-
sonar.jdbc.url=jdbc:mysql:
//localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
-
sonar.jdbc.driverClassName=com.mysql.jdbc.Driver
新的sonarQbude的表相对旧版来说有了新的改动,升级时候需要把旧的表删除。否则会报以下错。
-
exception caught on transport layer [[id: 0xc7fbf08e, /127.0.0.1:55381 => /127.0.0.1:9001]], closing connection
-
java.io.IOException: 远程主机强迫关闭了一个现有的连接。
4.2 启动sonarQube
运行“StartSonar.bat”, 访问9000端口,sonar默认自带了jetty服务器。
默认用户名密码 admin、admin。一些设置和菜单只有在登陆之后才能看到。
因个人9000端口有其他用途,将端口改为9090.
-
sonar.web.port=
9090
4.3汉化sonarQube
官网地址 http://docs.sonarqube.org/display/PLUG/Plugin+Library
将sonar-l10n-zh-plugin-1.13.jar放置于sonarqube-6.2\extensions\plugins下。重启sonarQube。
soanrQube有一些需要到的插件,用于代码分析。
插件介绍
插件的下载地址为:http://docs.codehaus.org/display/SONAR/Plugin+Library
将下载后的插件上传到${SONAR_HOME}extensions\plugins目录下,重新启动sonar。
sonar默认集成了Java Ecosystem插件,该插件是一组插件的合集
(1)Java [sonar-java-plugin]:java源代码解析,计算指标等
(2)Squid [sonar-squid-java-plugin]:检查违反Sonar定义规则的代码
(3)Checkstyle [sonar-checkstyle-plugin]:使用CheckStyle检查违反统一代码编写风格的代码
(4)FindBugs [sonar-findbugs-plugin]:使用FindBugs检查违反规则的缺陷代码
(5)PMD [sonar-pmd-plugin]:使用pmd检查违反规则的代码
(6)Surefire [sonar-surefire-plugin]:使用Surefire执行单元测试
(7)Cobertura [sonar-cobertura-plugin]:使用Cobertura获取代码覆盖率
(8)JaCoCo [sonar-jacoco-plugin]:使用JaCOCO获取代码覆盖率
常用的插件:
(1)
JavaScript
代码检查:http://docs.codehaus.org/display/SONAR/JavaScript+Plugin
(2)
Python
代码检查:http://docs.codehaus.org/display/SONAR/Python+Plugin
(3)Web页面检查(HTML、JSP、JSF、Ruby、
PHP
等):http://docs.codehaus.org/display/SONAR/Web+Plugin
(4)xml文件检查:http://docs.codehaus.org/display/SONAR/XML+Plugin
(5)scm源码库统计分析:http://docs.codehaus.org/display/SONAR/SCM+Stats+Plugin
(6)文件度量:http://docs.codehaus.org/display/SONAR/Tab+Metrics+Plugin
(7)中文语言包:http://docs.codehaus.org/display/SONAR/Chinese+Pack
(8)时间表显示度量结果:http://docs.codehaus.org/display/SONAR/Timeline+Plugin
(9)度量结果演进图:http://docs.codehaus.org/display/SONAR/Motion+Chart+Plugin
(b)插件配置示例(本段内容来自
http://www.ibm.com/developerworks/cn/java/j-lo-sonar/
五 sonar-runner安装
5.1 修改sonar-runnerc下的conf的sonar-scanner.properties配置文件
-
sonar.host.url=http:
//localhost:9090
-
sonar.jdbc.username=root
-
sonar.jdbc.password=
11111
-
sonar.jdbc.url=jdbc:mysql:
//10.10.5.64:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
-
sonar.jdbc.driverClassName=com.mysql.jdbc.Driver
5.2 在项目的根目录下添加文件sonar-project.properties
1 单一个项目
-
sonar.projectKey=pljj
-
sonar.projectName=pljj
-
sonar.projectVersion=
1.0
-SNAPSHOT
-
sonar.sources=src/main/java
-
sonar.tests=src/test/java
-
sonar.binaries=target/classes
-
-
sonar.language=java
2 maven多模块
-
sonar.projectKey=pljj
-
sonar.projectName=pljj
-
sonar.projectVersion=
1.0
-
sonar.sources=.
-
sonar.language=java
-
sonar.sourceEncoding=UTF-
8
或者
-
sonar.projectKey=com.pljj-job
-
sonar.projectName=elastic-job
-
sonar.projectVersion=
1.1
.
0
-
# Set modules IDs
-
sonar.modules=pljj-core,pljj-console,pljj-spring
-
# Modules inherit properties set at parent level
-
sonar.sources=src/main/java
-
sonar.tests=src/test/java
-
sonar.java.binaries=target
-
sonar.language=java
5.3,运行sonar-runner
在“开始”》“运行”中输入cmd切换的项目更目录 输入“sonar-runner”
以下为成功目录
登陆http://localhost:9090/ 查看代码扫描结果,帐号密码admin/admin
六 mave安装和sonarQbude配合使用
6.1在maven官网下载maven
http://maven.apache.org/download.cgi
打开cmd窗口数据 mvn –v 查看maven版本,打印如下表示安装成功。
6.2 在%M2_HOME%\conf\setting.xml中输入下面内容(直接用下面的内容覆盖原文件)
-
<?
xmlversion
xmlversion
=
“1.0”
encoding
=
“UTF-8”
?>
-
<
settingsxmlns
settingsxmlns
=
“http://maven.apache.org/SETTINGS/1.0.0”
-
xmlns:xsi
=
“http://www.w3.org/2001/XMLSchema-instance”
-
xsi:schemaLocation
=
“http://maven.apache.org/SETTINGS/1.0.0http://maven.apache.org/xsd/settings-1.0.0.xsd”
>
-
<
profiles
>
-
<
profile
>
-
<
id
>
sonar
</
id
>
-
<
activation
>
-
<
activeByDefault
>
true
</
activeByDefault
>
-
</
activation
>
-
<
properties
>
-
<!– EXAMPLE FOR MYSQL–>
-
<
sonar.jdbc.url
>
-
jdbc:mysql://localhost:3306/sonar?
useUnicode
=
true
&
characterEncoding
=
utf8
-
</
sonar.jdbc.url
>
-
<
sonar.jdbc.driverClassName
>
com.mysql.jdbc.Driver
</
sonar.jdbc.driverClassName
>
-
<
sonar.jdbc.username
>
root
</
sonar.jdbc.username
>
-
<
sonar.jdbc.password
>
11111
</
sonar.jdbc.password
>
-
-
<!– SERVER ON A REMOTE HOST–>
-
<
sonar.host.url
>
http://localhost:9000
</
sonar.host.url
>
-
</
properties
>
-
</
profile
>
-
</
profiles
>
-
</
settings
>
使用asonr-maven-plugin进行分析,在项目的pom.xml里面添加
参考官网:
http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Maven
-
<
build
>
-
<
pluginManagement
>
-
<
plugins
>
-
<
plugin
>
-
<
groupId
>
org.codehaus.mojo
</
groupId
>
-
<
artifactId
>
sonar-maven-plugin
</
artifactId
>
-
<
version
>
2.7.1
</
version
>
-
</
plugin
>
-
</
plugins
>
-
</
pluginManagement
>
-
</
build
>
保存后找到你需要被sonar分析的maven项目根目录, 被分析的项目必须带有pom文件
6.3、在CMD窗口下,命令行进入到项目文件的目录中去,然后再输入mvn sonar:soanr进行测试
6.4、在浏览器中输入http://localhost:9000查看结果。
7 ,nexus安装
nexus仓库并不是必须的,不使用私有仓库情况,不必安装。
7.1下载nexus
http://www.sonatype.org/nexus/go/ 我的是linux版本
将安装包复制到需要安装的机器,我安装在/opt/soft/nexus
tar –zxvf nexus-latest-bundle.tar.gz
解压后会出现两个目录
cd nexus-2.7.7-01/bin
./nexus
输出如下:
列出了nexus的各种命令
./nexus start 启动服务
然后在本机访问http://localhost:8081/nexus,默认用户名密码:admin,admin123
如果是远程机访问还需要开通8081端口,操作如下:
-
/sbin/iptables -I INPUT -p tcp –dport
8081
-j ACCEPT #开启
8081
端口
-
/etc/rc.d/init.d/iptables save #保存配置
-
/etc/rc.d/init.d/iptables restart #重启服务
-
查看端口是否已经开放
-
/etc/init.d/iptables status
7.2 项目本地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/xsd/maven-4.0.0.xsd”
>
-
<
modelVersion
>
4.0.0
</
modelVersion
>
-
-
<
modules
>
-
<
module
>
pljj-common
</
module
>
-
<
module
>
pljj-core
</
module
>
-
<
module
>
pljj-report
</
module
>
-
</
modules
>
-
-
<
groupId
>
com.pljj
</
groupId
>
-
<
artifactId
>
pljj-pom
</
artifactId
>
-
<
version
>
0.0.1-SNAPSHOT
</
version
>
-
<
packaging
>
pom
</
packaging
>
-
<
name
>
pljj-pom
</
name
>
-
<
url
>
http://maven.apache.org
</
url
>
-
-
<
properties
>
-
<
project.build.sourceEncoding
>
UTF-8
</
project.build.sourceEncoding
>
-
<
project.groupId
>
com.pljj
</
project.groupId
>
-
<
project.version
>
0.0.1-SNAPSHOT
</
project.version
>
-
<
spring.version
>
3.2.9.RELEASE
</
spring.version
>
-
<
lucene.version
>
3.6.2
</
lucene.version
>
-
<
querydsl.version
>
2.9.0
</
querydsl.version
>
-
<
jackson.version
>
2.0.6
</
jackson.version
>
-
<
shiro.version
>
1.2.1
</
shiro.version
>
-
</
properties
>
-
-
<
dependencies
>
-
<!– dependency of apache-commons –>
-
<
dependency
>
-
<
groupId
>
commons-beanutils
</
groupId
>
-
<
artifactId
>
commons-beanutils
</
artifactId
>
-
<
version
>
1.9.2
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
org.apache.commons
</
groupId
>
-
<
artifactId
>
commons-lang3
</
artifactId
>
-
<
version
>
3.1
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
commons-cli
</
groupId
>
-
<
artifactId
>
commons-cli
</
artifactId
>
-
<
version
>
1.2
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
commons-codec
</
groupId
>
-
<
artifactId
>
commons-codec
</
artifactId
>
-
<
version
>
1.6
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
commons-io
</
groupId
>
-
<
artifactId
>
commons-io
</
artifactId
>
-
<
version
>
2.2
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
commons-net
</
groupId
>
-
<
artifactId
>
commons-net
</
artifactId
>
-
<
version
>
3.3
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
commons-fileupload
</
groupId
>
-
<
artifactId
>
commons-fileupload
</
artifactId
>
-
<
version
>
1.3.1
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
commons-dbcp
</
groupId
>
-
<
artifactId
>
commons-dbcp
</
artifactId
>
-
<
version
>
1.3
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
org.apache.ant
</
groupId
>
-
<
artifactId
>
ant
</
artifactId
>
-
<
version
>
1.8.4
</
version
>
-
</
dependency
>
-
-
<!– dependency of lucene –>
-
<
dependency
>
-
<
groupId
>
org.apache.lucene
</
groupId
>
-
<
artifactId
>
lucene-core
</
artifactId
>
-
<
version
>
${lucene.version}
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
org.apache.lucene
</
groupId
>
-
<
artifactId
>
lucene-analyzers
</
artifactId
>
-
<
version
>
${lucene.version}
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
org.apache.lucene
</
groupId
>
-
<
artifactId
>
lucene-highlighter
</
artifactId
>
-
<
version
>
${lucene.version}
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
org.apache.lucene
</
groupId
>
-
<
artifactId
>
lucene-memory
</
artifactId
>
-
<
version
>
${lucene.version}
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
org.apache.lucene
</
groupId
>
-
<
artifactId
>
lucene-queries
</
artifactId
>
-
<
version
>
${lucene.version}
</
version
>
-
</
dependency
>
-
-
<!– dependency of springframework –>
-
<
dependency
>
-
<
groupId
>
org.springframework
</
groupId
>
-
<
artifactId
>
spring-core
</
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-web
</
artifactId
>
-
<
version
>
${spring.version}
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
org.springframework
</
groupId
>
-
<
artifactId
>
spring-webmvc
</
artifactId
>
-
<
version
>
${spring.version}
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
org.springframework.data
</
groupId
>
-
<
artifactId
>
spring-data-commons-core
</
artifactId
>
-
<
version
>
1.4.1.RELEASE
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
org.springframework.data
</
groupId
>
-
<
artifactId
>
spring-data-jpa
</
artifactId
>
-
<
version
>
1.2.0.RELEASE
</
version
>
-
</
dependency
>
-
-
<
dependency
>
-
<
groupId
>
org.slf4j
</
groupId
>
-
<
artifactId
>
slf4j-api
</
artifactId
>
-
<
version
>
1.7.2
</
version
>
-
</
dependency
>
-
-
<
dependency
>
-
<
groupId
>
ch.qos.logback
</
groupId
>
-
<
artifactId
>
logback-classic
</
artifactId
>
-
<
version
>
1.0.7
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
ch.qos.logback
</
groupId
>
-
<
artifactId
>
logback-core
</
artifactId
>
-
<
version
>
1.0.7
</
version
>
-
</
dependency
>
-
-
<!– dependency of freemarker –>
-
<
dependency
>
-
<
groupId
>
org.freemarker
</
groupId
>
-
<
artifactId
>
freemarker
</
artifactId
>
-
<
version
>
2.3.19
</
version
>
-
</
dependency
>
-
-
<!– dependency of querydsl –>
-
<
dependency
>
-
<
groupId
>
com.mysema.querydsl
</
groupId
>
-
<
artifactId
>
querydsl-jpa
</
artifactId
>
-
<
version
>
${querydsl.version}
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
com.mysema.querydsl
</
groupId
>
-
<
artifactId
>
querydsl-lucene
</
artifactId
>
-
<
version
>
${querydsl.version}
</
version
>
-
</
dependency
>
-
-
<!– dependency of hibernate-jpa –>
-
<
dependency
>
-
<
groupId
>
org.hibernate
</
groupId
>
-
<
artifactId
>
hibernate-core
</
artifactId
>
-
<
version
>
3.6.10.Final
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
org.hibernate
</
groupId
>
-
<
artifactId
>
hibernate-entitymanager
</
artifactId
>
-
<
version
>
3.6.10.Final
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
org.hibernate.javax.persistence
</
groupId
>
-
<
artifactId
>
hibernate-jpa-2.0-api
</
artifactId
>
-
<
version
>
1.0.1.Final
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
org.hibernate
</
groupId
>
-
<
artifactId
>
hibernate-validator
</
artifactId
>
-
<
version
>
4.2.0.Final
</
version
>
-
</
dependency
>
-
-
<!– dependency of shiro –>
-
<
dependency
>
-
<
groupId
>
org.apache.shiro
</
groupId
>
-
<
artifactId
>
shiro-web
</
artifactId
>
-
<
version
>
${shiro.version}
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
org.apache.shiro
</
groupId
>
-
<
artifactId
>
shiro-ehcache
</
artifactId
>
-
<
version
>
${shiro.version}
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
org.apache.shiro
</
groupId
>
-
<
artifactId
>
shiro-spring
</
artifactId
>
-
<
version
>
${shiro.version}
</
version
>
-
</
dependency
>
-
-
<!– dependency of jcaptcha –>
-
<
dependency
>
-
<
groupId
>
com.octo.captcha
</
groupId
>
-
<
artifactId
>
jcaptcha
</
artifactId
>
-
<
version
>
2.0-alpha-1
</
version
>
-
</
dependency
>
-
-
<!– dependency of imgscalr –>
-
<
dependency
>
-
<
groupId
>
org.imgscalr
</
groupId
>
-
<
artifactId
>
imgscalr-lib
</
artifactId
>
-
<
version
>
4.2
</
version
>
-
</
dependency
>
-
-
<!– dependency of filters –>
-
<
dependency
>
-
<
groupId
>
com.jhlabs
</
groupId
>
-
<
artifactId
>
filters
</
artifactId
>
-
<
version
>
2.0.235
</
version
>
-
</
dependency
>
-
-
<!– dependency of jackson –>
-
<
dependency
>
-
<
groupId
>
com.fasterxml.jackson.core
</
groupId
>
-
<
artifactId
>
jackson-annotations
</
artifactId
>
-
<
version
>
${jackson.version}
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
com.fasterxml.jackson.core
</
groupId
>
-
<
artifactId
>
jackson-core
</
artifactId
>
-
<
version
>
${jackson.version}
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
com.fasterxml.jackson.core
</
groupId
>
-
<
artifactId
>
jackson-databind
</
artifactId
>
-
<
version
>
${jackson.version}
</
version
>
-
</
dependency
>
-
<
dependency
>
-
<
groupId
>
com.fasterxml.jackson.dataformat
</
groupId
>
-
<
artifactId
>
jackson-dataformat-xml
</
artifactId
>
-
<
version
>
${jackson.version}
</
version
>
-
</
dependency
>
-
-
<!– dependency of htmlparser –>
-
<
dependency
>
-
<
groupId
>
org.htmlparser
</
groupId
>
-
<
artifactId
>
htmlparser
</
artifactId
>
-
<
version
>
1.6
</
version
>
-
</
dependency
>
-
-
<!– dependency of joda –>
-
<
dependency
>
-
<
groupId
>
joda-time
</
groupId
>
-
<
artifactId
>
joda-time
</
artifactId
>
-
<
version
>
2.1
</
version
>
-
</
dependency
>
-
-
<
dependency
>
-
<
groupId
>
com.oracle
</
groupId
>
-
<
artifactId
>
ojdbc14
</
artifactId
>
-
<
version
>
10.2.0.3.0
</
version
>
-
</
dependency
>
-
-
<!–
<
dependency
>
-
<
groupId
>
com.oracle
</
groupId
>
-
<
artifactId
>
ojdbc6
</
artifactId
>
-
<
version
>
12.1.0.1
</
version
>
-
</
dependency
>
—
>
-
-
-
<
dependency
>
-
<
groupId
>
org.aspectj
</
groupId
>
-
<
artifactId
>
aspectjweaver
</
artifactId
>
-
<
version
>
1.7.2
</
version
>
-
</
dependency
>
-
-
<
dependency
>
-
<
groupId
>
javax.servlet
</
groupId
>
-
<
artifactId
>
servlet-api
</
artifactId
>
-
<
version
>
2.4
</
version
>
-
<
scope
>
provided
</
scope
>
-
</
dependency
>
-
-
<
dependency
>
-
<
groupId
>
org.apache.httpcomponents
</
groupId
>
-
<
artifactId
>
httpcore
</
artifactId
>
-
<!–<scope>test</scope>–>
-
<
version
>
4.3.2
</
version
>
-
</
dependency
>
-
-
<
dependency
>
-
<
groupId
>
org.apache.httpcomponents
</
groupId
>
-
<
artifactId
>
httpclient
</
artifactId
>
-
<!–<scope>test</scope>–>
-
<
version
>
4.3.2
</
version
>
-
</
dependency
>
-
-
<
dependency
>
-
<
groupId
>
org.codehaus.woodstox
</
groupId
>
-
<
artifactId
>
stax2-api
</
artifactId
>
-
<
version
>
4.0.0
</
version
>
-
</
dependency
>
-
-
<
dependency
>
-
<
groupId
>
org.quartz-scheduler
</
groupId
>
-
<
artifactId
>
quartz
</
artifactId
>
-
<
version
>
2.1.7
</
version
>
-
</
dependency
>
-
-
<
dependency
>
-
<
groupId
>
junit
</
groupId
>
-
<
artifactId
>
junit
</
artifactId
>
-
<
version
>
3.8.1
</
version
>
-
<
scope
>
test
</
scope
>
-
</
dependency
>
-
</
dependencies
>
-
<
distributionManagement
>
-
<
repository
>
-
<
id
>
nexus
</
id
>
-
<
name
>
Releases
</
name
>
-
<
url
>
http://私服ip:8081/repository/maven-releases
</
url
>
-
</
repository
>
-
<
snapshotRepository
>
-
<
id
>
nexus
</
id
>
-
<
name
>
Snapshot
</
name
>
-
<
url
>
http://私服ip:8081/repository/maven-snapshots
</
url
>
-
</
snapshotRepository
>
-
</
distributionManagement
>
-
<
build
>
-
<
defaultGoal
>
compile
</
defaultGoal
>
-
<
finalName
>
page
</
finalName
>
-
<
plugins
>
-
<
plugin
>
-
<
groupId
>
org.apache.maven.plugins
</
groupId
>
-
<
artifactId
>
maven-surefire-plugin
</
artifactId
>
-
<
version
>
2.6
</
version
>
-
<
configuration
>
-
<
skip
>
true
</
skip
>
-
</
configuration
>
-
</
plugin
>
-
<
plugin
>
-
<
groupId
>
org.apache.maven.plugins
</
groupId
>
-
<
artifactId
>
maven-compiler-plugin
</
artifactId
>
-
<
version
>
2.1
</
version
>
-
<
configuration
>
-
<
source
>
1.6
</
source
>
-
<
target
>
1.6
</
target
>
-
</
configuration
>
-
</
plugin
>
-
</
plugins
>
-
</
build
>
-
</
project
>
7.3 maven配置,修改conf下的setting.xml
-
<
settings
>
-
-
<
pluginGroups
>
-
<
pluginGroup
>
org.sonatype.plugins
</
pluginGroup
>
-
</
pluginGroups
>
-
-
<
servers
>
-
<
server
>
-
<
id
>
nexus
</
id
>
-
<
username
>
admin
</
username
>
-
<
password
>
admin123
</
password
>
-
</
server
>
-
</
servers
>
-
-
<
mirrors
>
-
<
mirror
>
-
<
id
>
nexus
</
id
>
-
<
mirrorOf
>
*
</
mirrorOf
>
-
<
url
>
http://私服ip:8081/repository/maven-public/
</
url
>
-
</
mirror
>
-
<
mirror
>
-
<
id
>
repo2
</
id
>
-
<
mirrorOf
>
central
</
mirrorOf
>
-
<
name
>
Human Readable Name for this Mirror.
</
name
>
-
<
url
>
http://repo2.maven.org/maven2/
</
url
>
-
</
mirror
>
-
-
</
mirrors
>
-
-
<
profiles
>
-
<
profile
>
-
<
id
>
nexus
</
id
>
-
<
repositories
>
-
<
repository
>
-
<
id
>
central
</
id
>
-
<
url
>
http://私服ip:8081/repository/maven-central
</
url
>
-
<
releases
>
<
enabled
>
true
</
enabled
>
</
releases
>
-
<
snapshots
>
<
enabled
>
true
</
enabled
>
</
snapshots
>
-
</
repository
>
-
</
repositories
>
-
<
pluginRepositories
>
-
<
pluginRepository
>
-
<
id
>
central
</
id
>
-
<
url
>
http://私服ip:8081/repository/maven-central
</
url
>
-
<
releases
>
<
enabled
>
true
</
enabled
>
</
releases
>
-
<
snapshots
>
<
enabled
>
true
</
enabled
>
</
snapshots
>
-
</
pluginRepository
>
-
</
pluginRepositories
>
-
</
profile
>
-
</
profiles
>
-
<
activeProfiles
>
-
<
activeProfile
>
nexus
</
activeProfile
>
-
</
activeProfiles
>
-
</
settings
>
然后执行mvn clean deploy
八 jenkins,sonar和 maven集成
8.1 jenkins安装
1,Jenkins的默认工作间在C:\Users\xxxxxx.jenkins,修改工作空为间D:\jenkins,只需要添加环境变量
JENKINS_HOME=D:\jenkins
2,从jenkins官网下载jenkins:https://wiki.jenkins-ci.org/display/JENKINS/Meet+Jenkins点击“download jenkins.war”
下载的为jenkins.zip将其更改为jenkins.war包,有两种运行方式
a. 运行 java -jar jenkins.war,采用内置jetty服务器。
b. 将下载的war包文件部署到 servlet 容器,然后启动容器(推荐这种方式)。
用浏览器打开http://localhost:8080/ jenkins,访问Jenkins主页。
8.2 修改jenkins配置
为了让
jenkins
可以在构建项目的时候执行
sonar
,所以我们需要在jenkins上安装插件 。
同时为了可以使用svn,maven,email也同样需要安装插件。
现在就可以进行配置,让jenkins和sonar结合在一起。这样我们构建项目的时候就会进行代码检测
1,系统设置
1)全局属性 -Tool Locations
2) SonarQube servers
3) Jenkins Location
3) Extended E-mail Notification
邮件配置中的密码均为,邮件的takens,直接stmp的端口,还是回不能成功,连接不到,改为ssl,端口为465。成功发送邮件。
参考 https://zhuanlan.zhihu.com/p/22810691。
4) 邮件通知
可以通过这个功能测试邮件是否可以正常使用。
用户名不带@163.com,使用ssl,465端口。在不勾选情况下,使用smtp的25端口发送失败。
2 Global Tool Configuration
点击“系统管理”页面,点击Global Tool Configuration。
增加jdk,maven,sonar-runner等环境地址。如jdk,点击新增,去掉【自动安装】的勾选。
配完后点击保存。
7.4 构建一个自由风格的项目
1)增加svn的url和需要密码验证是后输入帐号密码
2)构建触发器,3分钟触发一次
3)构建 编辑我们的项目,选择最下放。找到构建
- Not authorized. Please check the properties sonar.login and sonar.password.
4)sonarQbude必须把sonar-project.properties的内容粘贴进来。否则构建时候,会报异常,
5)构建后操作.增加一个邮件通知的管理。
转载自:
https://zhuanlan.zhihu.com/p/22810691
进入到具体的某一job中配置邮件通知
选择html
点击“add trigger”选择awyas
-
<
hr
/>
-
(本邮件是程序自动下发的,请勿回复!)
<
br
/>
<
hr
/>
-
项目名称:$PROJECT_NAME
<
br
/>
<
hr
/>
-
构建编号:test_$BUILD_NUMBER
<
br
/>
<
hr
/>
-
svn版本号:${SVN_REVISION}
<
br
/>
<
hr
/>
-
构建状态:$BUILD_STATUS
<
br
/>
<
hr
/>
-
触发原因:${CAUSE}
<
br
/>
<
hr
/>
-
构建日志地址:
<
a
href
=
“${BUILD_URL}console”
>
${BUILD_URL}console
</
a
>
<
br
/>
<
hr
/>
-
构建地址:
<
a
href
=
“$BUILD_URL”
>
$BUILD_URL
</
a
>
<
br
/>
<
hr
/>
-
变更集:${JELLY_SCRIPT,
template
=
“html”
}
<
br
/>
<
hr
/>
将构建日志压缩作为附件也添加到邮件当中发送,需要注意的是,每一次构建都会产生一个报告文档,仓库里会包含有之前构建的所有的报告文档,
八 构建项目
填写完毕后,我们点击保存
点击开始构建,则开始构建job,点击sonarQbude可以查看分析的结果。点击具体的构建记录可以看到
构建的日志。
Build History的#50,#49可以看到构建的日志。