How to acces POM project name from properties file - spring

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

Related

spring-boot-starter-parent resources includes/excludes explained

I'm a little puzzled by the resources tag in spring-boot-starter-parent version 2.2.4.RELEASE. What is the purpose of having the include and exclude with the same patterns?
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/application*.yml</exclude>
<exclude>**/application*.yaml</exclude>
<exclude>**/application*.properties</exclude>
</excludes>
</resource>
</resources>
The maven-resources-plugin filter option, allows you to include variables in your resources. The variables will be interpolated in the resources which are copied to the output directory.
The spring-boot-starter-parent contains 2 resources blocks.
The first contains <filter>true</filter>, which means that variables will be interpolated. The first contains an includes filter, indicating that the variable interpolation will only be executed for these files.
The second doesn't contain <filter>true</filter>, which means that de default value (= false) will be used and that no interpolation will be done. The second also contains an excludes filter, indicating that the files specified in this filter will be excluded by this resource block.
So the first block will copy all application*.(yml|yaml|properties) files to the output folder, and will interpolate variables. And the second block will copy all other files, without interpolating variables.

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)

maven resources plugin - How to filter from external file?

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)?

Using maven-rpm-plugin how do I to replace text in files similar to the assembly plugin

I have a maven project where I create two packagings. One is a tar.gz file (for some targets) and an RPM for linux targets that can use RPM. I use the maven-assembly-plugin for the tar.gz file. I use maven-rpm-plugin for the RPM packaging.
The assembly plug allows the specification of a true option that will replace any maven properties in the target files. For example (from my pom):
<fileSet>
<directory>${basedir}/src/resources/</directory>
<outputDirectory>/</outputDirectory>
<filtered>true</filtered>
<includes>
<include>**/*.sh</include>
</includes>
<fileMode>0774</fileMode>
</fileSet>
My .sh file has a section in it that declared the jar file for the java command line:
java -cp $ARGO_HOME/client/lib/${project.artifactId}-${project.version}.jar
When I use the maven assembly plugin as defined above, the ${project.artifactId}-${project.version} gets translated accordingly.
However, when I use the same files for my RPM build these variables are not replaced.
Is there a way I can get the RPM configuration to work like the Assembly config? I cannot find any docs that tell me this is possible. BTW my RPM config looks like this:
<mapping>
<directory>/opt/argo/client/bin</directory>
<directoryIncluded>false</directoryIncluded>
<username>argo</username>
<groupname>argogroup</groupname>
<filemode>744</filemode>
<sources>
<source>
<location>src/resources/client/bin</location>
<includes>
<include>*.sh</include>
</includes>
</source>
</sources>
</mapping>
What I would love is to just put true in the mapping and call it a day. Is there any way to do this using the maven-rpm-plugin?
I am thinking of using the maven-replacer-plugin, but that is not as elegant as I'd like.
Any suggestions?
Had the same issue using the postinstallScriptlet configuration, which was solved by following the orientation to use the maven-resources-plugin, that could be seen here: does an external script in rpm-maven-plugin have access to maven properties
So your configuration should be:
for maven-resources-plugin:
<configuration>
<outputDirectory>${basedir}/target/classes/scripts</outputDirectory>
<resources>
<resource>
<directory>src/resources/client/bin</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
for rpm-maven-plugin:
<mapping>
<directory>/opt/argo/client/bin</directory>
<directoryIncluded>false</directoryIncluded>
<username>argo</username>
<groupname>argogroup</groupname>
<filemode>744</filemode>
<sources>
<source>
<location>${basedir}/target/classes/scripts</location>
<includes>
<include>*.sh</include>
</includes>
</source>
</sources>
</mapping>
This way the maven-resources-plugin will filter your maven properties to the copied file, that will be referred on rpm-maven-plugin
Take look at POM Reference, Resources:
▪ filtering: is true or false, denoting if filtering is to be enabled for this resource. ... resources can also use properties that are by default defined in the POM (such as ${project.version})

Excluding Resource while packaging in maven

I have a project with sample structure as :
jcr_root
|_apps
|_A
|_B
|_etc
|_A
|_B
What I need to do is while creating a package, I need to include either "apps/A & etc/A" or "apps/B & etc/B"
In my pom.xml, I tried something like :
<resources>
<resource>
<directory>src/main/content/jcr_root</directory>
<excludes>
<exclude>apps/A/**</exclude>
<exclude>etc/A/**</exclude>
</excludes>
</resource>
</resources>
But still both 'A' and 'B' under apps and etc get included while packaging. I'm using content-package-maven-plugin to build a package that would be deployed on CQ.
I tried putting entries in filter.xml but then it is used while deployment and not while packaging.
It seems, the include/exclude tags are not at all working. For testing, I tried:
<resources>
<resource>
<directory>src/main/content/jcr_root</directory>
<excludes>
<exclude>**/*.otf</exclude>
</excludes>
</resource>
</resources>
But still fonts.otf file was getting included in the packaged zip.
Some help or hints please. Let me know if any more info is required.
Many Thanks in Advance.
So finally I was able to create a package with the excluded resources.
The issue was not with the include/exclude tag(they always worked fine.)
The files after excluding some resource were copied to "target/classes" directory
Issue was that the maven-content-package plugin took the resource to package from original source directory rather than the "target/classes" directory created.Thus it always included everything. This is the default behaviour of maven-content-package plugin.
Thus I had to explicitly tell the plugin that you need to pick the resource to package from "target/classes".
<builtContentDirectory>${basedir}/target/classes</builtContentDirectory>
Please let me know if anyone needs more detail. Thanks for all your answers:)
Since you want to include jcr_root/app/A and jcr_root/etc/A
Please you try using this in POM.xml:
<project>
...
<resources>
<resource>
<directory>jcr_root</directory>
<includes>
<include>**/*A*</include>
</includes>
<excludes>
<exclude>**/*B*</exclude>
</excludes>
</resource>
<resources>
</project>
Similarly you can do it for getting jcr_root/app/B and jcr_root/etc/B.
Regards
Jyotsna

Resources