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

前端 未结 10 415
深忆病人
深忆病人 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:16

    Update the POM.xml as

    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.7.201606060606</version>
        <executions>
          <execution>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
                <id>report</id>
                <phase>prepare-package</phase>
                <goals>
                  <goal>report</goal>
                </goals>
          </execution>
        </executions>
    </plugin>
    
    <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.12.1</version>
          <configuration>
             <argLine>${argLine} -XX:PermSize=256m -XX:MaxPermSize=1048m</argLine> 
          </configuration>
    </plugin>
    

    and then the most important thing is to run the Maven project with goals: mvn jacoco:prepare-agent clean test jacoco:report

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

    Try using

    @{argLine}
    

    instead of

    ${argLine}
    

    (or surefire.argLine in your case)

    It allows surefire to read a property as modified by other plugins instead of reading the one substituted by Maven itself. Then you can set the argLine param to empty in Maven properties:

    <properties>
        <argLine></argLine>
    </properties>
    

    Which now will not cause any problems. More here: How do I use properties set by other plugins in argLine?

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

    I recently ran into the same issue and even took implicitly the same steps as you described with the same result. No clean solution I found worked for me.

    So I ran several steps in debug mode and it seems that Maven replaces properties twice. That is not just in a lazy manner, as I thought, but in both eager and lazy manner:

    1. eagerly (before any goal is run) are replaced static properties (defined in properties section of POM and probably also settings.xml),
    2. lazily (before each execution) are replaced dynamic properties.

    This is where our step with setting a blank property as a default failed. Maven just went:

    1. eager replace of default value (blank)
    2. JaCoCo sets dynamic value
    3. lazy replace of dynamic values (nothing to replace now, already used the blank value)

    Finally the solution is to set the default value dynamically. This can be done with GMaven plugin like this:

    <plugin>
      <groupId>org.codehaus.gmaven</groupId>
      <artifactId>gmaven-plugin</artifactId>
      <version>1.5</version>
      <executions>
        <execution>
          <id>set-default-values</id>
          <phase>initialize</phase>
          <goals>
            <goal>execute</goal>
          </goals>
          <configuration>
            <source>
              project.properties.'surefire.argLine' = ''
            </source>
          </configuration>
        </execution>
      </executions>
    </plugin>
    

    So now Maven goes:

    1. eager replace of static properties
    2. GMaven dynamically sets default value (if profile is active)
    3. JaCoCo sets dynamic value
    4. Surefire runs with correctly set argLine

    With active profile the exec file is generated, with non-active profile the blank default value is used and build succeeds.

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

    My solution to use argLine in the maven-surefire-plugin safely.

    <plugin>
        <groupId>org.codehaus.groovy.maven</groupId>
        <artifactId>gmaven-plugin</artifactId>
        <version>2.0</version>
        <executions>
            <execution>
                <id>set-custom-arg-line</id>
                <phase>validate</phase>
                <goals>
                    <goal>execute</goal>
                </goals>
                <configuration>
                    <source>
                        def argLine = project.properties['argLine'];
                        if (argLine == null) {
                            argLine = "";
                        }
                        project.properties.argLine = argLine;
                    </source>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
            <argLine>-Xmx1024m ${argLine}</argLine>
        </configuration>
    </plugin>
    
    0 讨论(0)
提交回复
热议问题