Maven - rename files with names put in variable - maven

so here is my problem,
I have files, each of them must be renamed differently when using different profiles.
So I have 2 .properties files, dev.properties and rec.properties
in dev.properties you can find :
machineName=marin
prefix=DEV
fileOneName=node
in rec.properties you can find :
machineName=marin
prefix=REC
fileOneName=node
what I want now is to be able to use the content of those variables when setting wich profile to use with :
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Filter name -->
<cogepat.filter.properties>dev.properties</cogepat.filter.properties>
</properties>
</profile>
But my variables are not being filled, when using :
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>run-ant-rename-war</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy file="mavencopyfrom/adeplacer.txt" tofile="mavencopyto/${prefix}_${fileOneName}${machineName}.txt"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
What I end up with is a file named :
${prefix}_${fileOneName}${machineName}.txt

Try to set the properties like this instead:
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<machineName>marin</machineName>
<prefix>DEV</prefix>
<fileOneName>node</fileOneName>
</properties>
</profile>
That should do the trick.

Related

injecting new argument/property value to maven profile in module

i have main pom.xml
i like to change from the main mvn command line cli which I'm using and change the :
<argument>${docker.image}</argument>
argument in only in the submodule :
module_y profile NOT module_x
this is the command I'm executing now :
mvn clean install -Ddocker_build=build
<artifactId>foo</artifactId>
<version>b1</version>
<packaging>pom</packaging>
<properties>
<docker.image>www.repo.org:8000/${project.artifactId}:${project.version}</docker.image>
</properties>
<modules>
<module>module_x</module>
<module>module_y</module>
</modules>
this is the section in the module_x and module_y
<profiles>
<profile>
<activation>
<property>
<name>docker_build</name>
<value>build</value>
</property>
<file>
<exists>Dockerfile</exists>
</file>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<executable>docker</executable>
<arguments>
<argument>build</argument>
<argument>-f</argument>
<argument>${project.basedir}/Dockerfile</argument>
<argument>-t</argument>
<argument>${docker.image}</argument>
<argument>.</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
in short, how do i change only the property value ${docker.image} in profile docker_build in module_y from main mvn run?
If you cannot change the POMs, this cannot be done.
The only possible approach would be to build the modules separately (by using -pl module_x -am or something like that) and use different command line parameters in both cases.

maven - build once to generate separate war files for separate environment

My pom.xml -
<?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.example</groupId>
<artifactId>test-application</artifactId>
<packaging>war</packaging>
<version>0.2.0</version>
<profiles>
<profile>
<id>local</id>
<activation>
<property>
<name>envType</name>
<value>local</value>
</property>
</activation>
<properties>
<envType>local</envType>
</properties>
</profile>
<profile>
<id>local2</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<envType>local2</envType>
</properties>
</profile>
<profile>
<id>dev</id>
<activation>
<property>
<name>envType</name>
<value>dev</value>
</property>
</activation>
<properties>
<envType>dev</envType>
</properties>
</profile>
<profile>
<id>sit</id>
<activation>
<property>
<name>envType</name>
<value>sit</value>
</property>
</activation>
<properties>
<envType>sit</envType>
</properties>
</profile>
<profile>
<id>uat</id>
<activation>
<property>
<name>envType</name>
<value>uat</value>
</property>
</activation>
<properties>
<envType>uat</envType>
</properties>
</profile>
</profiles>
<build>
<finalName>my-application</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<nodeVersion>v6.10.0</nodeVersion>
<npmVersion>3.10.10</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>bower install</id>
<goals>
<goal>bower</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>gulp build</id>
<goals>
<goal>gulp</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<arguments>optimize --env ${envType}</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Front end maven plugin installs node, all required npm and bower packages and initiates the gulp task for optimizing all the front end code. Gulp places all the front end code into the below -
/src/main/webapp/index.html
/src/main/webapp/js/
etc
web.xml is at -
/src/main/webapp/WEB-INF/web.xml
Command to build the war package for local,SIT,DEV, etc environment -
mvn clean install -DenvType=local
mvn clean install -DenvType=sit
mvn clean install -DenvType=uat
This builds the war file - my-application.war in /target directory.
I need to run the maven command separately each time in order to build the war package for each environment. (because the front-end code uses separate environment variables for each environment).
How can I change the build process so that I need to build only once and maven takes care of running the gulp tasks for each environment (gulp optimize --env ${envType}) one after the other and produces separate war files in the target directory -
my-application-local.war
my-application-sit.war
my-application-uat.war
etc
I need maven to run the gulp task in sequence. I can make each run of gulp to output the front-end code to separate directories. The maven should pick from these separate directories and create separate war files. Not sure whether this is the right approach. Please let me know on this.

Maven: How to print the current profile on the console?

I'm trying to print the current profile that is active running a build of a Maven Project.
I'm using the maven-antrun-plugin in order to print messages on the console, in combination with a property that refers to the current profile.
I have tried the following properties:
${project.activeProfiles[0].id}
${project.profiles[0].id}
But in both cases it prints the "string" as it is written, without resolving the variable.
This is my test:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>current active profile: ${project.activeProfiles[0].id}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
But this is the result that I obtain:
main:
[echo] current active profile: ${project.activeProfiles[0].id}
Any suggestion will be appreciated.
Thanks.
The maven-help-plugin offers what you need. It has an active-profiles goal.
You can add it to your pom or even call it from the command line (include it in your maven build call). The How can I tell which profiles are in effect during a build? section of the Maven profile introduction page will show you how. In short:
mvn help:active-profiles
As this does not work for you (see comments) here is another solution:
I think the active profiles (there can be more than one!) are not propagated as available variables - but properties are.
So set a custom property in the profile section and use that, like
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<myProfile>default</myProfile>
</properties>
</profile>
<profile>
<id>debug</id>
<activation>
<property>
<name>debug</name>
</property>
</activation>
<properties>
<myProfile>debug</myProfile>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>current active profile: ${myProfile}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
you can add the maven-help-plugin in your pom to display always the active profile
<build>
<plugins>
<!-- display active profile in compile phase -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>show-profiles</id>
<phase>compile</phase>
<goals>
<goal>active-profiles</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
source: https://www.mkyong.com/maven/maven-profiles-example

Skip exec-maven-plugin from Command Line Argument in Maven

By default in my project POM, exec-maven-plugin, rpm-maven-plugin will be executed,
which is not required in local compilation/build.
I want to skip these plugin execution by passing Command Line Arguments
I tried below command to skip them like normal plugins, but didn't work though!
mvn install -Dmaven.test.skip=true -Dmaven.exec.skip=true
-Dmaven.rpm.skip=true
This page should tell you that the name of the argument to be passed by cmdline (i.e. the user property) is called skip, which is a poorly chosen name. To fix this do the following:
<properties>
<maven.exec.skip>false</maven.exec.skip> <!-- default -->
</properties>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<skip>${maven.exec.skip}</skip>
</configuration>
</plugin>
Try -Dexec.skip from specification:
http://www.mojohaus.org/exec-maven-plugin/java-mojo.html#skip
Using profiles (as little as possible) and execution phase you may achieve what you want for plugins that do not handle the skip property:
Plugin configuration:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>rpm-maven-plugin</artifactId>
<executions>
<execution>
<phase>${rpmPackagePhase}</phase>
<id>generate-rpm</id>
<goals>
<goal>rpm</goal>
</goals>
</execution>
</executions>
<configuration>
...
</configuration>
</plugin>
Profile configuration:
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<rpmPackagePhase>none</rpmPackagePhase>
</properties>
</profile>
<profile>
<id>rpmPackage</id>
<activation>
<property>
<name>rpm.package</name>
<value>true</value>
</property>
</activation>
<properties>
<rpmPackagePhase>package</rpmPackagePhase>
</properties>
</profile>
</profiles>
Invocation:
mvn package -Drpm.package=true [...]

Heroku: managing different properties files in with maven

Maybe someone can help me. I have searched the web but haven't been able to find a solution yet.
I have a java application running on Heroku. I want to be able to have different properties files loaded for different Heroku instances (dev, test, prod), but I get java.io.FileNotFoundException. This is what I have in my pom.xml.
<profiles>
<profile>
<id>dev</id>
<activation>
<property>
<name>env.APP_ENVIRONMENT</name>
<value>dev</value>
</property>
</activation>
<properties>
<profile.name>dev</profile.name>
</properties>
</profile>
<profile>
<id>test</id>
<activation>
<property>
<name>env.APP_ENVIRONMENT</name>
<value>test</value>
</property>
</activation>
<properties>
<profile.name>test</profile.name>
</properties>
</profile>
<profile>
<id>prod</id>
<activation>
<property>
<name>env.APP_ENVIRONMENT</name>
<value>prod</value>
</property>
</activation>
<properties>
<profile.name>prod</profile.name>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.github.jsimone</groupId>
<artifactId>webapp-runner</artifactId>
<version>7.0.34.0</version>
<destFileName>webapp-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/profiles/${profile.name}</directory>
<includes>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</resource>
</resources>
</build>
How to customize the Maven build is described here:
https://devcenter.heroku.com/articles/using-a-custom-maven-settings-xml
You can create a settings file for each build type:
settings.xml <-- Default
settings-dev.xml
settings-test.xml
settings-prod.xml
A production build could then be performed as follows:
heroku config:set MAVEN_SETTINGS_PATH=settings-prod.xml
git push heroku master
Details
The Java buildpack supports a way to specify a custom Maven settings file.
By default it uses a settings file located in the build directory, this can be overridden by setting one of the following variables:
MAVEN_SETTINGS_PATH
MAVEN_SETTINGS_URL

Resources