SonarQube-Java-Sonar扫描仪未分析src/test/Java文件夹
我下载了SonarQube 7.0,并将Maven项目设置为使用Sonar scanner。pom.xml如下所示:SonarQube-Java-Sonar扫描仪未分析src/test/Java文件夹,java,maven,sonarqube,sonarqube-scan,Java,Maven,Sonarqube,Sonarqube Scan,我下载了SonarQube 7.0,并将Maven项目设置为使用Sonar scanner。pom.xml如下所示: <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>com.github.jitpack</groupId>
<artifactId>maven-simple</artifactId>
<version>0.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Simple Maven example</name>
<url>https://jitpack.io/#jitpack/maven-simple/0.1</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<mavenSurefireVersion>2.20</mavenSurefireVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<src.dir>src/main/java</src.dir>
<sonar.sources>pom.xml,src/main/java</sonar.sources>
<mavenSurefireVersion>2.20</mavenSurefireVersion>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>sonarLocal</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<sonar.host.url>http://localhost:9000</sonar.host.url>
</properties>
</profile>
</profiles>
<build>
<sourceDirectory>${src.dir}</sourceDirectory>
<testResources>
<testResource>
<directory>${project.basedir}</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.source}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<configuration>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<configLocation>google_checks.xml</configLocation>
<encoding>${project.build.sourceEncoding}</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<failOnViolation>true</failOnViolation>
<violationSeverity>warning</violationSeverity>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${mavenSurefireVersion}</version>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.4.0.905</version>
</plugin>
</plugins>
</build>
4.0.0
com.github.jitpack
马文简单
0.2-1快照
罐子
简单的Maven示例
https://jitpack.io/#jitpack/maven-简单/0.1
1.8
1.8
2.20
UTF-8
src/main/java
xml,src/main/java
2.20
org.testng
testng
6.8
测试
声纳本地
假的
http://localhost:9000
${src.dir}
${project.basedir}
真的
maven编译器插件
3.3
${maven.compiler.source}
${maven.compiler.source}
${project.build.sourceEncoding}
org.apache.maven.plugins
maven资源插件
3.0.2
${project.build.sourceEncoding}
org.apache.maven.plugins
maven checkstyle插件
3.0.0
验证
验证
真的
google_checks.xml
${project.build.sourceEncoding}
真的
真的
真的
警告
检查
org.apache.maven.plugins
maven surefire插件
${MAV恢复}
org.sonarsource.scanner.maven
声纳maven插件
3.4.0.905
我的项目结构如下所示:
src/main/java-->包含应用程序代码
src/test/java-->包含所有测试代码
我使用命令“mvncleanstallsonar:sonar-PsonarLocal”来执行sonar
现在我注意到Sonar从未分析src/test/java文件夹中的所有.java文件
为了证明这一点,我在其中一个测试类中添加了一个未使用的变量,Sonar没有捕捉到它。如果我在src/main/java文件夹中的任何文件中执行相同的操作,Sonar就会捕获它
有什么我遗漏的吗?您希望对测试应用“正常”规则。今天情况并非如此。相反,只应用测试特定规则的子集。尝试标记测试@Ignore
d。那么你应该看到一个问题
在不进行其他分析的情况下进行双重检查的一种方法是查看项目的代码页。您应该会看到这里列出的测试文件,如果您钻取到其中一个文件,则应该能够从文件查看器右上角的“汉堡菜单”图标访问特定于文件的度量
目前,要对源文件运行常规规则,您需要进行第二次单独的分析(确保使用
-Dsonar.projectKey
输入不同的项目密钥,或者覆盖常规分析),并将测试目录指定为源(sonar.sources=relative/path/to/tests
).使用以下命令,您可以在同一Sonar项目中分析src/main/java
和src/test/java
中的代码:
mvn clean org.jacoco:jacoco maven插件:准备代理安装-Dmaven.test.failure.ignore=false
mvn sonar:sonar-Dsonar.host.url=http://localhost:9000 -Dsonar.login=(或任何需要身份验证的主机url)。替换为有效的令牌
然而,我建议在单独的Sonar项目中扫描src/test/java
代码,因为在扫描测试代码和应用程序代码时,代码覆盖率等指标会被忽略(因为Sonar会报告所有测试类本身的覆盖率为0%)。要单独扫描:
对于应用程序代码(src/main/java
),请运行:
mvn sonar:sonar-Dsonar.host.url=http://localhost:9000 -Dsonar.login=
对于测试代码(src/test/java
),运行:
mvn sonar:sonar-Dsonar.projectKey=-tests-Dsonar.host.url=http://localhost:9000 -Dsonar.login=-Dsonar.sources=src/test-Dsonar.test.includes=src/test/java/*
使用项目名称更新-Dsonar.projectKey=-tests
,以便为测试扫描提供唯一的项目密钥
对于这种方法,不需要在pom.xml
中添加特定于Sonar/JaCoCo的内容
请注意,无论何时对代码进行更新,都必须在mvn sonar:sonar…
之前使用JaCoCo执行mvn clean
(上述步骤1)
我使用SonarQube 6.6和Sonar Scanner 3.0.3.778对此进行了测试。你好,G.安,我在Maven项目中使用了TestNG。当我尝试添加
@Ignore
注释时,它显示import org.junit.Ignore代码>需要导入。这样做对吗?如果没有,我如何为TestNG测试添加ignore?请忽略我的上述评论。当我添加@Ignore
时,我看到声纳中出现了错误错误。我得到的错误是“修复或删除此跳过的单元测试”,而不是显示未使用的变量。有什么方法可以在测试中运行声纳分析吗?请参阅我的扩展@JohnSmith。好的,我也尝试过实现它。我输入命令,mvn clean install sonar:sonar-PsonarLocal-Dsonar.projectKey=com.github.jitpack:maven simple-Dsonar,sources=src/test/java/com/github/jitpack
,它仍然没有扫描tests文件夹。我做错什么了吗?-Dsonar,sources
或-Dsonar.sources
?