Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a executable jar pick the properties file during time time using maven

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.

like image 981
sugar Avatar asked Mar 01 '15 05:03

sugar


People also ask

How do I create an executable jar from Maven?

In order to compile the project into an executable jar, please run Maven with mvn clean package command.

How do you access the properties file in a library jar file?

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().


1 Answers

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>

like image 188
sugar Avatar answered Oct 25 '22 03:10

sugar