Sonar+IDEA + Maven的集成

理由:因新搭建了一个项目,完成之后需要检测代码进行评优。

过程:

1、环境配置: IDEA 2019 + Maven3.3.9 + sonarqube7.7 + JDK1.8 + MySQL5.7

2、参考链接:

>>> https://blog.csdn.net/weixin_40861707/article/details/82117232  >>>https://blog.csdn.net/wojiushiwo945you/article/details/100699885https://blog.csdn.net/wojiushiwo945you/article/details/100699885

两个结合一起查看,信息已经很全了。

3、配置过程:

sonar.propertis文件

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.username=root
sonar.jdbc.password=root 
sonar.sorceEncoding=UTF-8

maven setting.xml文件

<profile>
        <id>sonar</id>
            <properties>
            <sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar</sonar.jdbc.url>
            <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
            <sonar.jdbc.username>root</sonar.jdbc.username>
            <sonar.jdbc.password>root</sonar.jdbc.password>
            <sonar.host.url>http://localhost:9000</sonar.host.url>
        </properties>
    </profile>

<activeProfiles> 
  <activeProfile>sonar</activeProfile> 

</activeProfiles>
View Code

IDEA 项目 pom.xml文件

<plugin>
                <groupId>org.sonarsource.scanner.maven</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>3.1.1</version>
            </plugin>

  

4、运行命令:mvn clean install -X -Dmaven.test.skip sonar:sonar (-X可查看运行日志)

5、自己碰到的问题:

<1> sonarQube服务端安装成功后,启动过程中自动退出,并报错

原因:sonarQube对数据库和JDK不同版本的兼容性,5.6 <= MySQL版本 < 8, SQLServer > 2012

可参考https://www.cnblogs.com/xingyunblog/p/9947854.html

<1>[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.4.0.905:sonar (default-cli) on project practise: Unable to execute SonarQube:  

Caused by: java.lang.UnsupportedClassVersionError: org/sonarsource/scanner/maven/SonarQubeMojo : Unsupported major.minor version 52.0

原因:sonar插件版本不对。

正确:

<plugin>
                <groupId>org.sonarsource.scanner.maven</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>3.1.1</version>
</plugin>

错误:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>sonar-maven-plugin</artifactId>
    <version>3.7.1</version>
</plugin>

<2> maven编译报错

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project report: F
atal error compiling: 无效的标记: -parameters -> [Help 1]

原因:是maven运行编译时使用的JDK版本不一样导致的,这个时候需要排查一下你编辑器中项目所配置的JDK版本是什么,配置成一样的就可以了。

另外问题:我自己的本地有两个JDK1.7、JDK1.8,项目配置的是JDK1.8,在IDEA Terminal下运行命令,一直再报编译错误,网上查询所有的都是版本不兼容导致的,

自己检查了所有的JDK配置:

  • 编辑器中项目指定的运行、编译JDK都更改为1.8
  • 电脑环境配置也是JDK1.8
  • maven配置文件setting.xml配置为1.8

IDEA中执行命令一直使用JDK1.7,在CMD DOS命令窗口执行则显示JDK1.8,很奇怪,还没有发现问题,索性先使用DOS执行命令。

<3> org.tmatesoft.svn.core.SVNException: svn: E175002:

原因: SVN没有连接上。

因为我们项目开发时内外网分离,且使用的时SVN管理,所以切换到内网上就可以连接上SVN,再执行一次命令就OK了。

#手是好汉,眼是懒蛋,记录随笔,愿有帮助#

猜你喜欢

转载自www.cnblogs.com/alvin-perfect/p/11776245.html
今日推荐