Cannot use jacoco JVM args and surefire JVM args together in maven

前端 未结 10 414
深忆病人
深忆病人 2020-11-30 01:48

I am using maven with the jacoco plugin to generate code coverage metrics. I am having some difficulty in configuring the surefire plugin with the java opt

相关标签:
10条回答
  • 2020-11-30 02:08

    My solution is to use multiple profiles.

    The first profile sets a blank value for the surefire.argLine and the failsafe.argLine and is active by default.

    <profile>
        <id>not-sonar</id>
        <properties>
            <surefire.argLine/>
            <failsafe.argLine/>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    

    The second profile has the jacoco plugin configuration and is inactive by default.

    <profile>
    <id>sonar</id>
    <activation>
        <activeByDefault>false</activeByDefault>
    </activation>
    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco-maven-plugin-version}</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <propertyName>surefire.argLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-prepare-agent-integration</id>
                        <goals>
                            <goal>prepare-agent-integration</goal>
                        </goals>
                        <configuration>
                            <propertyName>failsafe.argLine</propertyName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    </profile>
    

    When you activate the sonar profile the not-sonar profile will automatically be turned off.

    This should be a little more elegant than using other plugins to do the work for you. You can now use the ${surefire.argLine} variable in your argLine and it will always exists and be set when you run your build.

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <argLine>${surefire.argLine} -XX:MaxPermSize=512m</argLine>
        </configuration>
      </plugin>
    

    If you still have problems because the ${surefire.argLine} does not have a value you can also set a dummy property like so:

    <profile>
        <id>not-sonar</id>
        <properties>
            <surefire.argLine>-DdummyProperty=notUsed</surefire.argLine>
            <failsafe.argLine>-DdummyProperty=notUsed</failsafe.argLine>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    
    0 讨论(0)
  • Try adding the argLine property in the properties section (as shown in the code below) instead of adding it in the configuration section of maven-sure-fire plugin. Jacoco maven plugin will just append to this and things will work as expected.

    <properties>
      <argLine>-XX:MaxPermSize=512m</argLine>
    </properties>
    

    See https://docs.sonarqube.org/display/PLUG/Usage+of+JaCoCo+with+Java+Plugin

    0 讨论(0)
  • 2020-11-30 02:11

    I have added a Maven/Java project with 1 domain class with the following features:

    • Unit or Integration testing with the plugins Surefire and Failsafe.
    • Findbugs.
    • Test coverage via Jacoco.

    I kept the project as simple as possible. The project puts many suggestions from these and other posts together in an example project. Thank you, contributors!

    The readme file gives a brief explanation. It explains how you can run either a user or an integration test with Jacoco.

    Enjoy!

    0 讨论(0)
  • 2020-11-30 02:13

    For me upgrading the version from 0.7.7.201606060606 to 0.7.9 also fixed this.

    I had to explicitly add the version to the commandline (not just to the pom) because the build server kept using the old verison. This can be done as follows:

     org.jacoco:jacoco-maven-plugin:0.7.9:prepare-agent
    

    instead of

    org.jacoco:jacoco-maven-plugin:prepare-agent
    

    The jacoco plugin site (for sonar) states that argline must be added as a property. For me it also worken when using the @{argLine} in the surefire plugin settings.

    0 讨论(0)
  • 2020-11-30 02:14

    Since the jacoco-maven-plugin:prepare-agent goal executes before the maven-surefire-plugin, try adding the ${argLine} variable into the argLine value set by the maven-surefire-plugin.

    Example:

    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.12.1</version>
      <configuration>
        <argLine>-server -ea -XX:MaxPermSize=256m -Xmx4g -XX:-UseSplitVerifier ${argLine}</argLine>
      </configuration>
    </plugin>
    

    I had the same problem and this solution worked for me, without any need to reconfigure other sections of the POM.

    0 讨论(0)
  • 2020-11-30 02:16

    If your project already uses the argLine to configure the surefire-maven-plugin, be sure that argLine defined as a property, rather than as part of the plugin configuration. For example:

      <properties>
        <argLine>-your -extra -arguments</argLine>
      </properties>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <!-- Do not define argLine here! -->
        </configuration>
      </plugin>
    

    Resulting coverage information is collected during execution and by default written to a file when the process terminates.

    Worked for me. See: http://www.eclemma.org/jacoco/trunk/doc/prepare-agent-mojo.html

    0 讨论(0)
提交回复
热议问题