How to make an executable jar select a properties file over time using maven

Hi, I am using maven buil to create an executable jar, I have several property files. if I put the properties file in

src/main/resources 

maven packs them inside the can itself. I do not want this to happen, instead I want to put the properties file in a folder named conf, and I want these properties files to be accessible to jar at runtime.

The reason this is due to the fact that in the future the user may have the flexibility to use multiple property values, such as port number, etc.

I inserted 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 properties file I use is "utility.properties", which is present in src / main / resources

I use this in java code as below

ResourceBundle locationUtilityProp = ResourceBundle.getBundle("utility", locale);

pom.xml, jar,

java.util.MissingResourceException: Can't find bundle for base name utility

un jarred jar , .properties.

maven, , , - , src/main/resources .

+4
2

, , - -

:   .properties src/main/resources maven build

src
 |-main/java/com.abc/.java classes
 |-main/resources/error.properties 

, maven jar, config .properties . jar , ,

example
 |-config
   -error.properties
 |-jar file generated by maven

static Locale locale = new Locale("en", "US");
 static ResourceBundle locationUtilityProp =ResourceBundle.getBundle("error", locale);

pom.xml jar

maven, , src/main/resources

<build>


<resources>
  <resource>
    <directory>src/main/resources</directory>
    <excludes>
      <exclude>**/*.properties</exclude>
    </excludes>  
  </resource>

</resources>      

maven

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

maven .

, , maven ( 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>

+4

, , , . , -cp ( -classpath) , yout utility.properties. , :

java -cp ./conf -jar youApp.jar 

, config conf, .

classpath.

0

All Articles