Accessing module root path from spring cloud config server - spring-boot

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!

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.

Spring Boot not loading the active application.properties file

I'm using Spring Boot to activate profiles based on the environment as documented here
For the most part it works. However, I've hit an odd scenario.
I have three application-{profile}.properties files as follows :
application-dev.properties
application-uat.properties
application-release.properties
When I deploy a war file to uat, Spring is picking up a JDBC connection from the release file. The application.properties file contains one line -
spring.profiles.active=uat
If I change the name of the application-release.properties file to application-release.properties.tmp then Spring picks up the connection from application-uat.properties.
Any ideas.
BTW I'm using Spring.Boot 2.2.0-RELEASE
Solved!
Not sure which of the two changes that I made solved the issue but here it is.
When reading the comments in Daniel Olszewski's article, Marcelo Martins says that it is unnecessary to specify resource filtering in pom.xml when using 'spring-boot-starter-parent' which was further confirmed by Daniel. As I am using 'spring-boot-starter-parent', I removed it.
To solve my problem I re-inserted it.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
I upgraded the maven plugin to 2.2.0.RELEASE
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.0.RELEASE</version>
</plugin>

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)

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

How to filter resources when using maven jetty plugin?

I have an XML file (urlrewrite.xml) that needs a property placeholder resolved. I enable Maven filtering to achieve this. This works fine for the assembled WAR file.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
The problem is when trying to run the application in development mode using the maven-jetty-plugin (Maven Jetty Plugin), as maven jetty:run .
The file in question, urlrewrite.xml, is located in the src/main/resources directory, and therefore should (and does) ends up in /WEB-INF/classes (or target/classes for maven jetty:run).
The URLRewriteFilter config specifies the location of the config file as follows:
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>confPath</param-name>
<param-value>/WEB-INF/classes/urlrewrite.xml</param-value>
</init-param>
</filter>
This will work at deployment time. However, Using the jetty maven plugin, URLRewrite will die with a NullPointerException because it uses context.getResourceAsString("/WEB-INF/classes/urlrewrite.xml") in order to load the config file. Jetty returns null for this because when running the application from workspace it resolves /WEB-INF/classes/... to src/main/webapp/WEB-INF/... . The file does not exist there because the WAR has not yet been assembled. It should instead pull the resource from target/classes/urlrewrite.xml.
If that is obscure to you, then you probably won't be able to answer this question because I suspect you will need to be a Jetty guru to figure out a workaround (hint: that's a challenge!).
Does anyone know a way around this? I have also tried the following workarounds to know avail:
Put urlrewrite.xml under a new directory, src/main/webResources and add it to the maven war plugin <webReources> and enable filtering. That will copy it's contents in the appropriate location when the WAR is packaged, but will not make it available for jetty:run
Some other hacks I can't even remember ... (will update if I do)
In summary, maven-jetty-plugin needs the file to be under src/main/resources/webapp/insert path and filename in order to be available for the maven jetty:run command ...
Thanks for you help ...
Sincerely,
Lloyd Force
Answered my own question.
Upgrade maven-jetty-plugin to at least 6.1.12
See this wiki page on 'Configuring Multiple WebApp Source Directory' (available since jetty-6.1.12.rc2 and jetty-7.0.0pre3)
Add some magic to pom.xml:
First, add a new directory (src/main/webResources) for your filtered web resources and add a <resource> element:
<resource>
<directory>src/main/webResources</directory>
<filtering>true</filtering>
<targetPath>../jettyFilteredResources</targetPath>
</resource>
That will copy the files to target/jettyFilteredResources (we will reference this later). This directory will NOT get copied to your packaged WAR file, it is for jetty only!
Add the following element to your maven-war-plugin <configuration> element:
<webResources>
<resource>
<directory>src/main/webResources</directory>
<filtering>true</filtering>
</resource>
</webResources>
That will ensure everything is packaged up for your real WAR file.
Finally, tell jetty to use the resources your copied especially for it, by added the following snippet to your <baseResource> element:
<baseResource implementation="org.mortbay.resource.ResourceCollection">
<resourcesAsCSV>src/main/webapp,target/jettyFilteredResources</resourcesAsCSV>
</baseResource>
Now everything will worketh! (Well, technically I haven't tested the production WAR yet, but ... blah ... it should work too).
If anyone has a better answer, I will accept it provided the answer is provided in a reasonable amount of time (say 1 day).
I think the answer in this other question is better:
Running resource filters when using jetty:run
Basically, instead of running 'mvn jetty:run' you have to use 'mvn jetty:run-exploded'.
The only drawback is that it needs to build the WAR file, which might be expensive in some cases. If that's not an issue for you, then I guess it's better.
add this to pom.xml:
<resources>
<resource>
<directory>src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>../jettyFilteredResources</targetPath>
</resource>
</resources>
and this is how embedded Jetty server should look like:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.1.3.v20140225</version>
<configuration>
<webAppConfig>
<descriptor>target/jettyFilteredResources/web.xml</descriptor>
</webAppConfig>
<scanIntervalSeconds>3</scanIntervalSeconds>
</configuration>
</plugin>
woila! thanks #les2 for inspiration ;-)
I found another way.
build the project and add the target folder as extra classpath.
<webAppConfig>
....
<extraClasspath>${basedir}/target/mywebapp</extraClasspath>
....
</webAppConfig>

Resources