jacoco+surefire+sonar配置

<profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <buildEnv>dev</buildEnv>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <buildEnv>test</buildEnv>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <buildEnv>product</buildEnv>
            </properties>
        </profile>
        <profile>
            <id>pre</id>
            <properties>
                <buildEnv>pre</buildEnv>
            </properties>
        </profile>

        <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- mvn clean test jacoco:report install sonar:sonar -->
                <!-- mvn clean test org.jacoco:jacoco-maven-plugin:0.8.1:prepare-agent install -Dmaven.test.failure.ignore=true -->
                <!--jacoco 跑覆盖-->
                <sonar.host.url>http://sonar.example.com</sonar.host.url>
                <!-- mvn sonar:sonar 使用此命令进行提交 -->
                <!-- 全包扫描 也可以指定具体的类,使用","分割多个类或者目录 mvn sonar:sonar -->
                <sonar.inclusions>**/service/**</sonar.inclusions>
                <!--排除某些目录-->
                <sonar.exclusions>
                    **/modules/**,
                    **/common/**,
                    **/domain/**,
                    **/sdk/**,
                    **/dao/**
                </sonar.exclusions>
                <sonar.projectKey>
                    ${project.groupId}:${project.artifactId}
                </sonar.projectKey>
                <sonar.projectName>
                    ${project.artifactId}
                </sonar.projectName>
                <sonar.projectVersion>
                    ${project.version}
                </sonar.projectVersion>
            </properties>
        </profile>
    </profiles>

    <!-- 公共插件定义 -->
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <source>${jdk.version}</source>
                        <target>${jdk.version}</target>
                        <encoding>${maven.compiler.encoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.5</version>
                </plugin>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.5.5</version>
                </plugin>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.7</version>
                </plugin>
                <plugin>
                    <!-- 自动跑单元测试 -->
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M5</version>
                    <configuration>
                        <includes>
                        <!--指定要跑的单元测试(增量),全量则注释掉下面的include-->
                        </includes>
                        <excludes>
                            <!-- 排除某些包 -->
                            <exclude>**/common/**/*</exclude>
                            <exclude>**/modules/**/*</exclude>
                            <exclude>**/dao/**/*</exclude>
                            <exclude>**/domain/**/*</exclude>
                            <exclude>**/sdk/**/*</exclude>
                        </excludes>
                        <argLine>
                            @{surefireArgLine} -Dfile.encoding=UTF-8
                        </argLine>
                        <encoding>UTF-8</encoding>
                        <useSystemClassLoader>true</useSystemClassLoader>
                        <parallel>classesAndMethods</parallel>
                        <forkCount>10</forkCount>
                        <reuseForks>true</reuseForks>
                        <useUnlimitedThreads>true</useUnlimitedThreads>
                        <testFailureIgnore>true</testFailureIgnore>
                        <enableAssertions>true</enableAssertions>
                        <disableXmlReport>true</disableXmlReport>
                    </configuration>
                </plugin>
                <plugin>
                    <!-- 跑本地单元测试覆盖率,出覆盖率报告 -->
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.8.5</version>
                    <configuration>
                        <includes>
                            <!-- 全量跑service包下的单元测试 -->
                            <include>**/service/**</include>
                            <include>**/service/**/*.java</include>
                            <!-- 增量跑service包下的单元测试 -->
                            <!--<include>**/service/**/ab/**</include>-->
                            <!--<include>**/service/**/ab/*.java</include>-->
                            <!--<include>**/service/**/amsPayout/**</include>-->
                        </includes>
                        <excludes>
                            <!-- 排除某些包 -->
                            <exclude>**/common/**/*</exclude>
                            <exclude>**/modules/**/*</exclude>
                            <exclude>**/dao/**/*</exclude>
                            <exclude>**/domain/**/*</exclude>
                            <exclude>**/sdk/**/*</exclude>
                        </excludes>
                    </configuration>
                    <executions>
                        <execution>
                            <id>default-prepare-agent</id>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                            <configuration>
                                <propertyName>surefireArgLine</propertyName>
                            </configuration>
                        </execution>
                        <execution>
                            <id>default-report</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>report</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

出现下面类似异常时,执行mvn sonar:sonar 提交覆盖结果的时候报错

1、org/sonarsource/scanner/maven/SonarQubeMojo has been compiled by a more recent version of the Java Runtime;
2、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插件版本发生变更(默认情况下咋们是没有指定sonar插件版本的)
需要知道sonar插件版本,解决不兼容问题:

<plugin>
    <groupId>org.sonarsource.scanner.maven</groupId>
    <artifactId>sonar-maven-plugin</artifactId>
    <version>3.1.1</version>
</plugin>
posted @ 2024-07-11 14:49  itwetouch  阅读(13)  评论(0编辑  收藏  举报