Executable JAR - JVM Arguments - Externalized Config Properties - Spring Boot Maven Plugin - spring-boot

We have a spring boot app with externalized configuration in a properties file. Executing the jar file like so works:
java -jar -Dspring.config.location="application.properties" app.jar
Executing the jar directly like below works, but without the properties file. Right now, none of these options below take the properties file.
./app.jar
./app.jar -Dspring.config.location="file:./application.properties"
I was hoping we could do something in the pom.xml or in the command that executes to achieve this.
FYI, we have this plugin in the pom.xml to build us the jar file:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
<includeSystemScope>true</includeSystemScope>
<!-- This below does not work -->
<!-- <jvmArguments> -->
<!-- -Dspring.config.location=application.properties -->
<!-- </jvmArguments> -->
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>

Create file: app.conf (filename is the same as your jar) with content:
JAVA_OPTS="-Dspring.config.location=application.properties"
Similar question was answered here. Documentation for customizing start script can be found here.

Related

How to pass add-module to spring boot application?

I am using a maven dependency which require me to pass add-module during compilation and runtime as mentioned here.
Can someone let me know how can I pass --add-module option to a spring boot application during compilation and runtime? It will be good if I can control both compilation and runtime behaviour from the pom.xml.
For compilation you can use the maven-copmiler-plugin in your build of your pom.xml.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<parameters>true</parameters>
</configuration>
</execution>
</executions>
<configuration>
<source>14</source>
<target>14</target>
<compilerReuseStrategy>reuseSame</compilerReuseStrategy>
<compilerArgs>
<arg>--enable-preview</arg>
<arg>--add-modules=jdk.incubator.foreign</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
As you see I have included also the --enable-preview. In JDK14 as reported in JEPS 370 it is not needed since this is not a preview feature. But keep an eye on it since it might be needed in other jdk versions since in some versions this belongs in preview features.
As for runntime a spring-boot application is normally just a .jar executable file which you start with the normal java -jar myApp.jar command.
According to oracle documentation, the format of the command line is
To execute a JAR file:
java [options] -jar jarfile [args...]
So the command you want would be
java --add-modules jdk.incubator.foreign -jar myApp.jar

Running flyway commands from command line using spring boot properties for maven

I have a spring boot project using maven that I've included flyway in:
pom.xml:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>6.5.0</version>
</dependency>
and application.properties:
#LOCAL
spring.datasource.url=jdbc:postgresql://localhost:5432/theDatabase
spring.datasource.username=theRightUser
spring.datasource.password=theRightPassword
and it works as expected when I run the application.
However, I'm trying to run mvn flyway:clean from the command line, and it doesn't seem to recognize the configuration correctly:
[ERROR] Failed to execute goal org.flywaydb:flyway-maven-plugin:6.4.4:clean (default-cli) on project my-service: org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password! -> [Help 1]
I tried adding spring.flyway properties (user/pass/url) in the application.properties file but it gave me the same error. What do I need to do to get flyway to read from the application.properies like it does when the application runs normally?
EDIT: I have made slight progress: I was able to reference my application.properties as a flyway config file by adding this to the pom.xml:
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>6.5.0</version>
<configuration>
<configFiles>${project.basedir}/src/main/resources/application.properties</configFiles>
</configuration>
</plugin>
So now in that file, I have flyway.url, flyway.user and flyway.password. This allows me to run flyway goals from the command line, but is not totally the solution I want. I am looking into using this plugin to try and read the properties into the pom.xml file, and then using those values in the flyway-maven-plugin's <configuration> area.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${project.basedir}/src/main/resources/application.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
which would allow me to do this:
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>6.5.0</version>
<configuration>
<url>${spring.datasource.url}</url>
<user>${spring.datasource.username}</user>
<password>${spring.datasource.password}</password>
</configuration>
</plugin>
When you run flyway as maven goal it will not pick up the properties from the application.properties, instead it will use the configuration provided by the flyway-maven-plugin, you can configure the flyway-maven-plugin in the following way -
Add the following plugin to pom.xml -
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>6.5.0</version>
</plugin>
Then we will configure the flyway-maven-plugin, the Flyway Maven plugin can be configured in a wide variety of following ways (most convenient),
Configuration section of the plugin
The easiest way is to simply use the plugin’s configuration section in your pom.xml:
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>6.5.0</version>
<configuration>
<driver>org.hsqldb.jdbcDriver</driver>
<url>jdbc:hsqldb:file:${project.build.directory}/db/flyway_sample;shutdown=true</url>
<user>SA</user>
<password>mySecretPwd</password>
<connectRetries>10</connectRetries>
<initSql>SET ROLE 'myuser'</initSql>
<schemas>
<schema>schema1</schema>
<schema>schema2</schema>
<schema>schema3</schema>
</schemas>
<callbacks>
<callback>com.mycompany.project.CustomCallback</callback>
<callback>com.mycompany.project.AnotherCallback</callback>
</callbacks>
<skipDefaultCallbacks>false</skipDefaultCallbacks>
<cleanDisabled>false</cleanDisabled>
<skip>false</skip>
<configFiles>
<configFile>myConfig.conf</configFile>
<configFile>other.conf</configFile>
</configFiles>
<workingDirectory>/my/working/dir</workingDirectory>
</configuration>
</plugin>
Maven properties
To make it easy to work with Maven profiles and to logically group configuration, the Flyway Maven plugin also supports Maven properties, update the properties section in pom.xml as follows:
<project>
...
<properties>
<!-- Properties are prefixed with flyway. -->
<flyway.user>myUser</flyway.user>
<flyway.password>mySecretPwd</flyway.password>
<!-- List are defined as comma-separated values -->
<flyway.schemas>schema1,schema2,schema3</flyway.schemas>
<!-- Individual placeholders are prefixed by flyway.placeholders. -->
<flyway.placeholders.keyABC>valueXYZ</flyway.placeholders.keyABC>
<flyway.placeholders.otherplaceholder>value123</flyway.placeholders.otherplaceholder>
</properties>
...
</project>
External Configuration File
Another way is to create a separate .properties file, the default configuration file name is flyway.properties and it should reside in the same directory as the pom.xml file. Encoding is specified by flyway.encoding (Default is UTF-8):
flyway.user=databaseUser
flyway.password=databasePassword
flyway.schemas=schemaName
...
If you are using any other name (e.g customConfig.properties) as the configuration file, then it should be specified explicitly when invoking the Maven command:
$ mvn <goals> -Dflyway.configFile=customConfig.properties
After configuring your flyway-maven-plugin, with the desired configuration, we will be able to execute flyway maven goals from command line.
You can do further reading here.
Hope this helps!

How to externalise .properties files when deploying a Spring Boot WAR in Wildfly Server?

I am developing a web application using Spring Boot, and want to generate war instead of jar.
It works fine using the conversion from jar to war described here : http://spring.io/guides/gs/convert-jar-to-war/
But I want to externalize the application.properties from the war, because I want to be able to modify it without opening the war archive.
I already defined the spring-boot-maven plugin.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>
com.application.Application
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
I think I need to add Dependency: config to my manifest file.
So, I've done it like that :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Dependencies>config</Dependencies>
</manifestEntries>
</archive>
</configuration>
</plugin>
But when I launch the Application.war on Wildfly 8.4, I've got this
{"JBAS014671: Failed services" => {"jboss.module.service.\"deployment.Application.war\".main" => "org.jboss.msc.service.StartException in service jboss.module.service.\"deployment.screening.war\".main: JBAS018759: Failed to load module: deployment.Application.war:main
Caused by: org.jboss.modules.ModuleNotFoundException: config:main"}}
I would like that my application start with my custom MANIFEST.MF (with Dependency: config) so that I can externalize my application.properties file.
Thank you.
The problem was on the server side !
When you specify a package, you need to add a module.xml file with a Wildfly server.
So in modules/config/main/, I've added my application.properties and a module.xml file that contains :
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="config">
<resources>
<resource-root path="."/>
</resources>
</module>
Thank you for your response #NielsNet.

When does a Maven plugin uses the POM in the current directory?

I use the Versions Maven Plugin to check for updates of my dependencies. Therefore I added the following lines to my pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>${versions-plugin.version}</version>
<configuration>
<rulesUri>classpath:///rules.xml</rulesUri>
</configuration>
<dependencies>
<dependency>
<groupId>versionrules</groupId>
<artifactId>versionrules</artifactId>
<version>1-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
But this configuration is not used if I run the Versions Maven Plugin from the commandline in the same directory as the pom.xml. The only way to use my own configuration is to put this plugin configuration in a profil and execute this profil during the Maven run.
Is there a way to run the Versions plugin on the commandline and to configure it via the pom.xml? I am sure my questions does not only apply to the Versions plugin, but to any Maven plugin.
This can be done by using an execution id default-cli in your execution definition the configuration will be used during the execution on command line (using the current configuration) furthermore since Maven 3.3.1 you can use things like:
mvn version:set#second-cli
which means you can do a different configuration for command line in the pom file:
Just by simply separating them by different id
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.5.</version>
<executions>
<execution>
<id>default-cli</id>
<configuration>
...
</configuration>
</execution>
<execution>
<id>second-cli</id>
<configuration>
....
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
So this means you can have different configurations for running on command line by giving the id.

Specify javaagent argument with Maven exec plugin

I have a similar question to: this previous question
I am converting a Java project using Netbeans to Maven. In order to launch the program, one of the command-line arguments we need is the -javaagent setting. e.g.
-javaagent:lib/eclipselink.jar
I'm trying to get Netbeans to launch the application for development use (we will write custom launch scripts for final deployment)
Since I'm using Maven to manage the Eclipselink dependencies, I may not know the exact filename of the Eclipselink jar file. It may be something like eclipselink-2.1.1.jar based on the version I have configured in the pom.xml file.
How do I configure the exec-maven-plugin to pass the exact eclipselink filename to the command line argument?
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Xmx1000m</argument>
<argument>-javaagent:lib/eclipselink.jar</argument> <==== HELP?
<argument>-classpath</argument>
<classpath/>
<argument>my.App</argument>
</arguments>
</configuration>
</plugin>
I figured out a way that seems to work well.
First, setup the maven-dependency-plugin to always run the "properties" goal.
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>getClasspathFilenames</id>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
Later on, use the property it sets as documented here with the form:
groupId:artifactId:type:[classifier]
e.g.
<argument>-javaagent:${mygroup:eclipselink:jar}</argument>
Simply define a property for the eclipse link version and use the property in your <dependency> and the exec plugin:
<properties>
<eclipselink.version>2.4.0</eclipselink.version>
</properties>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>${eclipselink.version}</version>
</dependency>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Xmx1000m</argument>
<argument>-javaagent:lib/eclipselink-${eclipselink.version}.jar</argument>
<argument>-classpath</argument>
<classpath/>
<argument>my.App</argument>
</arguments>
</configuration>
</plugin>
the maven-dependency-plugin and exec-maven-plugin should be put under the node ,otherwise it will not work

Resources