Maven dependency only for testing purposes - java

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

Related

how to include specific applicaction.properties in spring boot war

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>

maven pom.xml sensitive information and VCS

I am developing one java app and I made pom.xml with database URL I am using:
<profiles>
<profile>
<id>development</id>
<properties>
<db.url>jdbc:mysql://project.******.eu-central-1.rds.amazonaws.com:3306</db.url>
</properties>
</profile>
<profile>
<id>validation</id>
</profile>
</profiles>
Problem is I am using version control and I would not want to submit my specific configuration. What is the best way to handle this?
You can put the information in your personal settings.xml.
edit: And you should use encryption to save your credentials.
Put this type of configuration outside of your pom file. There is no one right answer here... just use a personal settings or properties file or something to that effect. Then just omit that file from version control.
in user home folder /.m2/settings.xml I have put this, works nice. Thanks!
<settings>
<profiles>
<profile>
<id>development</id>
<properties>
<db.url>jdbc:mysql://project.******.eu-central-1.rds.amazonaws.com:3306</db.url>
</properties>
</profile>
<profile>
<id>validation</id>
</profile>
</profiles>
<settings>

How to specify packaging in a Maven profile?

I have a project which can be packaged and deployed two different ways, it's either a WAR for Tomcat, or a shaded JAR for AWS Lambda. Currently this isn't working very well, I have to keep changing the pom.xml back and forth when doing a release. Is there a way to accomplish this with Maven profiles?
e.g., I'd like to do
mvn install -Pwar
to generate the WAR, and
mvn install -Plambda
to generate the shaded JAR.
Is this possible?
You can try to include the following in your pom.xml
<packaging>${packaging.type}</packaging>
<profiles>
<profile>
<id>lambda</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<packaging.type>jar</packaging.type>
</properties>
</profile>
<profile>
<id>war</id>
<properties>
<packaging.type>war</packaging.type>
</properties>
</profile>
</profiles>

How to parameterize a Maven file (pom.xml)?

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>

JDK tools.jar as maven dependency

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

Categories