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.
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");
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