【问题标题】:sonar-maven-plugin: Can not set sonar.sources src/main/resourcessonar-maven-plugin:无法设置 sonar.sources src/main/resources
【发布时间】:2017-04-11 21:30:05
【问题描述】:

我对 maven-sonar 设置有疑问。我需要在 src/main/javasrc/main/resources 中包含我的所有项目文件,以便 SonarQube 显示我所有文件的结果,但在 src/main/resources/static/file.对于测试路径,它已经默认设置在 src/test/java

这就是我设置 POM.XML 的方式

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->

    <!-- Sonar static analysis / Jacoco code coverage -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <start-class>com.Example.wp.WpCoreApplication</start-class>
    <java.version>1.8</java.version>
    <sonar.sources>src/main/java, src/main/resources</sonar.sources>
    <sonar.exclusions>src/main/resources/static/file</sonar.exclusions>
</properties>

<dependencies>
    <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-cassandra</artifactId> 
        </dependency> -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-neo4j</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.debatty</groupId>
        <artifactId>java-string-similarity</artifactId>
        <version>RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.11</version>
    </dependency>
</dependencies>
<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>2.7.2</version>
        </plugin>
    </plugins>
</reporting>
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.19.1</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <argLine>${jacoco.agent.argLine}</argLine>
                    <skipTests>false</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
            </plugin>
            <plugin>
                <groupId>org.sonarsource.scanner.maven</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>3.1.1</version>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.7.201606060606</version>
            <configuration>
                <dataFile>target/jacoco.exec</dataFile>
            </configuration>
            <executions>
                <execution>
                    <id>jacoco-initialize</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <propertyName>jacoco.agent.argLine</propertyName>
                        <destFile>target/jacoco.exec</destFile>
                    </configuration>
                </execution>
                <execution>
                    <id>jacoco-report</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-site-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>rpm-maven-plugin</artifactId>
            <version>2.1-alpha-3</version>
            <executions>
                <execution>
                    <id>attach-rpm</id>
                    <goals>
                        <goal>attached-rpm</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

但是当我使用命令提示符运行我的 maven-sonar 时: mvn sonar:sonar 。有这样的错误日志

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.865 s
[INFO] Finished at: 2016-11-28T14:46:40+07:00
[INFO] Final Memory: 68M/449M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:
3.1.1:sonar (default-cli) on project wp: The directory 'D:\PROJECT\wp-core\
src\main\resources' does not exist for Maven module com.sample:wp:jar:0.1.26. Pleas
e check the property sonar.sources -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
ch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please rea
d the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionE
xception

如何设置声纳以便它可以扫描我的所有文件?我已经看过documentation 和这个Stackoverflow

【问题讨论】:

    标签: java maven sonarqube pom.xml sonar-maven-plugin


    【解决方案1】:

    您可能应该删除多余的空间。改变

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

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

    【讨论】:

    • 如果这是您可能应该改进sonar-maven-plugin的解决方案;-)
    • 啊..这很棘手,当我检查它时,我发现路径有“空间”.. 我认为他们需要像@FrVaBe 所说的那样改进 sonar-maven-plugin。
    • @David Vincent - 不过,如果删除空间解决了您的问题,您应该接受(并且可能赞成)答案。
    【解决方案2】:

    sonarQube 仅需要以下与声纳相关的指定条目来进行静态代码分析。

    在 pom.xml 中的属性标签下,仅添加以下与声纳相关的条目:您可以属性。默认情况下,声纳会缩放 src 文件夹下的所有文件。

        <sonar.language>java</sonar.language>
        <sonar.exclusions>
            **/static/file/**
        </sonar.exclusions>
    

    在 pom.xml 中的 plugins 标签下,将 maven-surefire-plugin 和 jacoco-maven-plugin 条目替换为以下条目:

        <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.13</version>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.2.201409121644</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    

    让我们不要添加声纳实际上不需要的所有参数来进行静态代码分析。 你会看到,我只在属性标签中添加了和属性。 正确使用的语法是不要使用 src/main/ 这种类型的路径。如果您在 sonar.exclusions 中指定 src/main,则 sonar 不会从静态代码分析中排除文件。 我在我的项目中遇到了这个问题。

    如果您仍然遇到一些问题,请告诉我。

    【讨论】:

    • 看完你的帖子,我发现我的错误。我只需要编写 src/main 及其作品。我不知道为什么我不能像我的设置一样指定每个路径。
    • 顺便说一句,我没有使用你的 maven-surefire-plugin 和 jacoco-maven-plugin 设置,但它仍然有效。我只看了你写的End Summary,突然明白我的错误了。谢谢@Reena
    • 也许你可以编辑你的答案,并添加 src/main 因为在阅读了你的解释之后,我只需要像这样更改我的设置,它就像魔法。哈哈哈。谢谢你:)
    猜你喜欢
    • 2016-09-29
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多