sonar使用说明

sonar使用介绍

1.通过maven插件(主要方式支持子模块)

  • 配置私有仓库 pom.xml (可省略)
         <repositories>
             <repository>
                 <id>nexus-releases</id>
                 <name>Nexus Release Repository</name>
                 <url>http://ip:8081/repository/maven-releases/</url>
             </repository>
             <repository>
                 <id>nexus</id>
                 <name>Nexus Repository</name>
                 <url>http://ip:8081/repository/maven-public/</url>
                 <snapshots>
                     <enabled>true</enabled>
                 </snapshots>
                 <releases>
                     <enabled>true</enabled>
                 </releases>
             </repository>
             <repository>
                 <id>aliyun-repos</id>
                 <name>Aliyun Repository</name>
                 <url>http://maven.aliyun.com/nexus/content/groups/public</url>
                 <releases>
                     <enabled>true</enabled>
                 </releases>
                 <snapshots>
                     <enabled>false</enabled>
                 </snapshots>
             </repository> 
             <repository>
                 <id>sonatype-repos</id>
                 <name>Sonatype Repository</name>
                 <url>https://oss.sonatype.org/content/groups/public</url>
                 <releases>
                     <enabled>true</enabled>
                 </releases>
                 <snapshots>
                     <enabled>false</enabled>
                 </snapshots>
             </repository>
             <repository>
                 <id>sonatype-repos-s</id>
                 <name>Sonatype Repository</name>
                 <url>https://oss.sonatype.org/content/repositories/snapshots</url>
                 <releases>
                     <enabled>false</enabled>
                 </releases>
                 <snapshots>
                     <enabled>true</enabled>
                 </snapshots>
             </repository>
             <repository>
                 <id>spring-snapshots</id>
                 <name>Spring Snapshots</name>
                 <url>https://repo.spring.io/snapshot</url>
                 <snapshots>
                     <enabled>true</enabled>
                 </snapshots>
             </repository>
             <repository>
                 <id>spring-milestones</id>
                 <name>Spring Milestones</name>
                 <url>https://repo.spring.io/milestone</url>
                 <snapshots>
                     <enabled>false</enabled>
                 </snapshots>
             </repository>
         </repositories>
         <distributionManagement>
             <repository>
                 <id>nexus-releases</id>
                 <name>Nexus Release Repository</name>
                 <url>http://ip:8081/repository/maven-releases/</url>
             </repository>
             <snapshotRepository>
                 <id>nexus-snapshots</id>
                 <name>Nexus Snapshot Repository</name>
                 <url>http://ip:8081/repository/maven-snapshots/</url>
             </snapshotRepository>
         </distributionManagement>
         <pluginRepositories>
             <pluginRepository>
                 <id>nexus</id>
                 <name>Nexus Plugin Repository</name>
                 <url>http://ip:8081/repository/maven-public/</url>
                 <snapshots>
                     <enabled>true</enabled>
                 </snapshots>
                 <releases>
                     <enabled>true</enabled>
                 </releases>
             </pluginRepository>
             <pluginRepository>
                 <id>aliyun-repos</id>
                 <name>Aliyun Repository</name>
                 <url>http://maven.aliyun.com/nexus/content/groups/public</url>
                 <releases>
                     <enabled>true</enabled>
                 </releases>
                 <snapshots>
                     <enabled>false</enabled>
                 </snapshots>
             </pluginRepository>
         </pluginRepositories>
    
  • 配置覆盖率插件
    • 如果项目包含子项目则在子项目pom.xml添加插件
              <plugins>
                  <plugin>
                      <groupId>org.jacoco</groupId>
                      <artifactId>jacoco-maven-plugin</artifactId>
                      <configuration>
                          <destFile>${project.basedir}/../target/jacoco-it.exec</destFile>
                      </configuration>
                  </plugin>
               </plugins>
      
      在父级项目pom.xml添加配置
                <properties>
                    <sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-it.exec</sonar.jacoco.itReportPath>
                    <sonar.groovy.binaries>target/classes</sonar.groovy.binaries>
                    <sonar.java.binaries>target/classes</sonar.java.binaries> 
                <properties>
                <profiles>
                       <profile>
                           <id>sonar-coverage</id>
                           <activation>
                               <activeByDefault>true</activeByDefault>
                           </activation>
                           <build>
                               <pluginManagement>
                                   <plugins>
                                       <plugin>
                                           <groupId>org.jacoco</groupId>
                                           <artifactId>jacoco-maven-plugin</artifactId>
                                           <version>0.7.8</version>
                                       </plugin>
                                   </plugins>
                               </pluginManagement>
                               <plugins>
                                   <plugin>
                                       <groupId>org.jacoco</groupId>
                                       <artifactId>jacoco-maven-plugin</artifactId>
                                       <configuration>
                                           <append>true</append>
                                       </configuration>
                                       <executions>
                                           <execution>
                                               <id>agent-for-ut</id>
                                               <goals>
                                                   <goal>prepare-agent</goal>
                                               </goals>
                                           </execution>
                                           <execution>
                                               <id>agent-for-it</id>
                                               <goals>
                                                   <goal>prepare-agent-integration</goal>
                                               </goals>
                                           </execution>
                                           <execution>
                                               <id>jacoco-site</id>
                                               <phase>verify</phase>
                                               <goals>
                                                   <goal>report</goal>
                                               </goals>
                                           </execution>
                                       </executions>
                                   </plugin>
                                   <plugin>
                                       <groupId>org.apache.maven.plugins</groupId>
                                       <artifactId>maven-surefire-plugin</artifactId>
                                       <configuration>
                                           <argLine>-Dfile.encoding=UTF-8</argLine>
                                       </configuration>
                                   </plugin>
                               </plugins>
                           </build>
                       </profile>
                </profiles>
      
    • 如果项目不包含子项目则在项目pom.xml添加插件
                <properties>
                    <sonar.jacoco.itReportPath>${project.basedir}/target/jacoco-it.exec</sonar.jacoco.itReportPath>
                    <sonar.java.binaries>target/classes</sonar.java.binaries>
                <properties>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>org.jacoco</groupId>
                            <artifactId>jacoco-maven-plugin</artifactId>
                            <version>0.7.8</version>
                            <configuration>
                                <append>true</append>
                            </configuration>
                            <executions>
                                <execution>
                                    <id>agent-for-ut</id>
                                    <goals>
                                        <goal>prepare-agent</goal>
                                    </goals>
                                </execution>
                                <execution>
                                    <id>agent-for-it</id>
                                    <goals>
                                        <goal>prepare-agent-integration</goal>
                                    </goals>
                                </execution>
                                <execution>
                                    <id>jacoco-site</id>
                                    <phase>verify</phase>
                                    <goals>
                                        <goal>report</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-surefire-plugin</artifactId>
                            <version>2.5</version>
                            <configuration>
                                <argLine>-Dfile.encoding=UTF-8</argLine>
                            </configuration>
                        </plugin>
                    </plugins>
                </pluginManagement>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <configuration>
                        <destFile>${project.basedir}/target/jacoco-it.exec</destFile>
                    </configuration>
                </plugin>
    
  • 配置maven配置文件
    • 在setting.xml 添加
      <profile>
           <id>sonar</id>
           <activation>
              <activeByDefault>true</activeByDefault>
           </activation>
           <properties>
              <sonar.jdbc.url>jdbc:mysql://ip:3306/sonar</sonar.jdbc.url>
              <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
              <sonar.jdbc.username>xx</sonar.jdbc.username>
              <sonar.jdbc.password>xx</sonar.jdbc.password>
              <sonar.host.url>http://ip:9999</sonar.host.url> <!-- Sonar服务器访问地址 -->
          </properties>
       </profile>
    
  • 运行
  mvn clean package sonar:sonar

2.通过sonar-scanner(单项目不支持子模块)

  • 从sonar官网下载Scanner
  • 修改sonar-scanner.properties
    sonar.host.url=http://ip:9999(sonar的ip地址)
    sonar.sourceEncoding=UTF-8
    sonar.jdbc.url=jdbc:mysql://ip:3306/sonar?useUnicode=true&amp;characterEncoding=utf8   
    sonar.jdbc.username=xx
    sonar.jdbc.password=xx
    sonar.login=xx  
    sonar.password=xx
    
  • 在要检测的项目根目录下新建sonar-project.properties
    # must be unique in a given SonarQube instance
    sonar.projectKey=your_project(自己定义)
    # this is the name displayed in the SonarQube UI
    sonar.projectName=your_project(自己定义)
    sonar.projectVersion=1.0(自己定义)
    # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
    # Since SonarQube 4.2, this property is optional if sonar.modules is set. 
    # If not set, SonarQube starts looking for source code from the directory containing 
    # the sonar-project.properties file.
    sonar.sources=src(代码目录)
    sonar.java.binaries=target(classes文件目录,不加会报错)
    sonar.language=java
    # Encoding of the source code. Default is default system encoding
    sonar.sourceEncoding=UTF-8
    
  • 输入sonar-scanner运行
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值