【问题标题】:SonarQube doesn't analyze the main java code with IntelliJ and MavenSonarQube 不使用 IntelliJ 和 Maven 分析主要的 java 代码
【发布时间】:2019-03-12 03:22:12
【问题描述】:

我在我的项目中使用 IntelliJ 和 Maven,我必须使用 SonarQube 对其进行分析,但是当我使用 webApp 提供的命令扫描我的项目时,它只分析 pom.xml 文件而忽略了项目的其余部分。

Analysis results

clean install sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login="这里的对应键"

我的 pom.xml(项目标签里面是什么):

<groupId>cl.itau.simulacionCredito</groupId>
<artifactId>SimulacionCreditoIntelliJ</artifactId>
<version>1.0</version>
<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.beust/jcommander -->
    <dependency>
        <groupId>com.beust</groupId>
        <artifactId>jcommander</artifactId>
        <version>1.72</version>
    </dependency>
</dependencies>
<properties>
    <maven.compiler.source>6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
                <excludes>
                    <exclude>**/HomeTest.java</exclude>
                    <exclude>**/PropuestaTest.java</exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>3.0.0-M1</version>
        </plugin>
        <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>3.5.0.1254</version>
        </plugin>
    </plugins>
</build>

<distributionManagement>
    <repository>
        <id>SimulacionCredito</id>
        <name>SimulacionCredito</name>
        <url>file:C:\Users\Pc\IdeaProjects</url>
    </repository>
</distributionManagement>

我也尝试在配置标签中使用这个:

<sonar.sources>src/main/java</sonar.sources> 

这发生了:

Second analysis

我在 Maven 的 conf 中的 settings.xml:

<pluginGroups>
    <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
  </pluginGroups>

  <proxies>
  </proxies>

  <servers>
  </servers>

  <mirrors>
  </mirrors>

  <profiles>
    <profile>
       <id>sonar</id>
       <activation>
           <activeByDefault>true</activeByDefault>
       </activation>
       <properties>
           <sonar.host.url>
             http://localhost:9000
           </sonar.host.url>
        </properties>
     </profile>
  </profiles>

我一直在向 POM 添加内容,因为在尝试修复此问题时出现了不同的错误。

我安装了最新版本的 SonarQube。

【问题讨论】:

  • 您是否还在本地主机上的 SonarQube 安装中安装了 Java 分析器? (我不记得它是否默认存在,或者您是否必须在安装时安装/启用它,以便在它分析的项目中使用......)
  • @moilejter SonarJava 默认情况下它就在那里(我不得不更新它)
  • 能否请您在启用调试 (-X) 的情况下发布来自 maven 的输出?

标签: java maven intellij-idea sonarqube


【解决方案1】:

Sonarqube 通过分析 jacoco 输出来工作。我在你的 pom.xml 中没有看到任何 jacoco 的配置。 https://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin/ 是一个很好的设置教程。

您还必须告诉 sonarqube 您的 jacoco.exec 文件在哪里。 https://docs.sonarqube.org/display/PLUG/Code+Coverage+by+Unit+Tests+for+Java+Project

【讨论】:

  • SonarQube 可以分析比 JaCoCo 报告更多的内容,它还可以分析源代码。覆盖率只是众多指标之一。
【解决方案2】:

所以,问题在于 IntelliJ 配置了 Java SE Development Kit 11,我降级到 Java SE Development Kit 8u181 并配置了 POM,因此 Maven 可以使用该更改,我再次执行扫描程序并且它工作正常。

对 pom.xml 的更改:

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

    <sonar.sources>src/main/java</sonar.sources>
</properties>

添加:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
         <version>3.8.0</version>
         <configuration>
            <source>8</source>
            <target>8</target>
         </configuration>
</plugin>

New analysis results

【讨论】:

    猜你喜欢
    • 2019-11-08
    • 2018-12-08
    • 2011-08-22
    • 2017-04-29
    • 2018-09-07
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 2018-10-05
    相关资源
    最近更新 更多