一、概述
Sonar 是一个用于代码质量管理的开放平台。通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具。
二、安装
1.docker安装postgres
拉取postgres镜像
docker pull postgres:9.6
创建postgres实例
docker run
--name postgres_5432
-p 5432:5432
-e POSTGRES_USER=sonar
-e POSTGRES_PASSWORD=123456
postgres:9.6
2.docker安装sonarqube
docker拉取sonarqube
docker pull sonarqube:7.9-community
创建sonarqube实例
docker run
-d
--name sonarqube_9000
-p 9000:9000 --link postgres_5432
-e SONARQUBE_JDBC_URL=jdbc:postgresql://postgres_5432:5432/sonar
-e SONARQUBE_JDBC_USERNAME=sonar
-e SONARQUBE_JDBC_PASSWORD=123456
sonarqube:7.9-community
三、sonar使用
1.pom文件
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.example</groupId>
<artifactId>sonar-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.12</junit.version>
<sonar.jdbc.url>jdbc:postgresql://postgres_5432:5432/sonar</sonar.jdbc.url>
<sonar.jdbc.driver>org.postgresql.Driver</sonar.jdbc.driver>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>123456</sonar.jdbc.password>
<sonar.host.url>http://localhost:9000</sonar.host.url>
</properties>
<dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<skipTests>false</skipTests>
<testFailureIgnore>false</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>pre-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.java代码
Utils.java
public class Utils {
private Utils() {
throw new IllegalStateException("Utility class");
}
public static String getString1(String str) {
return "a" + str;
}
public static String getString2(String str) {
return "b" + str;
}
public static String getString3(String str) {
return "c" + str;
}
public static String getString4(String str) {
return "d" + str;
}
}
UtilsTest.java
public class UtilsTest {
@Test
public void testGetString1() {
String string1 = Utils.getString1("a");
Assert.assertEquals("aa", string1);
}
@Test
public void testGetString2() {
String string2 = Utils.getString2("b");
Assert.assertNotNull(string2);
}
@Test
public void testGetString3() {
String string3 = Utils.getString3("c");
Assert.assertNotNull(string3);
}
@Test
public void testGetString4() {
Assert.fail("This is fail");
}
}
3.执行maven命令
maven clean test
maven sonar:sonar
5.结果
登陆浏览器 localhost:9000/projects
查看结果