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

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>

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.

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

Related

<activeByDefault/> doesn't work when another profile triggered

I have profiles look like this.
<profile>
<id>active-by-default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>${version.org.mongodb}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>java8</id>
<activation>
<jdk>(,1.8]</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<configuration>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
<lib>${java.home}/lib/jce.jar</lib>
<lib>${java.home}/lib/jsse.jar</lib>
</libs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>java9</id>
<activation>
<jdk>[1.9,)</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<configuration>
<libs>
<lib>${java.home}/jmods/java.base.jmod</lib>
</libs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Those two java- profiles are for ProGuard.
And I found the active-by-default profile doesn't work even with no profiles specified.
I found it works when I remove one of those java- profile which meets my current JDK version.
Is this intended?
Yes, this is the documented behaviour :
This profile will automatically be active for all builds unless another profile in the same POM is activated using one of the previously described methods. All profiles that are active by default are automatically deactivated when a profile in the POM is activated on the command line or through its activation config.
https://maven.apache.org/guides/introduction/introduction-to-profiles.html

Springboot remove class only when deploy

I have one springboot project but I wanna to deploy in my nexus to use with component in another project, so I try to remove some classes just like this:
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>br.com.lumera.balcaoonline.api.BalcaoonlineApiApplication.class</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>br/com/lumera/balcaoonline/api/BalcaoonlineApiApplication.class</exclude>
<exclude>br/com/lumera/balcaoonline/api/central/controller/rtdpj/*.class</exclude>
<exclude>**/application-*.yml</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
I need to remove the controllers and the main class, but now when I try to run the project the springboot dont find the main class
how can I fix this?
tks
You can use profiles to create one jar for nexus and another not for nexus.
Maven command:
mvn install -DwithNexus=true
Example:
<profiles>
<profile>
<id>nexus</id>
<activation>
<property>
<name>withNexus</name>
</property>
</activation>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>br.com.lumera.balcaoonline.api.BalcaoonlineApiApplication.class</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>br/com/lumera/balcaoonline/api/BalcaoonlineApiApplication.class</exclude>
<exclude>br/com/lumera/balcaoonline/api/central/controller/rtdpj/*.class</exclude>
<exclude>**/application-*.yml</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>no-nexus</id>
<activation>
<property>
<name>withNexus</name>
<value>!true</value>
</property>
</activation>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>br.com.lumera.balcaoonline.api.BalcaoonlineApiApplication.class</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</profile>

How to run maven from the project

I am trying to start the maven from the root folder (where the file pom.xml is located) of the project via the command line, but I get an error that the command "mvn install" was not found. If I run a maven through the directory where it is installed, then it cannot find the project. How to run maven from the project?
pom.xml
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>ru.evgeniyosipov.facshop</groupId>
<artifactId>facshop</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>facshop</name>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.exec.plugin.version>1.4.0</maven.exec.plugin.version>
<integration.container.id>glassfish4x</integration.container.id>
<glassfish.home>${glassfish.home.prefix}/glassfish4</glassfish.home>
</properties>
<profiles>
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<glassfish.home.prefix>c:/</glassfish.home.prefix>
<glassfish.executables.suffix>.bat</glassfish.executables.suffix>
</properties>
</profile>
<profile>
<id>unix</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<properties>
<glassfish.home.prefix>${user.home}</glassfish.home.prefix>
<glassfish.executables.suffix />
</properties>
</profile>
</profiles>
<modules>
<module>facshop-events</module>
<module>facshop-entities</module>
<module>facshop-resources</module>
<module>facshop-payment</module>
<module>facshop-store</module>
<module>facshop-shipment</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.14</version>
<executions>
<execution>
<id>Deploy</id>
<phase>integration-test</phase>
<goals>
<goal>redeploy</goal>
</goals>
<configuration>
<container>
<containerId>${integration.container.id}</containerId>
<type>installed</type>
<home>${glassfish.home}</home>
</container>
<configuration>
<type>existing</type>
<home>${glassfish.home}/glassfish/domains</home>
<properties>
<cargo.glassfish.domain.name>domain1</cargo.glassfish.domain.name>
<cargo.glassfish.admin.port>4848</cargo.glassfish.admin.port>
<cargo.remote.username>admin</cargo.remote.username>
<cargo.remote.password></cargo.remote.password>
</properties>
</configuration>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
You need to add the maven bin directory to the PATH. Then you can call it from the project directory.

Maven - 'override' not work in external settings

I am using spring boot in my app like this:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
In my external settings.xml I defined resource plugin like this:
<profile>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<id>dev</id>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration combine.self="override">
<delimiters>
<delimiter>^^</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>
and this not work. When I move it to my pom it works. I know that settings file is red because dev properties are taken from it. What is wrong?

Running surefire test on another\diffrent pom.xml file

I have 2 pom.xml-
one is for testing and the second is for running jetty with war deployment.
How can I run surefire tests on the second pom, when I'm running the first pom?
I tried to call it as a profile in the first pom and the surefire plugin is
started but it doesn't run my tests.
12:01:24 [INFO] --- maven-surefire-plugin:2.16:test (integration-test) # apm-tests ---
12:01:32 [INFO] No tests to run.
pom structure:
<parent>
<artifactId>apm-root</artifactId>
<groupId>com.platform</groupId>
<version>12.50.9999-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apm-tests</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
</dependency>
</dependencies>
<profile>
<id>generate-schema</id>
<build>
<plugins>
<plugin>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>start-jetty</id>
<build>
<plugins>
<plugin>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>tests</id>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skipTests>false</skipTests>
<argLine>-Xmx1536m</argLine>
<testSourceDirectory>path_to_tests(2nd pom)</testSourceDirectory>
<includes>
<include>path_to_tests/**/*.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

Resources