2016-11-28 262 views
1

我有Maven-sonar設置的問題。我需要包括我的所有項目文件在的src/main/java的的src/main /資源使SonarQube會顯示我的所有文件的結果,但在的src /主/資源/靜態/文件排除某些文件。對於測試路徑則默認情況下在的src/test/java下sonar-maven-plugin:無法設置sonar.sources src/main/resources

已經設置這是我如何設置我的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的聲納與命令提示符:mvn聲納:聲吶。有錯誤日誌中這樣

[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

回答

3

您應該刪除額外的空間。更改

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

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

如果這是解決方案,你可能應該改進'sonar-maven-plugin' ;-) – FrVaBe

+0

啊..這是非常棘手的,當我檢查它時,我發現路徑有「空間」.. 我認爲他們需要像@FrVaBe所說的那樣改進sonar-maven-plugin。 –

+0

@大衛文森特 - 然而,如果刪除空間解決了你的問題,你應該接受(並可能upvote)答案。 – FrVaBe

1

只有sonarQube需要聲納相關的條目才能進行靜態代碼分析。

在pom.xml中的屬性標籤下,只添加以下與聲納相關的條目:您可以屬性。默認情況下,聲納將對位於src文件夾下的所有文件進行縮放。

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

下在你的pom.xml的插件標籤,具有以下條目替換您的Maven的萬無一失,插件和jacoco - Maven的插件項:

<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,則聲納不會從靜態代碼分析中排除文件。 我在我的項目中遇到過這個問題。

讓我知道你是否仍然面臨一些問題。

+0

後看了你的帖子,我發現我錯了。我只需要寫 src/main及其作品。 我不知道爲什麼我不能像我的設置一樣指定每個路徑。 –

+0

順便說一句,我沒有使用你的maven-surefire-plugin和jacoco-maven-plugin設置,但它仍然有效。我只讀了你寫的「結束總結」,突然明白我的錯誤。 謝謝你@Reena –

+0

也許你可以編輯你的答案,並添加 src/main 因爲在閱讀你的解釋之後,我只需要改變我的設置,就像那樣。 ahaha。謝謝:) –