maven resources plugin - How to filter from external file? - maven

I need to configure some log4j2.xml file based on some properties and those properties vary according to the environment. For example I set log.org.hibernate=info for development and log.org.hibernate=error for production etc.
I'm using maven resources plugin as shown below and it's working fine.
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>${project.basedir}/src/main/resources/env/prod.properties</filter>
</filters>
Now I need to filter external properties file and set those properties to the same log4j2.xml file. But AFAIK resources plugin doesn't allow external file filtering.
How can I read the properties from external properties file (outside of my project directory) and set them to an inner resource (such as log4j2.xml file)?

Related

Maven exclude resources not working as expected

I am trying to exclude from a build all YAML resource files, but the ones with a prod clause within the filename.
For example, given that my resource directory contains application-dev.yaml, application-test.yaml and application-prod.yaml, I would like application-dev.yaml and application-test.yaml to be excluded and application-prod.yaml to be kept.
The portion of my POM that deals with the resources is below:
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<excludes>
<exclude>**/*-!(prod).yaml</exclude>
</excludes>
</resource>
<resource>
<directory>${project.basedir}/web/WEB-INF</directory>
</resource>
</resources>
However, exclusion does not work and all YAML files are copied, including application-dev.yaml and application-test.yaml.
I tested the exclusion pattern in Bash shell by ls *-!(prod).yaml and it worked as expected.
At this point I am lost and am looking for the community assistance.
I thank you all in advance for your thoughts and comments.
In order to solve that I would go with maven profiles and resource plugin maven resource plugin
You can have variables to the resource file name according to what you need (prod, dev, etc)

How to acces POM project name from properties file

I have logback.properties file which contains the following properties.
#logging
logging.server=
logging.port=
logging.path=/opt/${project.name}/logs
Inside my POM file I've got the <name> tag, which specifies the project name. I'd like to inject this name in few properties files like the lockback one above. Doing the above results in creating a folder called project.name_IS_UNDEFINED. Is it possible to access the project name and how?
UPDATE
Ralph gave the correct answer, however check my comment, because for spring boot applications you need to used %project.name% instead of ${project.name}!
You need to transfer the properties defined in you maven compile-time script into the application at run time.
The most easiest way is to use maven's resource filtering (the name is a bit misleading, it is not a filter that selects files, it is a property replace for text/resource files) in order to let maven replace ${project.name} in you logback.properties file.
true
${basedir}/src/main/resources
If you want enabling resource filtering just for one file (and not for the others to prevent maven from replacing other markers then you can use this snippet:
<resources>
<!-- enable filtering for logback.properties only -->
<resource>
<filtering>false</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>WHEREVER_LOCATED/logback.properties</exclude>
</excludes>
</resource>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>WHEREVER_LOCATED/logback.properties</include>
</includes>
</resource>
</resources>
Try with the resource filtering:
https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

Maven resource filtering - Explicitly specify which files require property injection

Does the Maven resources plugin allow a flexible way to exclude certain files during the injection of Maven profile properties?
I don't want to exclude the files from assembly, just from the injection phase.
The project I'm working on defines unique Maven Profiles (and corresponding properties) in Settings.xml for each deployment environment. When the project is built the following steps occur
The projects POM defines the resources folder as the target to apply resource filtering
The resources folder contains .XML and .PROPERTIES files
During mvn:deploy Maven injects Profile properties into the .PROPERTIES file as expected
Maven also injects Profile properties into .XML files. This is not desired behavior (these files contain placeholders which allow the project to flexible inject values during deploy of the application)
The resource plugin provides configuration options to define include and exclude options however choosing the exclude option will also exclude the specified file from the assembly folder which is not desired.
Is it possibly to tell Maven which files should have placeholders replaced?
You are probably using the filters mechanism, for which you can decide whether to apply it to a certain folder and which filter should be applied to that folder.
Given the following sample POM:
<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.sample</groupId>
<artifactId>resources-example</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<filters>
<filter>src/main/filters/filter.properties</filter>
</filters>
<resources>
<!-- configuring an additional resources folder: conf -->
<resource>
<directory>${project.basedir}/src/main/conf</directory>
<filtering>true</filtering>
<excludes>
<exclude>*.txt</exclude>
</excludes>
<includes>
<include>*.properties</include>
</includes>
<targetPath>${project.basedir}/target</targetPath>
</resource>
</resources>
</build>
</project>
Note the filters section within the build section. Here we are telling Maven where how filter is, providing placeholders replacement.
Note then the <filtering>true</filtering> addition to a new resource configured afterwards and related includes/excludes patterns. As such, Maven will filter only the *.properties files of this folder.
Now, src/main/conf can contain a conf.properties file with the following content:
## add some properties here
property.example=#property.value1#
property.example2=${property.value2}
(Note the ant and maven style placeholders.)
While the src/main/filters (you need to create this folder) contains the filter.properties file with the following content:
property.value1=filtered-value1
property.value2=filtered-value2
Running the build you will get the conf.properties file in the target directory with the following content:
property.example=filtered-value1
property.example2=filtered-value2
Now, if your filter (file name) is a property injected by a profile, you can then inject different filters depending on the environment and only targeting certain files.

maven bundle plugin No translation found for macro:

I use maven bundle plugin for bundle spring project. I'm use spring property placeholder in my project. When I building my project I take following warnings:
[WARNING] Bundle groupId:artifactId:bundle:1.9-SNAPSHOT : No translation found for macro: spring.property
How I can prevent this warnings message ? May be some maven bundle plugin settings can help me ?
Thanks
It appears that the maven-bundle-plugin (v. 2.5.3 at the time of writing) has its own go at resource filtering after the resource plugin is done. If the resource plugin cannot replace a property it will simply leave it as it is. Which is what you want, of course, if the property is in a Spring context file to be replaced by Spring at run-time. But the left-over properties confuse the bundle plugin.
The only way around this that I could find was to disable resource filtering for the Spring context file. In the build section of your POM add something along the lines of this:
<resources>
<!-- globally enable resource filtering -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<!-- then disable it for specific resources -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*-context.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
You can also turn it around and explicitly include your to be filtered files in a resource declaration that enables filtering and globally disable filtering.

Unable to retrieve values from property files in Maven

Iam using maven pom.xml ( just started learning )
I had some .properties files (for eg: log4j.properties), I should be able to retrieve values from them either in pom.xml or in web.xml file , I mean if I use something like ${somename.version} in pom.xml or web.xml, this value should be retrieved from .properties files.
My properties files are under as below:
src/main/resources/log4j.properties
src/main/env/dev/config.properties
iam trying as below, BUT unable to retrieve values from properties files.. iam doing something wrong.
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/env/dev</directory>
<filtering>true</filtering>
</resource>
</resources>
please suggest me.
Your configuration is wrong, for reading properties from file use:
<filters>
<filter>src/main/resources/log4j.properties</filter>
</filters>
Your actual configuration is about what files should be filtered.
For more information and example go to: Maven Resource Plugin - Filtering
You can also use Properties Maven Plugin
Resource filtering is meant to set a property in pom.xml and then use it in a property file.
with ${somename.version} in log4j.properties and the appropriate code in pom.xml your ${somename.version} is replaced with what you typed in pom.xml
for example inside pom.xml
<property>
<name>somename.version</name>
<value>123</value>
</property>
in your log4j.property
${somename.version} will be replaced by 123
you will find your file with the replaced value inside the target directory after you launch a mvn package
resources filtering is used when you package with profiles. each profile can change the properties in pom.xml
your properties can hold values that change with environnement like configuration for windows or configuration for linux

Resources