Last time, when I was investigating MessageSource related, I needed to put the property file that manages the message outside the Spring Boot project. Reload Spring MessageSource
At that time, when I investigated how to add a classpath with Spring Boot, there were several methods, so I will summarize them.
The settings may differ depending on the startup method, and there may be several setting methods.
mvn spring-boot: run
When starting an application using Spring Boot Maven Plugin. You can add it in one of the following ways.
You can specify multiple paths separated by commas.
mvn spring-boot:run -Dspring-boot.run.folders=[path]
pom.xml
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<folders>
<folder>[path]</folder>
</folders>
</configuration>
</plugin>
When creating an Executable Jar and executing it with the java
command.
By the way, the classpath is not added even if you do the following.
Bad example
java -cp [path] -jar jarFile
The java
command ignores -cp
if -jar
is specified.
You can add a classpath by using PropertiesLauncher
as the Main class.
If you are using Spring Boot Maven Plugin, you can use Properties Launcher
by setting layout
to ZIP
or DIR
.
pom.xml
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
</configuration>
</plugin>
Next, regarding the settings related to the classpath to be added, there are several setting methods. They are listed in descending order of priority.
You can add the classpath with -Dloader.path
at startup.
java -Dloader.path=[path] -jar jarFile
By the way, I ran it like -Dloader.path = [path]
after -jar
and it didn't work, so I was worried for a while.
The java
command must be specified in the orderjava [-options] -jar jarFile [args ...]
.
Set to the environment variable LOADER_PATH
.
You can start it by using the java
command normally.
windows
set LOADER_PATH=[path]
linux
export LOADER_PATH=[path]
Place the configuration file with the file name loader.properties
directly under resources.
This is also OK if you use the java
command normally to start it.
loader.properties
loader.path=[path]
The file name and location can be changed by setting.
You can add the classpath by specifying Loader-Path
in the manifest file.
Start with the java
command normally.
If you use Maven Jar Plugin, you can set as follows.
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Loader-Path>[path]</Loader-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
There are several ways to use Executable Jar, but I wondered if system properties or environment variables that can be set at startup would be good.
In the case of property files and manifest files, the path is written in solid code, so I feel that it is difficult to do if the settings are different for each environment.
Or you can create a property file for each environment and specify the property file name in the system properties at startup.
Recommended Posts