I have Java Meven project and inside my pom.xml i have this property:
<properties>
<suiteXmlFile>testing.xml</suiteXmlFile>
<JAVA_1_8_HOME>C:\Program Files\Java\jdk1.8.0_181\bin\javac.exe</JAVA_1_8_HOME>
</properties>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<fork>true</fork>
<executable>${JAVA_1_8_HOME}</executable>
</configuration>
</plugin>
So i case i am running my project from Windows i just type mvn test
In case i am with MACOS/Linux this path doesn't exist and i wonder what solutions can be found to fix this issue.
UPDATE
As suggestion here i add this profile:
<profile>
<id>platform-windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<fork>true</fork>
<executable>C:\Program Files\Java\jdk1.8.0_181\bin\javac.exe</executable>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>mac</id>
<activation>
<os>
<family>mac</family>
</os>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<fork>true</fork>
<executable>/usr/bin/javac</executable>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Now how my code will now to run this particular id ?
You can make this configuration in two ways:
1) Explicit Profile
Open Maven pom.xml file available in your project directory:
<properties>
<suiteXmlFile>testing.xml</suiteXmlFile>
<JAVA_1_8_HOME>C:\Program Files\Java\jdk1.8.0_181\bin\javac.exe</JAVA_1_8_HOME>
<JAVA_1_8_HOME_LINUX>/usr/java/jdk1.8.0_181/</JAVA_1_8_HOME_LINUX>
</properties>
<profiles>
<!-- Windows Profile-->
<profile>
<id>jdk-8-windows</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<fork>true</fork>
<executable>${JAVA_1_8_HOME}</executable>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<!-- Mac/Linux Profile-->
<profile>
<id>jdk-8-linux</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<fork>true</fork>
<executable>${JAVA_1_8_HOME_LINUX}</executable>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Default active profile was defined as: jdk-8-windows
If the main profile is Mac/Linux, use: <activeProfile>jdk-8linux</activeProfile>
To execute your Mac/Linux profile use: mvn test -P jdk-8-linux
2) Profile Activation via Maven Settings
Open Maven settings.xml file available in %USER_HOME%/.m2 directory where %USER_HOME% represents the user home directory. If settings.xml file is not there, then create a new one.
<settings>
[...]
<profiles>
[...]
<!-- Windows Profile-->
<profile>
<id>jdk-8-windows</id>
<properties>
<JAVA_1_8_HOME>C:\Program Files\Java\j2sdk1.4.2_09</JAVA_1_8_HOME>
</properties>
</profile>
<!-- Mac/Linux Profile-->
<profile>
<id>jdk-8-linux</id>
<properties>
<JAVA_1_8_HOME_LINUX>/usr/bin/javac</JAVA_1_8_HOME_LINUX>
</properties>
</profile>
</profiles>
[...]
<activeProfiles>
<activeProfile>windows</activeProfile>
</activeProfiles>
</settings>
Default active profile was defined as: jdk-8-windows
If the main profile is Mac/Linux, use: <activeProfile>linux</activeProfile>
To execute your Mac/Linux profile use: mvn test -P jdk-8-linux
Reference:
Maven - Build Profiles
Compiling Sources Using A Different JDK
You could pass the property as a command line argument when running the mvn command, for example:
mvn test"-DJAVA_1_8_HOME=<OS specific path>"
For another solutions, take a look at maven condition based on os family
For profiles:
<project>
<profiles>
<profile>
<properties>
// Define profile specific properties here
</properties>
</profile>
</profiles>
</project>
After defining the profile specific properties, use them as you would use any other property.
Related
I have a Maven project and I tried to extract sensitive information from my code using ~/.m2/settings.xml. As far as I can tell from checking with the mvn -X command, the file can be read from.
This is my file:
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
<profiles>
<profile>
<activeByDefault>true</activeByDefault>
<id>camera</id>
<properties>
<camera.url>some url</camera.url>
</properties>
</profile>
</profiles>
</settings>
I have tried getting the value from camera.url in my code using pom.xml:
<build>
<defaultGoal>install</defaultGoal>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<systemPropertyVariables>
<camera.url>${camera.url}</camera.url>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
And then I am trying to retrieve the value in code using System.getProperty("camera.url"), but this returns null.
I can't seem to figure out what I have done wrong.
I have two maven profiles P1 and P2 and what I want to do is that depending on the profile I use to build my project, certain resources should be excluded.
For example
<profiles>
<profile>
<id>P1</id>
<properties>
<app.home>Path to project home</app.home>
<exclude>src/main/java/foo/*.*</exclude> <!-- need to exclude all files in src/main/java/foo in this profile -->
</properties>
</profile>
<profile>
<id>P2</id>
<properties>
<app.home>Path to project home</app.home>
<exclude>src/main/java/bar/*.*</exclude> <!-- need to exclude all files in src/main/java/bar in this profile-->
</properties>
</profile>
</profiles>
So, here what I want to do is to exclude all files in src/main/java/foo/ when I build using the P1 profile and exclude all files in src/main/java/bar when I build using the P2 profile.
Is this possible and if not is there any alternative?
You can add a build with the Maven Compiler Plugin to your profile and add a exclude in there
E.g.
<profile>
<id>P1</id>
<properties>
<app.home>Path to project home</app.home>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>**src/main/java/foo/*.*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
See for additional information Maven: excluding java files in compilation
If you are using spring boot maven plugin, use should do it like this:
<profiles>
<profile>
<id>sample-profile</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>com/example/foo/ToSkip.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
This would do below. For more details, see: https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>src/my-resources</directory>
<excludes>
<exclude>**/*.bmp</exclude>
<exclude>**/*.jpg</exclude>
<exclude>**/*.jpeg</exclude>
<exclude>**/*.gif</exclude>
</excludes>
</resource>
...
</resources>
...
</build>
...
</project>
Another Maven question. I have app with TestNG tests running by maven-surefire-plugin. I have created 2 profiles, for pdoruction and for testing.
I'm building my app by 'mvn clean install' command. Now my goal is to run TestNG tests only when I specify test profile.
Code:
profiles>
<profile>
<id>prod</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${basedir}/target/test-classes/firstTest.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
But the problem is that tests are running everytime when I build my app... no matter if 'test' profile is specified, or not. Why?
There is a solution to run tests ONLY in case maven profile "test" is activated:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>false</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
See also How to keep Maven profiles which are activeByDefault active even if another profile gets activated?
You can run mvn clean install -DskipTests or change your production profile definition:
<profile>
<id>prod</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
I think you have to skip the tests explicitly.
Try to add the following configuration to your default profile.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
Hope it helps.
I'm trying to get images to my Maven web-application according to the profile selected at build time. But I keep on getting the whole folder of all the profiles to the point where I need the images to go.
<build>
<finalName>user-interface</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<webResource>
<directory>src/main/profile/webapp/</directory>
<targetPath>images</targetPath>
</webResource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>user</id>
</profile>
<profile>
<id>dev</id>
</profile>
</profiles>
How to fix this? Default image is banner.jpg and both profiles have respective images in correct order. Inside src/main/profile/webapp/ there are 2 folders user and dev with each having images folder and the banner.jpg inside images folder.
You need to define a custom property inside each profile and then reference that property in the configuration of the plugin:
<build>
<finalName>user-interface</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<webResource>
<directory>src/main/profile/webapp/${banner.folder}</directory>
<targetPath>images</targetPath>
</webResource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>user</id>
<properties>
<banner.folder>user</banner.folder> <!-- defines banner.folder property when the user profile is activated -->
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<banner.folder>dev</banner.folder> <!-- defines banner.folder property when the dev profile is activated -->
</properties>
</profile>
</profiles>
I am facing very ridiculous problem here i have a pom.xml which is being used to build the war file .I have introduced profile to work it for different environment(dev/prod) .but the problem is when i create build it create correct build directory for the profile which is below in order in pom.xml
Please assist what is the issue here.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<activation>
<property>
<name>lifecycle</name>
<value>prod</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<warSourceExcludes>**login.jsp</warSourceExcludes>
</configuration>
</plugin>
</plugins>
<directory>target/${lifecycle}</directory>
<finalName>testapp</finalName>
<resources>
<resource>
<directory>src/main/resources/${lifecycle}</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<activation>
<property>
<name>lifecycle</name>
<value>dev</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
</plugin>
</plugins>
<directory>target/${lifecycle}</directory>
<finalName>testapp</finalName>
<resources>
<resource>
<directory>src/main/resources/${lifecycle}</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
Correct build directory should be
if -Dlifecycle=dev
target/dev/testapp/
if -Dlifecycle=prod
target/prod/testapp/ but here i get 'target/testapp-xx.x'
why is this different behavior?
Command to trigger the build:mvn clean install -Dlifecycle=prod
Adding an id element to the prod profile fix this.
One thing you can do is specify which profile should be built.
Define an ID for each profile:
<profiles>
<profile>
<id>dev</id>
...
<profile>
When building, use the argument -P to inform maven which profiles to activate
mvn clean install -P dev