[JAVA] How to use Maven to place resource files outside the JAR

The problem to be solved is as the title says. In a normal Java program, resource files are placed under src / main / resources, and these resource files are included in the JAR at build time. However, there are cases where you want to put these resource files in an external folder instead of inside the JAR. For example, by placing the property file outside the JAR, you can change the behavior of the program without building even if environment-dependent settings such as DB connection information are changed. This article describes how to use Maven to achieve such a build.

Specify an external resource folder

You can use maven-jar-plugin to put resources externally by passing an external folder through the classpath instead of src / main / resources. The following is a sample to set the settings folder in the same path as the JAR file by specifying the relative path.

As a prerequisite, assume that the installation folder structure of the JAR file is as follows.

Installation folder structure


install_dir
 L MY_JAR_FILE.jar
 L settings
   L my_resource.properties

Describe as follows in pom.xml.

pom.xml


<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.gmail.greencoffeemaker.MainApp</mainClass>
            <addClasspath>true</addClasspath>
          </manifest>
          <manifestEntries>
            <!--If you specify a folder, at the end of the path/Don't forget to put on-->
            <Class-Path>./settings/</Class-Path>
          </manifestEntries>
        </archive>
      </configuration>
    </plugin>
  </plugin>
</build>

From Java programs, you can use the ResoruceBundle class to handle resources without being aware of the path.

Resource acquisition sample


ResourceBundle res = ResourceBundle.getBundle("my_resource");
String val = res.getString("mykey");

Make debugging easier

If you specify the resource folder externally by the above method, you have to set the external folder in the execution configuration when debugging with Eclipse, which is troublesome. To avoid this, place the resource file under src / main / resources according to Java's method, and change the build configuration so that the resource file is output under target at build time. This will eliminate the hassle of running debugs. (Use the resource file under target for deployment)

To output the resource file, use maven-antrun-plugin and execute the ant command. There are two commands to execute: "Create output directory" and "Copy resource file".

As a prerequisite, assume that the project structure is as follows:

Project structure


my-project
 L src/main/java
 L src/main/resource
    L my_resource.properties
 L target

Describe as follows in pom.xml.

pom.xml


<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <!--Omission-->
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-antrun-plugin</artifactId>
      <executions>
        <execution>
          <phase>package</phase>
          <configuration>
            <target>
              <!--copy: /src/main/resources => /target/settings -->
              <mkdir dir="${project.build.directory}/settings" />
              <copy todir="${project.build.directory}/settings">
                <fileset dir="${project.basedir}/src/main/resources" />
              </copy>
            </target>
          </configuration>
          <goals>
            <goal>run</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugin>
</build>

When building with Maven, the JAR file is created under target and the settings folder is created at the same time, and the resource file is copied.

Build command


$> mvn clean package

Project configuration (after build)


my-project
 L src/main/java
 L src/main/resource
    L my_resource.properties
 L target
    L MY_JAR_FILE.jar
    L settings
        L my_resource.properties

that's all.

Recommended Posts

How to use Maven to place resource files outside the JAR
How to use the link_to method
How to use the include? method
How to use the form_with method
How to use the wrapper class
How to place and share SwiftLint config files on the server
How to add local jar to maven pom.xml
How to use binding.pry for view files
[Java] How to use the File class
[Java] How to use the hasNext function
[Java] How to use the HashMap class
[Rails] How to use the map method
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
[Processing × Java] How to use the function
[Java] How to use the Calendar class
How to place geckodriver (Selenium WebDriver) on the path using Maven command
How to use the camera module OV7725 (ESP32-WROVER-B)
[Java] How to use Thread.sleep to pause the program
When you want to use the method outside
Output of how to use the slice method
How to use the replace () method (Java Silver)
How to get resource files out with spring-boot
How to build an executable jar in Maven
[Ruby basics] How to use the slice method
How to play MIDI files using the Java Sound API (specify the MIDI device to use)
Add files to jar files
Introduction to JAR files
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to use Maven that I can't hear anymore
[Rails] I don't know how to use the model ...
[Beginner] Discover the N + 1 problem! How to use Bullet
How to specify the resource path in HTML import
How to debug the generated jar file in Eclipse
[Ruby] How to get the tens place and the ones place
How to use the getter / setter method (in object orientation)