Setting pom properties from Maven Plugin for resource filtering - maven

I have a conf file in a java app that contains an IP address parameter. I want to be able to put in this parameter the local ip address automatically at build time. I used maven resources-plugin as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>/home/user/config</outputDirectory>
<resources>
<resource>
<directory>config</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
Next, I've created a property that contains the parameter
<properties>
<gateway.ip>${local.ip}</gateway.ip>
</properties>
Then, I've created a Maven plugin that gets the local ip address and sets the above parameter:
final Properties props = mavenProject.getProperties();
props.put("local.ip", resultAddress.getHostAddress());
lastly, I define my custom plugin in the pom.xml:
<plugin>
<groupId>com.company</groupId>
<artifactId>get-local-ip-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<executions>
<execution>
<id>get-local-ip</id>
<phase>validate</phase>
<goals>
<goal>get-local-ip</goal>
</goals>
<configuration>
<localIp>${local.ip}</localIp>
</configuration>
</execution>
</executions>
</plugin>
The problem is that this doesn't work and I get ${local.ip} in the resulting file instead of xxx.xxx.xxx.xxx ip address.
Any suggestions?

Answered in comments:
found my bug :) - I bound both plugins to the validate phase (the good
old copy-paste) – rperez Jun 22 '11

Related

can you provide overwrite on a per resource basis using the maven-resources-plugin?

So I'm having a problem with sharing a resources root because of IntelliJ, We have some resources we already add with the resource plugin, some of these resources I do not want to overwrite for performance reasons. In other cases I would like them overwritten So we don't forget we have to delete them if we change them. Can I just specify (mostly copy-pasta-ed from the official documentation
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<resources>
<resource>
<overwrite>true</overwrite> <!-- will this work -->
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
in the resources I want to overwrite? really this question is simple Can I put overwrite directly on the resource? Will it work?

reading properties file into maven pom.xml not working

I need to use values from properties file in maven pom.xml, so i used properties-maven-plugin to read my properties file as follows
pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/src/main/resources/qura.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
qura.properties file contains something like this..
config.file.path = resources/python/config/test.py
I need use this config.file.path variable in resource element of pom.xml
pom.xml
<resources>
<resource>
<directory>${basedir}/multilang/</directory>
<includes>
<include>${config.file.path}</include>
</includes>
</resource>
<resources>
But the value for ${config.file.path} is not taking up from qura.properties file and I couldn't find test.py file in jar.
what I'm doing wrong in this code?
Thanks in Advance
try using version 1.0.0 and removing spaces around equal sign in properties file.
eg:
key=value
IMO, it does not matter whether you put the spaces or not around equal sign in properties file. You may need to check if the ${config.file.path} exists in the directory specified by ${basedir}/multilang
The below snippet works for me.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main</directory>
<includes>
<include>${config.file.path}</include>
</includes>
</resource>
</resources>
</configuration>
<inherited></inherited>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/src/main/resources/qura.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

Maven store MD5 checksum of file and file size in properties then filter another file

I am wondering if there is a way in Maven to calculate the MD5 checksum and size of a file, put them into properties then use those properties to filter (text replace) parameters in another file. I am trying to generate a config file for Advanced Installer before I run it.
After spending a while googling for ways to do it with Maven I decided to look into using the antrun plugin. I googled for both features and the first link of both solved the problem. Seems like antrun is a great way to script most things in Maven.
My antrun configuration:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<configuration>
<exportAntProperties>true</exportAntProperties>
</configuration>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<property name="my_path" value="some path"/>
<length file="${my_path}" property="file.size"/>
<checksum file="${my_path}" property="file.md5"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
Maven resources plugin config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>file1</include>
<include>file2</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
Which works great from the command line but for some reason the properties are not resolved in Intellij. I've posted another question for that.

Maven Profiles Jar issues

I have dev/XYZ.properties file, i am building a jar file using maven -P dev install. How can I place the property file in child.jar/META-INF/XYZ.properties file
You have to execute the maven-resources-plugin:copy-resources goal for your profile dev and bind it with the appropriate phase (process-resources seems the best phase to me). Here is how to define your profile dev :
<profile>
<id>dev</id>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/META-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}/dev</directory>
<includes>
<include>XYZ.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

How to use <includes> / <excludes> in maven-remote-resources-plugin

I am trying to use the maven-remote-resources-plugin as per this example to selectively share common resources between multiple maven modules and I'm having a lot of difficulty getting the selective import of the resources to work.
I am trying to use <includes> and <excludes> elements as per below. I haven't seen these mentioned in doco for the plugin anywhere but eclipse provides them as valid options in the command completion and I don't get any errors when I run the pom. So far I haven't been able to get <includes> or <excludes> to have any effect at all on the imported resources
The relevant sections of my pom are;
Shared resources
<build>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
Resource consumer
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.3</version>
<configuration>
<resourceBunldes>
<resourceBundle>myApp:myApp_sharedresources:${project.version}</resourceBundle>
</resourceBundles>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<includes>
<include>theOnlyResourceIWant.properties</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<phase>generate-resources</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>myApp</groupId>
<artifactId>myApp_sharedresources</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
I've tried many combinations of <includes> and <excludes> but all so far have had no impact.
So, are
<includes></includes>
and
<excludes></excludes>
valid elements for a maven-remote-resources-plugin configuration, and how do I use them?
I can reasonably seperate the resources out into seperate maven modules, but that could create a large number of single file maven modules and add a lot of extra xml so I'd like to avoid it if possible.
I'd really rather not start pawing through the plugin source code, but that is the next step.
I use a temp directory for the shared resources I'm importing and filter that.
Remote resource plugin configuration below. This copies all the shared resources into a temp directory in your project. Setting attached to false means they are not included in your final project artifact, which gives you the opportunity to select the ones you want to include using Maven's normal resource processing.
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<configuration>
<resourceBundles>
<resourceBundle>myApp:myApp_sharedresources:${project.version}</resourceBundle>
</resourceBundles>
<attached>false</attached>
<outputDirectory>${project.build.directory}/shared-resources</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<phase>generate-resources</phase>
</execution>
</executions>
</plugin>
Resource definition. When the maven-resource-plugin runs (it is bound to the lifecycle by default for jars/wars/ears), it will use the shared resource directory as well as the normal src/main/resources dir. You need to define both. (You may also enable resource filtering if you want.)
<resources>
<resource>
<directory>${project.build.directory}/shared-resources</directory>
<includes>
<include>theOnlyResourceIWant.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
I recommend making the shared directory be a subdirectory of ${project.build.directory}, so the clean lifecycle works without changes.
To enable the filter delimiters for the format '#{expr}' (Ruby-style), add the following to your plugin configuration:
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>process-remote-resources</id>
<goals>
<goal>process</goal>
</goals>
<configuration>
<filterDelimiters>
<filterDelimiter>#{*}</filterDelimiter>
</filterDelimiters>
[...]
</configuration>
</execution>
</executions>
</plugin>
Check this link for reference

Resources