I need to create different application properties for a spring boot project and include the proper one in the generated war. I'm able to generate a war, but no to include the proper file in it. I have different profiles created, and different application.properties following the pattern application-env.properties where env is (dev, cert...), all of then placed in src/main/resources but I'm not able to pick the proper one and include in the generated war, even including "-Dspring.profiles.active=cert" to define the profile active. The war is generated with all of them. Any idea?
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>dev</build.profile.id>
<packaging.type>jar</packaging.type>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
<profile>
<id>cert</id>
<properties>
<build.profile.id>cert</build.profile.id>
<spring.profiles.active>cert</spring.profiles.active>
<packaging.type>war</packaging.type>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
</profiles>
I am trying to setup an integration-test environment using Maven for my project but am getting the below error when I run the Compile goal.
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-resources-plugin:3.0.2:resources
(default-resources) on project mavenintegrationtest: Error loading
property file
'/Users/xxx/dev/poc/java/mavenintegrationtest/profiles/dev/config.properties'
-> [Help 1]
The error seems to be complaining that it cant find the config.properties file in that location which is correct. For some reason it has removed the "src/main/resources" bit from the file path.
So the correct full path is,
/Users/xxx/dev/poc/java/mavenintegrationtest/src/main/resources/profiles/dev/config.properties
but for some reason its removed src/main/resources and so is looking in,
/Users/xxx/dev/poc/java/mavenintegrationtest/profiles/dev/config.properties
Does anyone know whats causing this ?
My POM is as shown below and I get this error when I uncomment the following "filter" tag,
<filter>${basedir}/profiles/${build.profile.id}/config.properties</filter>
I've tried removing the ${basedir} statement and still get the same error.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>mavenintegrationtest</artifactId>
<version>1.0-SNAPSHOT</version>
<name>mavenintegrationtest</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<filters>
<filter>${basedir}/profiles/${build.profile.id}/config.properties</filter>
</filters>
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
</build>
<!-- Profile configuration -->
<profiles>
<!-- The configuration of the development profile -->
<profile>
<id>dev</id>
<!-- The development profile is active by default -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>dev</build.profile.id>
</properties>
</profile>
<!-- The configuration of the integration-test profile -->
<profile>
<id>integration-test</id>
<properties>
<build.profile.id>integration-test</build.profile.id>
</properties>
</profile>
</profiles>
</project>
The filter (link) element will parse your files and apply a content filter on them.
The profiles (link) element helps you define different environments for the build.
All of this as to do with your resources. These can be configuration files, or other type of files. If you filter then you can change the content of this files with other values - e.g. pom properties. When using profiles you can have different replacement properties for each environment.
You should move the profiles up in the tree for the default path, or add a configuration in your pom for the resource location.
The base dir is the folder containing your pom. You should have your profile folder here.
Also, here is some good information about profiles and its configurations.
I have a maven dependency like this:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${version}</version>
</dependency>
And the thing is that the ${version} property should be replaced by 1.8.1.RELEASE or 1.9.0.RELEASE depending on the profile I choose when installing, but for testing purposes only the 1.9.0.RELEASE should be used, even if I'm using the 1.8.1 profile. Is there a way to do this? I tried using the test scope but it didn't work as I think it would.
Would something like this work for you?
<profiles>
<profile>
<id>defaultProfile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<magicVersion>1.9.0.RELEASE</magicVersion>
</properties>
</profile>
<profile>
<id>Release181</id>
<properties>
<magicVersion>1.8.1.RELEASE</magicVersion>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<magicVersion>1.9.0.RELEASE</magicVersion>
</properties>
</profile>
</profiles>
The idea being is activating the test profile will overwrite the version even if it's been set already by your 1.8.1 profile. Use case would be;
mvn -P Release181,test test
I have a spring boot application where I want to package it according to the profile I am building with, for example when I issue profile x, I want maven to build as a jar; but when building with profile y, I want maven to build as a war.
I want something like:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<package>${buildMethod}</package>
</project>
Where ${buildMethod} is the type of packaging.
This would be decided by executing a command i.e. mvn package -P buildProfile.
Maybe this would help:
<profiles>
<profile>
<id>war</id>
<properties>
<deploy.type>war</deploy.type>
</properties>
</profile>
<profile>
<id>jar</id>
<properties>
<deploy.type>jar</deploy.type>
</properties>
</profile>
</profiles>
And then just use this property placeholder ${deploy.type}.
You can easily add profiles, with a list of properties, to your pom file.
Build using:
mvn package - Builds a WAR by default (implicit)
mvn package -P deployWar - Builds a WAR
mvn package -P deployJar - Builds a JAR
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<package>${deployment.type}</package>
<profiles>
<!-- This profile deploys the application as a WAR (default) -->
<profile>
<id>deployWar</id>
<properties>
<deployment.type>war</deployment.type>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- This profile deploys the application as a JAR -->
<profile>
<id>deployJar</id>
<properties>
<deployment.type>jar</deployment.type>
</properties>
</profile>
</profiles>
</project>
I would like to put JDK tools.jar as compile dependency. I found some examples that indicate to use the systemPath property like the following:
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
The problem is that the path is not correct for Mac Os X (however it is correct for Windows and Linux). For it, the correct path is ${java.home}/../Classes/classes.jar.
I am looking for a way in order to define a maven property such that if system is detected as Mac Os X, value is set to ${java.home}/../Classes/classes.jar, otherwise it is set to ${java.home}/../lib/tools.jar (like it is possible to do with ANT). Does someone has an idea ?
That's what profiles are for, extract the path to a property, setup profiles for windows, OSX, etc, and define the property values appropriately.
Here's the doc page that discussing profiles for OSes: Maven Local Settings Model
It should endup looking something like this:
<profiles>
<profile>
<id>windows_profile</id>
<activation>
<os>
<family>Windows</family>
</os>
</activation>
<properties>
<toolsjar>${java.home}/../lib/tools.jar</toolsjar>
</properties>
</profile>
<profile>
<id>osx_profile</id>
<activation>
<os>
<family>mac</family>
</os>
</activation>
<properties>
<toolsjar>${java.home}/../Classes/classes.jar</toolsjar>
</properties>
</profile>
</profiles>
Thank you for introducing me maven profiles.
I have used profile as mentioned above and by activating a profile based on the presence of the desired file :
<profiles>
<profile>
<id>default-profile</id>
<activation>
<activeByDefault>true</activeByDefault>
<file>
<exists>${java.home}/../lib/tools.jar</exists>
</file>
</activation>
<properties>
<toolsjar>${java.home}/../lib/tools.jar</toolsjar>
</properties>
</profile>
<profile>
<id>mac-profile</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${java.home}/../Classes/classes.jar</exists>
</file>
</activation>
<properties>
<toolsjar>${java.home}/../Classes/classes.jar</toolsjar>
</properties>
</profile>
</profiles>
I posted this answer to highlight a mistake in the previous post : the property section can only be used in activation section in order to activate a profile based on the existence of the specified property. In order to define a property, the properties section must be used like above.
Hi I know you guys are all smart, but it caused me couple of days to figure out the answer is not complete - both the profile and the dependency is necessary. I hope no one will waste time on this again. Please see my complete code below:
<profiles>
<profile>
<id>osx_profile</id>
<activation>
<activeByDefault>false</activeByDefault>
<os>
<family>mac</family>
</os>
</activation>
<properties>
<toolsjar>${java.home}/../Classes/classes.jar</toolsjar>
</properties>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.6.0</version>
<scope>system</scope>
<systemPath>${toolsjar}</systemPath>
</dependency>
</dependencies>
</profile>
</profiles>
I found a solution in Q: Declare maven dependency on tools.jar to work on JDK 9
As the actual maven wizardry is quite elaborate, surprising to newcomers and a subject of future improvements, it is better not co copy-paste it around. Hence this module exists so you do not have to know or care about the details. ~~ https://github.com/olivergondza/maven-jdk-tools-wrapper
<dependency>
<groupId>com.github.olivergondza</groupId>
<artifactId>maven-jdk-tools-wrapper</artifactId>
<version>0.1</version>
</dependency>
Somehow, the eclipse in windows fails to pick up {java.home}. So, I had to set JAVA_HOME instead of java.home. JAVA_HOME was set in Run->Run Configurations->Environment. This worked for me with standard JDK(not Apple JDK).
<profiles>
<profile>
<id>windows-profile</id>
<activation>
<activeByDefault>true</activeByDefault>
<file>
<exists>${JAVA_HOME}/lib/tools.jar</exists>
</file>
</activation>
<properties>
<toolsjar>${JAVA_HOME}/lib/tools.jar</toolsjar>
</properties>
</profile>
<profile>
<id>mac-profile</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${java.home}/../lib/tools.jar</exists>
</file>
</activation>
<properties>
<toolsjar>${java.home}/../lib/tools.jar</toolsjar>
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>jdk1.8.0</version>
<scope>system</scope>
<systemPath>${toolsjar}</systemPath>
</dependency>
</dependencies>
The comment of Edward is correct.
You need the profile AND you need the dependency outside of the profiles block.
The profile just determines which value ${toolsjar} is gonna get.
<dependencies>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>jdk1.8.0</version>
<scope>system</scope>
<systemPath>${toolsjar}</systemPath>
</dependency>
</dependencies>
Proper instructions for beginners
First Add this profile to Pom.xml file above tag or somewhere else in it.
<profiles>
<profile>
<id>default-profile</id>
<activation>
<activeByDefault>true</activeByDefault>
<file>
<exists>${java.home}/../lib/tools.jar</exists>
</file>
</activation>
<properties>
<toolsjar>${java.home}/../lib/tools.jar</toolsjar>
</properties>
</profile>
<profile>
<id>mac-profile</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${java.home}/../Classes/classes.jar</exists>
</file>
</activation>
<properties>
<toolsjar>${java.home}/../Classes/classes.jar</toolsjar>
</properties>
</profile>
</profiles>
then Correct JRE path
Goto :
Windows > Preferecnes > Installed JREs
selected intalled JRE and double click on it or from right menu click edit and then make sure JRE Home path is inside JDK something like:
C:\Program Files\Java\jdk1.8.0_181\jre
if you have installed JRE seperatly then eclipse would have picked standalone JRE like:
C:\Program Files\Java\jre1.8.0_181\
so change it to JRE which come with JDK:
C:\Program Files\Java\jdk1.8.0_181\jre
my solution:
put the Sun's tools.jar to the $JAVA_HOME/lib
make a symlink in the $JAVA_HOME/.. named lib where target will be $JAVA_HOME/lib