How to add a classpath in Spring Boot

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.

environment

When booting with mvn spring-boot: run

When starting an application using Spring Boot Maven Plugin. You can add it in one of the following ways.

Specify in the startup argument

You can specify multiple paths separated by commas.

mvn spring-boot:run -Dspring-boot.run.folders=[path]

Write in pom.xml

pom.xml


<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <folders>
            <folder>[path]</folder>
        </folders>
    </configuration>
</plugin>

For Executable Jar

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.

Change the Main class

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.

Specified in system properties

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 ...].

Specify with an environment variable

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]

Specify in the property file

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.

Specify in the manifest file

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>

Summary

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

How to add a classpath in Spring Boot
How to create a Spring Boot project in IntelliJ
How to bind to property file in Spring Boot
How to use CommandLineRunner in Spring Batch of Spring Boot
How to use Lombok in Spring
How to add columns to a table
How to set Spring Boot + PostgreSQL
[Spring Boot] How to get properties dynamically from a string contained in a URL
How to use ModelMapper (Spring boot)
How to change application.properties settings at boot time in Spring boot
How to call and use API in Java (Spring Boot)
How to add the same Indexes in a nested array
How to control transactions in Spring Boot without using @Transactional
[Xcode] How to add a README.md file
How to include Spring Tool in Eclipse 4.6.3?
How to add a new hash / array
How to publish a library in jCenter
How to split Spring Boot message file
Add spring boot and gradle to eclipse
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
Run a Spring Boot project in VS Code
How to display a web page in Java
How to make Spring Boot Docker Image smaller
How to use Spring Boot session attributes (@SessionAttributes)
How to add sound in the app (swift)
How to run a djUnit task in Ant
How to create a theme in Liferay 7 / DXP
How to conditionally add html.erb class in Rails
Java tips-Create a Spring Boot project in Gradle
How to implement a like feature in Rails
How to easily create a pull-down in Rails
Add classpath: to the path specified in spring.datasource.schema
How to not start Flyway when running unit tests in Spring Boot
[Java] Article to add validation with Spring Boot 2.3.1.
How to make a follow function in Rails
How to define multiple orm.xml in Spring4, JPA2.1
[Spring Boot] How to refer to the property file
Spring Boot --How to set session timeout time
How to automatically generate a constructor in Eclipse
How to display characters entered in Spring Boot on a browser and reference links [Introduction to Spring Boot / For beginners]
Sign in to a Spring Boot web application on the Microsoft ID platform
How to set environment variables in the properties file of Spring boot application
How to clear all data in a particular table
How to create a Java environment in just 3 seconds
How to set Dependency Injection (DI) for Spring Boot
How to implement a like feature in Ajax in Rails
A memorandum of addiction to Spring Boot2 x Doma2
How to create a data URI (base64) in Java
How to launch another command in a Ruby program
How to display a browser preview in VS Code
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
[Introduction to Spring Boot] Submit a form using thymeleaf
How to store Rakuten API data in a table
How to mock a super method call in PowerMock
How to test file upload screen in Spring + Selenium
How to convert A to a and a to A using AND and OR in Java
Introduce swagger-ui to REST API implemented in Spring Boot
How to convert a file to a byte array in Java
[Rails 6] How to set a background image in Rails [CSS]