How to put maven project version in log file name (log4j.xml) - maven

I want to generate log file according to maven project version.
In the the log4j.xml file I add the following line:
<appender name="rollingFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${catalina.home}/logs/ussdMoneyTransfert_logFile${project.version}.log" />
</appender>
but it doesn't work.

This can be done by enabling resource filtering:
<project>
...
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
</resources>
...
</build>
...
</project>
When resource filtering is enabled, Maven, by default, is replacing all occurences of ${...} with a property value (if one exist for that name). Properties can come from the system properties, your project properties, from your filter resources and from the command line.
${project.version} is one property that Maven adds during the build. It will be replaced by the current Maven project version during filtering.

after enabling resource filtering in springboot i change ${project.version} to #project.version# and it works fine!.

The answer is very simple... Let's says you have a pom.xml and log4j2.yml
pom.xml
<project xmlns="http://maven....Schema-instance"
....
<artifactId>ArtifactName</artifactId>
<version>2.36.2-SNAPSHOT</version>
Add #project.version# to the log file name this will append the version to the actual log file name.
log4j2.yml
Configuration:
name: "AppName"
monitorInterval: 30
status: info
....
RollingFile:
- name: json-log-file
fileName: "${sys:tomcat.log.dir}/${app_log}-#project.version#.log.json"
filePattern: "${app_log}-#project.version#-%d{MM-dd-yyyy}-%i.log.gz"
If you noticed this is reading it from pom.xml directly, where the project is parent tag and the version is child tag.

Related

Get value from pom.xml into a yaml file

I am using swagger ui and im trying to read some properties in pom.xml into yaml file (Like version, artifactId for example), but i am get this error:
Parser error on line 3
bad indentation of a mapping entry
Full error here
The head of my openapi.yaml file. I need to use the artifactId in the title.
openapi: 3.0.3
info:
title: #artifactId#
Do i need to make something with the pom.xml? Export the file? Or there is another way to retrieve data from pom.xml?
You can use the resource filtering functionality provided by maven. link to doc
Change your openapi.yaml file to:
openapi: 3.0.3
info:
title: ${artifactId}
And add the following section to your pom.xml. (assuming that your openapi.yaml is in the resources directory)
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
Maven build will create an openapi.yaml file with the substituted artifactId in the target directory.
try using single quote:
api:
version: '#project.version#'
and inside the pom.xml,
<project>
...
<version>0.0.1-SNAPSHOT</version>
...
</project>
Another important thing!
If you have using some certificate or binaries files, it's also important to configure something like:
`<plugins>
<!-- allow resource files to contain references to Maven properties like ${prop.name} -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<!-- exclude all binary files from filtering! -->
<nonFilteredFileExtension>p12</nonFilteredFileExtension>
<nonFilteredFileExtension>crt</nonFilteredFileExtension>
<nonFilteredFileExtension>pem</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>`
Because can be you corrupt your certs if you don't do this.

Accessing module root path from spring cloud config server

I have following modules in my project:
root-mod
+ cloud-config-server
+ cloud-config-server-config-files
+ microservice-1
The directory cloud-config-server-config-files contain configuration files for the application and is a git repository. I would like to access these files from application.yml of cloud-config-server module. The problem here is that the following does not work: ${project.baseDir}/cloud-config-server-config-files/. Is there a way to access the location from application.yml of my config server?
Although not the most elegant solution(will keep this post open so that anyone with a better answer can answer), following worked for me:
I created a property in the pom.xml of cloud-config-server:
<properties>
<git-local-url>${project.basedir}/../cloud-config-server-config-files</git-local-url>
</properties>
Then added the resources directory to the <build> tag:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
Access this variable in application.yml:
uri: #git-local-url#
Ofcourse, this should be done based on the profiles there fore its best if the property is defied in the profile instead.
Cheers!

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.

Eclipse error using Maven filtering in persistence.xml

I have a Maven project with JPA using hibernate.
I had to specify a jar file to load external classes in persistence.xml located in src/main/resources/META-INF
<persistence unit name="PersistenceUnit" transaction-type="JTA"
...
<jar-file>lib/${project.persistencejar}.jar</jar-file>
using Maven filtering (the filename can change based on various Maven settings).
I instruct then Maven to filter by including in the pom.xml
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>META-INF/persistence.xml</include>
</includes>
</resource>
</resources>
and running mvn clean install produces a deployable and working ear.
The problem is: if I keep JPA Facet enabled in Eclipse project configuration, Eclipse complains that
Multiple annotations found at this line:
- JAR file "lib/${project.persistencejar}.jar" cannot be resolved
- The path to the JAR file will vary on your runtime environment. Please make sure the
specified path fits your particular environment.
and disabling it doesn't build my JPA project everytime that I update it (thus having to run Maven everytime there is a change in the persistence. Is there a workaround to tell Eclipse where he can find the jar at development time? Or maybe I am using the wrong approach to filtering?
Thank you
If the problem only in incorrect error message from Eclipse, you can disable it in
Window->Preferences->Java
Persistence->JPA->Errors/Warnings->Persistence unit
Just set "Ignore" for "JAR file cannot be resolved"
I have solved it this way:
in persistence.xml substitute the whole <jar-file> tag, i.e.
<persistence-unit name="PersistenceUnit" transaction-type="JTA">
...
<!-- here is where the jar file is supposed to go -->
${importjarfile}
...
</persistence-unit>
Then include in pom.xml the property
<properties>
<!-- jar of persistence -->
<importjarfile><![CDATA[<jar-file>lib/${project.persistencejar}.jar</jar-file>]]></importjarfile>
</properties>
or alternatively in src/main/filters/profilename.properties file the line
importjarfile = <jar-file>lib/${project.persistencejar}.jar</jar-file>
In this way Eclipse won't see the jar-file tag and won't complain. At build time, maven will generate the correct persistence xml.

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