Maven profiles with variable for properties - maven

I am studying MAVEN profiles and i have a question about seting properties with variables. At the moment, i am using the following configuration :
<profile>
<id>Action type D restriction</id>
<activation>
<property>
<name>actionType</name>
<value>D</value>
</property>
</activation>
<properties>
<restriction.actionType>D</restriction.actionType>
</properties>
</profile>
<profile>
<id>Action type W restriction</id>
<activation>
<property>
<name>actionType</name>
<value>W</value>
</property>
</activation>
<properties>
<restriction.actionType>W</restriction.actionType>
</properties>
</profile>
My question is this ; is it possible to somehow use one profile with a variable to do the same job, something along the lines of :
<profile>
<id>Action type restriction</id>
<activation>
<property>
<name>actionType</name>
<value>[D,W]</value>
</property>
</activation>
<properties>
<restriction.actionType>${project.profiles.profile.activation.value}</restriction.actionType>
</properties>
</profile>
Or something along those lines.

Maybe you want something like this
<profiles>
<profile>
<id>Action type restriction</id>
<activation>
<property>
<name>actionType</name>
</property>
</activation>
<properties>
<restriction.actionType>${actionType}</restriction.actionType>
</properties>
</profile>
</profiles>

Thank you Tunaki. That did the trick. Now my setup is this :
<project>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<ActionType/>
</properties>
...
<profiles>
<profile>
<id>Action-type profile</id>
<activation>
<property>
<name>actionType</name>
<value>[D,W]</value>
</property>
</activation>
</profile>
</profiles>
...
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>properties.plugin</groupId>
<artifactId>Lazaruss-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<actionType>${actionType}</actionType>
</configuration>
...
</plugin>
</plugins>
</pluginManagement>
</build>
And running it with command :
mvn groupId:artefactId:version:goal -DactionType=W
It sets the variable in my class to W now :)
PS : notice that the variable in my class is also named 'actionType'

Related

Maven - Profile Depending on Property?

I have the following profile below, instead of using activeByDefault I would like to active it through a property instead. I saw this comment (https://stackoverflow.com/a/18890159/1959534) in passing about being able to do so through maven assembly plugin but I couldn't figure out how to specify that. Any input on how to accomplish this?
<project ...>
...
<profiles>
<profile>
<id>myProfile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<!-- Extra dependency just for this profile -->
</dependency>
</dependencies>
</profile>
</profiles>
</project>
Figured it out!
Defined a system variable in the project's /.mvn/jvm.config and then changed the activation step to be through property instead of activeByDefault. Then used this tips to throw config out to my .properties file: https://stackoverflow.com/a/14386303/1959534
jvm.config
-Dmy.property=true
pom.xml
<profiles>
<profile>
...
<activation>
<property>
<name>my.property</name>
<value>true</value>
</property>
</activation>
</profile>
</profiles>

How to use profile variables to define final properties values?

In Spring Boot, I know I can define profiles on application.yml. However, it's not clear to me how to achieve the same result as in this pom.xml snippet:
...
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<pucomex.config.redis.host>devhost</pucomex.config.redis.host>
<pucomex.config.redis.port>5678</pucomex.config.redis.port>
</properties>
</profile>
<profile>
<id>tst</id>
<properties>
<pucomex.config.redis.host>tsthost</pucomex.config.redis.host>
<pucomex.config.redis.port>1234</pucomex.config.redis.port>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>io.thorntail</groupId>
<artifactId>thorntail-maven-plugin</artifactId>
<version>2.5.0.Final</version>
<configuration>
<properties>
<!-- properties based on profile variables -->
<pucomex.config.redis.host>${pucomex.config.redis.host}</pucomex.config.redis.host>
<pucomex.config.redis.port>${pucomex.config.redis.port}</pucomex.config.redis.port>
<!-- global properties -->
<elastic.port>9200</elastic.port>
<elastic.rhlc.connect.timeout.ms>5000</elastic.rhlc.connect.timeout.ms>
<elastic.rhlc.socket.timeout.ms>60000</elastic.rhlc.socket.timeout.ms>
</properties>
</configuration>
</plugin>
</plugins>
</build>
...
In other words: I want to have some properties values varying according to each profile and then use them to define the value of another property.

pom.xml environment variable with default fallback

I would like to be able to use an environment variable if it's set or a default fallback value that I set in pom.xml similar to ${VARIABLE:-default} in bash. Is it possible? Something like:
${env.BUILD_NUMBER:0}
I wasn't really satisfied with the accepted approach, so I simplified it a little.
Basically set a default property in the normal properties block, and only override when appropriate (instead of an effective switch statement):
<properties>
<!-- Sane default -->
<buildNumber>0</buildNumber>
<!-- the other props you use -->
</properties>
<profiles>
<profile>
<id>ci</id>
<activation>
<property>
<name>env.buildNumber</name>
</property>
</activation>
<properties>
<!-- Override only if necessary -->
<buildNumber>${env.buildNumber}</buildNumber>
</properties>
</profile>
</profiles>
You could use profiles to achieve this:
<profiles>
<profile>
<id>buildnumber-defined</id>
<activation>
<property>
<name>env.BUILD_NUMBER</name>
</property>
</activation>
<properties>
<buildnumber>${env.BUILD_NUMBER}</buildnumber>
</properties>
</profile>
<profile>
<id>buildnumber-undefined</id>
<activation>
<property>
<name>!env.BUILD_NUMBER</name>
</property>
</activation>
<properties>
<buildnumber>0</buildnumber>
</properties>
</profile>
</profiles>
A bit more verbose than bash...

Maven not finding properties for profile when OS Version tag used

Trying to get a properties to be set based upon whether a user's OS is windows 10+ or below.
However whenever I enable the command inside the OS tags, the properties are not set. Comment them out however and then the properties are set again.
Code below:
<profiles>
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
<version>(,10.0)</version>
</os>
</activation>
<properties>
<ChromeWebDriverLocation>${basedir}${file.separator}driver${file.separator}win32${file.separator}chrome${file.separator}chromedriver.exe</ChromeWebDriverLocation>
<IEWebDriverLocation>${basedir}${file.separator}driver${file.separator}win32${file.separator}ie${file.separator}IEDriverServer.exe</IEWebDriverLocation>
<SkipIETests>false</SkipIETests>
<SkipEdgeTest>true</SkipEdgeTest>
</properties>
</profile>
<profile>
<id>windows10Edge</id>
<activation>
<os>
<family>windows</family>
<version>[10.0,)</version>
</os>
</activation>
<properties>
<ChromeWebDriverLocation>${basedir}${file.separator}driver${file.separator}win32${file.separator}chrome${file.separator}chromedriver.exe</ChromeWebDriverLocation>
<EdgeWebDriverLocation>${basedir}${file.separator}driver${file.separator}win64${file.separator}edge${file.separator}MicrosoftWebDriver.exe</EdgeWebDriverLocation>
<SkipIETests>true</SkipIETests>
<SkipEdgeTest>false</SkipEdgeTest>
</properties>
</profile>
<profiles>

how to set a property value according to profile

I'm new to maven and i got this issue when sometimes when i run maven i want to have a property with 1 value and sometimes the other.
I tried to do the following but still something is missing:
<profiles>
<profile>
<id>production</id>
<activation>
<property>
<name>com.sencha.cmd.dir</name>
<value>
${env.SENCHA_PATH}\senchaCmd-${com.sencha.cmd.version}\Sencha\Cmd\${com.sencha.cmd.version}
</value>
</property>
</activation>
</profile>
</profiles>
<properties>
<com.sencha.cmd.dir> ${env.SENCHA_PATH}\senchaCmd- ${com.sencha.cmd.version}\Sencha\Cmd\${com.sencha.cmd.version}</com.sencha.cmd.dir>
my question is how to replace the value of the property com.sencha.cmd.dir according to the availability of the profile?
Hope this is clear enough
It's really simple
<profiles>
<profile>
<id>profile 1</id>
<activation>
...
</activation>
<properties>
<my.property> xxx </my.property>
</properties>
</profile>
<profile>
<id>profile 2</id>
<activation>
...
</activation>
<properties>
<my.property> yyy </my.property>
</properties>
</profile>
</profiles>
and then you can use your my.properties outside, even in an other one properties
<properties>
<my.next.property> abc ${my.property} def </my.next.property>
</properties>

Resources