Sonar+Maven+Idea+Jenkins 持续测试

安装部署

server (sonarqube) +cli(oldversion:runner newversion:scanner)

本地使用(本机同时作为server&client)

  1. 下载解压sonarQube7.3,sonarscanner2.8 (可适配mysql5.7.29+jdk1.8)
  2. 创建数据库:sonar
  3. add env vriables :
    SONAR_HOME=your sonar install dir ;
    SONAR_SCANNER_HOME=your sonar_scanner install dir ;
  4. sonar.properties
sonar.jdbc.username=XXXXXXXXXXXXXXXX
sonar.jdbc.password=XXXXXXXXXXXXXXXX
sonar.jdbc.url=jdbc:mysql//XXXXXXXXXXXXXXXX
sonar.web.port=9000
  1. sonar-scanner.properties
sonar.host.url=https://localhost:9000
sonar.sourceEncoding=UTF-8
sonar.jdbc.username=XXXXXXXXXXXXX
sonar.jdbc.password=XXXXXXXXXXXXX
sonar.jdbc.url=jdbc:mysql://XXXXXXXXXXXXX
  1. run D:\sonarqube-7.3\bin\your ops sys\StartSonar.bat
  2. visit http://localhost:9000
  3. sonar-project.properties(create property file yourself in your project(to be analyzed) root dir)
# must be unique in a given SonarQube instance
sonar.projectKey=XXXXXXX

sonar.projectName=XXXXXXX
sonar.projectVersion=1.8.0.0
sonar.login=admin
sonar.password=admin

sonar.sources=src/main/java
sonar.java.binaries=./target/classes

sonar.tests=src/test
sonar.sourceEncoding=UTF-8

sonar.dynamicAnalysis=reuseReports
sonar.jacoco.reportPath=jacoco.exec
sonar.surefire.reportsPath=tests/test-reports

intergrate Jenkins

  1. add pom dependencies :
        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.8</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>3.2</version>
        </dependency>

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>3.2</version>
            </plugin>
            
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>false</skip>
                </configuration>
            </plugin>
<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.8</version>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>

                <configuration>
                    <includes>
                        <include>**/services/**</include>
                    </includes>
                    <excludes>
                        <exclude>**/model/**</exclude>
                        <exclude>**/constant/**</exclude>
                        <exclude>**/util/**</exclude>
                    </excludes>
                    <rules>
                        <rule implementation="org.jacoco.maven.RuleConfiguration">
                            <element>BUNDLE</element>
                            <limits>
                                <limit implementation="org.jacoco.report.check.Limit">
                                    <counter>METHOD</counter>
                                    <value>COVEREDRATIO</value>
                                    <minimum>0.80</minimum>
                                </limit>
                                <limit implementation="org.jacoco.report.check.Limit">
                                    <counter>INSTRUCTION</counter>
                                    <value>COVEREDRATIO</value>
                                    <minimum>0.80</minimum>
                                </limit>
                                <limit implementation="org.jacoco.report.check.Limit">
                                    <counter>LINE</counter>
                                    <value>COVEREDRATIO</value>
                                    <minimum>0.80</minimum>
                                </limit>
                                <limit implementation="org.jacoco.report.check.Limit">
                                    <counter>CLASS</counter>
                                    <value>MISSEDCOUNT</value>
                                    <maximum>0</maximum>
                                </limit>
                            </limits>
                        </rule>
                    </rules>
                </configuration>
            </plugin>
  1. push code to git repo
  2. ‘build now’ on jenkins : (auto pull code ---- run tests — execute sonar analysis — push report to sonar server )
    have to set sonar server in jenkins project;
    have to make sure project key same as project on sonar server
    在这里插入图片描述

issue solving

issue desc: surefire didnt stop even if tests done and build finished. so here runs many java process on jenkins server , and caused the resource problems.

reason : unknown.

action : add post step on jenkins to stop surefire processes manually.

#!/bin/bash
echo "Stopping surefire after sonar done... ..."
pid=`ps -ef | grep surefire | grep -v grep | grep -v jenkins.war | awk '{print $2}'`
if [ -n "$pid" ]
then
   kill -9 $pid
fi
echo "Surefire Stopped."
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值