Hi i am using maven buil to create an executable jar, i have few properties file. if i place the properties file in
src/main/resources
maven packages them inside the jar itself. I dont want this to happen, instead i want to place the properties file in a folder called conf and i want these properties file to be avalable to the jar during runtime.
The reason why this is because in future the user can have the flexibility to chnage a few property values like port number etc without.
i have pasted the pom.xml below
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hp.nfv</groupId>
<artifactId>DescriptorA</artifactId>
<version>1.0.0</version>
<name>DescriptorA/name>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.abc.Descripto</mainClass>
</manifest>
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
the property file i am using is 'utility.properties' which is present in the src/main/resources
i am using this in the java code as below
ResourceBundle locationUtilityProp = ResourceBundle.getBundle("utility", locale);
but when i execute the above pom.xml file, i get a jar file , which on running gives the below error
java.util.MissingResourceException: Can't find bundle for base name utility
when I un jarred the jar file, i found no .properties files in it.
I am completely new to maven so please can anyone help me make this jar pick the properties file from a directory structure other than src/main/resources at run time.
In order to compile the project into an executable jar, please run Maven with mvn clean package command.
inside a jar, in the WEB-INF/lib folder, a folder passed with -cp to java command, etc..) you can read the file using the getResourceAsStream method of the Class class. So: InputStream is = this. getClass().
I got this requirement working , and below i have answered in detail so that it can help someone else someday
package structure: you need to place .properties files inside src/main/resources when doing a maven build
src
|-main/java/com.abc/.java classes
|-main/resources/error.properties
after maven generates a jar file, then create a folder called config and copy all .properties files inside it. and place your jar file in same directory as your config folder as below
example
|-config
-error.properties
|-jar file generated by maven
code to fetch the resources files
static Locale locale = new Locale("en", "US");
static ResourceBundle locationUtilityProp =ResourceBundle.getBundle("error", locale);
pom.xml to create an executable jar file
first tell maven not to include any of properties files present inside src/main/resources
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
now include maven compiler plugin
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
now include the maven assembly plugin to create an executable jar.
in the mention the folder name where you want maven to pick the resource files( in my case error.properties)
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.abc.hello</mainClass>
</manifest>
<manifestEntries>
<Class-Path>config/</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With