djangofan
djangofan

Reputation: 29669

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>

Upvotes: 13

Views: 23836

Answers (2)

Skeebl
Skeebl

Reputation: 122

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

Upvotes: 6

A_Di-Matteo
A_Di-Matteo

Reputation: 27812

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.

Upvotes: 8

Related Questions