How to set a spring datasource url to a resources folder? - spring-boot

I have a hsqldb file in my project resources folrder "src/main/resources/mydb.data"
In my application.properites I need to set the path with spring.datasource.url=
How I can achieve that?
spring.datasource.url=.... ?

One solution would be to use Maven resource filtering. Just use something like this in your application.properties:
spring.datasource.url=jdbc:hsqldb:file:#basedir#/src/main/resources/mydb.data
Instead of using basedir you can define something like darasource-url in your pom.xml and use this property in your application.properties:
pom.xml
<project>
<!-- ... -->
<properties>
<datasource-url>jdbc:hsqldb:file:${basedir}/src/main/resources/mydb.data</datasource-url>
</properties>
<!-- ... -->
</project>
application.properties:
spring.datasource.url=#datasource-url#
Notice: Using the spring-boot-starter-parent as parent, you have to use #..# instead of ${..}, see 2.1.1. Automatic Property Expansion Using Maven in the Spring Boot How-to-Guides.

Related

How to get data for a particular log4j2 property from an alternative source (application.properties)

Having a standard configuration for log4j2 and spring property-file on classpath application.property.
log4j2.xml
<Properties>
...
<Property name="APP_LOG_ROOT">${bundle:application:log.root.dir}</Property>
...
</Properties>
application.properties
...
log.root.dir=/opt/tomcat/logs
...
The data is read into the log4j2.xml correctly, but what if I want to get an alternative property when creating an artifact with maven and put diferent application.property:
mvn clean install -Dapplication.properties.path=file:/some_path/application.properties
?
After that, I can correctly read the new properties.
#Value("${log.root.dir}")
private String ololo;
but the log4j2 cannot do this on its own.
If you want to use any value from Spring's Environment in a Log4j2 configuration file, you need to use the Spring Boot Lookup.
After adding log4j-spring-boot to your classpath, you just need to replace
${bundle:application:log.root.dir}
with
${spring:log.root.dir}

Creating Maven archetype using required properties

I have created my own archetype which defines in archetype-metadata.xml a required property:
<requiredProperty key="version.wildfly">
<defaultValue>16.0.0.Final</defaultValue>
</requiredProperty>
This property needs to be used in src/main/resources/archetype-resources/pom.xml as a property:
<properties>
<version.server.bom>${version.wildfly}</version.server.bom>
</properties>
Indeed, when I create a project using this archetype, I'm being asked to confirm the default value for the property:
version.wildfly: 16.0.0.Final
Y: : Y
However, in the generated project's pom.xml, it is not specified anywhere to use this property. The pom.xml merely contains:
<properties>
<version.server.bom>${version.wildfly}</version.server.bom>
</properties>
And thus the build fails. Did I use any wrong pattern to inject the property in the pom.xml ?
Thanks
Don't use dot, try version-wildfly.
looks like archetype consider dot-split property as internal use. see Custom Properties

How does maven replace text #env# in application.yml?

There is some profiles defined in pom.xml.
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
spring.profiles.active: #env# defined in application.yml and bootstrap.yml.
When I run mvn install -P test, text #env# in application.yml would be replaced by test.
How does it work?
Why it does't work for bootstrap.xml?
It works for application.yml because you are obviously using the Spring Boot Starter Parent. See the POM here: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-starters/spring-boot-starter-parent/pom.xml
The magic part is the <resources> configuration within that parent POM. You see that the application config files are explicitly copied with filtering. That is why the maven-resources-plugin resolves placeholders in these files.
If you want to add more files to be handled like this you can add your own <resources> section to your POM and extend it by more file patterns.

Add maven build time to jar name build with Spring Boot Maven Plugin

How can add maven build time to jar file name using Spring Boot Maven plugin?
I want to achieve something like: jar_name-build_time.jar
By default, Spring Boot Maven Plugin builds jar file with name ${project.build.finalName}.
This can be configured with non-required property finalName.
Maven build time can be used as ${maven.build.timestamp}
So, putting all things together, all you need to do is append build time to default jar name:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.6.RELEASE</version>
<configuration>
<mainClass>com.marand.thinkmed.meds.config.boot.MedsConfigApplication</mainClass>
<finalName>${project.build.finalName}-${maven.build.timestamp}</finalName>
</configuration>
</plugin>
Also, make sure to change timestamp format so it doesn't violate file naming strategies:
<properties>
<maven.build.timestamp.format>yyyy-MM-dd-HH-mm</maven.build.timestamp.format>
</properties>

How to access properties from settings.xml to spring.xml and pom.xml to spring.xml

I am new to maven spring.I have two questions.
How to access property set in settings.xml to spring.xml
How to accesss property set in pom.xml to spring.xml
Thank you.
You want to filter your resources, a maven term that means replacing property placehoders in one file with values from your maven pom. To do this you use the resources plugin in your build configuration.

Resources