Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass JVM args to maven surefire, conditional on build Java version?

Is there a way that I can pass this argLine configuration to the maven-surefire plugin ONLY when <jdk.version>1.7</jdk.version> is configured for Java 1.7 but NOT when a user changes the pom.xml to be configured for java 1.8?

The reason being that Java 1.8 doesn't have permgen space.

<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
like image 599
djangofan Avatar asked Mar 16 '16 18:03

djangofan


People also ask

What is Argline in POM XML?

Java command line when Run/Debug configuration is launched, the Surefire argline defined in maven pom. xml are automatically appended. This causes issues when the properties need to be overridden by specifying a system property in the VM options of the Run/Debug configuration.

What is perCoreThreadCount?

Note that the perCoreThreadCount is a "multiplier" of a sorts. With this parameter set to true (the default value), you will get the specified number of threads per CPU core. Also note that in this specific case threadCountSuites=2 has the same effect as threadCount=2 , since we are using parallel=suites .

Why do I need maven surefire plugin?

The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It generates reports in two different file formats: Plain text files (*.


2 Answers

Rather than a property, you should use the JDK based activation.

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample</groupId>
    <artifactId>sample-project</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <jdk.version>1.7</jdk.version>
    </properties>

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

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>surefire-java7</id>
            <activation>
                <jdk>(,1.8)</jdk>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.19.1</version>
                        <configuration>
                            <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>surefire-java8</id>
            <activation>
                <jdk>1.8</jdk>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.19.1</version>
                        <configuration>
                            <argLine>-Xmx1024m</argLine>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

Cf maven documentation. http://maven.apache.org/guides/introduction/introduction-to-profiles.html http://maven.apache.org/enforcer/enforcer-rules/versionRanges.html

like image 174
Skeebl Avatar answered Sep 16 '22 16:09

Skeebl


You can use Maven profile activation based on properties value, in this case the property will be jdk.version and its value the different configuration of JDK. The profile will then change the Maven Surefire Plugin configuration accordingly.

Hence, your pom may look like the following:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample</groupId>
    <artifactId>sample-project</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <jdk.version>1.7</jdk.version>
    </properties>

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

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>surefire-java7</id>
            <activation>
                <property>
                    <name>jdk.version</name>
                    <value>1.7</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.19.1</version>
                        <configuration>
                            <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>surefire-java8</id>
            <activation>
                <property>
                    <name>jdk.version</name>
                    <value>1.8</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.19.1</version>
                        <configuration>
                            <argLine>-Xmx1024m</argLine>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

Note the profiles section at the end. Two profiles are defined:

  • surefire-java7: it will be activated by the value 1.7 for the jdk.version variable and set the argLine for the Maven Surefire Plugin with the desired value
  • surefire-java8: it will be activated by the value 1.8 for the jdk.version variable and set a different argLine for the Maven Surefire Plugin.

Also note that with this configuration you can even switch JDK version (and as such Surefire configuration) at demand from the command line, as following:

mvn clean test -Djdk.version=1.8

The associated profile will be automatically activated as part of the build.


Important note about cross-compilation (you may already be aware of it, but just in case) I would suggest to carefully read this question/answer.

like image 44
A_Di-Matteo Avatar answered Sep 18 '22 16:09

A_Di-Matteo