如何在詹金斯中制作 jacoco.exec 文件

how to make jacoco.exec file in jenkins

我正在尝试在 jenkins + nexus + eclipse 中使用 jacoco。

但 jacoco 结果只有 0%。

下方代码为pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.github.myeongjoon</groupId>
  <artifactId>testPage</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>testPage Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- spring dependency -->

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.0.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.0.RELEASE</version>
    </dependency>

    <!-- jstl -->
    <dependency>
       <groupId>jstl</groupId>
       <artifactId>jstl</artifactId>
       <version>1.2</version>
    </dependency>
    <!-- jstl -->

    <!-- jsp dependency -->
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging-api</artifactId>
        <version>1.1</version>
        <exclusions>
            <exclusion>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>


    <!-- spring dependency -->

    <!-- jacoco report dependency -->


    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-provider-api</artifactId>
        <version>1.0-alpha-6</version>
    </dependency>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.0.4</version>
    </dependency>


    <!-- jacoco dependency -->


    <dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.7.201606060606</version>
    </dependency>
    <!-- jacoco dependency -->


  </dependencies>
  <build>
    <finalName>testPage</finalName>


  <!-- jacoco plugin -->


  <plugins>

  <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <configuration>
                <argLine>-Denv=local -Dlogback.configurationFile=logger/local-logback.xml</argLine>
                <excludedGroups>int</excludedGroups>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <!-- property set by jacoco-maven-plugin -->
                <argLine>${itCoverageAgent}</argLine>
                <groups>int</groups>
                <!-- by default only IT*, *IT and *ITCase classes are included -->
                <includes>
                    <include>**/*.java</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

                <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>   

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.7.201606060606</version>
            <executions>
                <!-- prepare agent for measuring unit tests -->
                <execution>
                    <id>prepare-unit-tests</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <destFile>${sonar.jacoco.reportPath}</destFile>
                    </configuration>
                </execution>

                <!-- prepare agent for measuring integration tests -->
                <execution>
                    <id>prepare-integration-tests</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <phase>pre-integration-test</phase>
                    <configuration>
                        <destFile>${sonar.jacoco.itReportPath}</destFile>
                        <propertyName>itCoverageAgent</propertyName>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    <!-- 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>








            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.7.201606060606</version>
                <configuration>
                    <fileSets>
                        <fileSet
                                implementation="org.apache.maven.shared.model.fileset.FileSet">
                            <directory>./..</directory>
                            <includes>
                                <include>*.exec</include>
                            </includes>
                        </fileSet>
                    </fileSets>
                    <destFile>jacoco.exec</destFile>

                    <dataFile>jacoco.exec</dataFile>
                </configuration>
            </plugin>
             -->
        </plugins>


  <!-- jacoco plugin -->

  </build>



  <pluginRepositories>
    <pluginRepository>
        <id>KCC_Central</id>
        <url>http://192.168.56.1:8081/repository/KCC_Central/</url>
        <releases><enabled>true</enabled></releases>
        <snapshots><enabled>true</enabled></snapshots>
    </pluginRepository>
</pluginRepositories>


</project>

下图是jenkins中的jacoco设置

下面的代码是 jacoco return 只有 0% 的覆盖率。

我认为 jenkins 不能生成 *.exec 文件。

所以post构建结果只有'find 0 exec file'。

我该怎么办?

我通过在 tomcat

中的 catalina.sh 添加代码解决了这个问题

代码:

JAVA_OPT=" -javaagent: {javaagent.jar location}"