Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Exclude properties file from jar file?

Tags:

java

maven

People also ask

How do I change the properties of a jar file?

Use 7zip, right click on jar and with 7z say open archive, then go to property file, double click and open file with notepad, edit it and save. Now when you will close 7z console, it will ask "update archive?" Say yes... That's it.

How do you exclude a jar file while building a application?

To exclude any file from a jar / target directory you can use the <excludes> tag in your pom.


I encountered this scenario as well. Basically, you want to be able to run your code locally from Eclipse using some userConfig.properties file that is readily accessible, such as inside /src/main/resources. Additionally, you want to provide a compiled executable JAR with an external userConfig.properties that allows the user to configure the application without cracking the JAR.

My implementation is as follows: running mvn clean install will:

  • create executable JAR with the specified mainClass
  • exclude all .properties files located in src/main/resources from the JAR
  • copy project dependencies into a lib folder in your project root
  • copy all .properties files located in src/main/resources into a conf folder in your project root. Note that this step is an optional convenience for your users of the JAR. You can require the explicitly create this file in the conf directory. This conf directory is effectively added to your runtime classpath via the manifest.
  • add this conf folder to the manifest, providing access to it from your executable JAR

Using these Maven plugins in conjunction with each other in your POM configuration will give you what you need. As to the "best practices" of this solution; I'm not sure.

Using maven-dependency-plugin as follows:

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

Using maven-jar-plugin as follows:

<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>package.path.to.your.main.class.MainClass</mainClass>
      </manifest>
      <manifestEntries>
        <Class-Path>conf/</Class-Path>
      </manifestEntries>
    </archive>
  </configuration>
</plugin>

Using maven-resources-plugin as follows:

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

Using this project setup, I can run in Eclipse using one config, and provide my users a properties file to configure, without properties stepping on each other.


You shoud take a look at this.

<project>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <excludes>
            <exclude>**/userConfig.properties</exclude>
          </excludes>
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

I think you're probably already doing it correctly, depending on where you configured that exclude (check with jar tf what files really are in your jar). Most likely, you're running into trouble because the file is not on your classpath and thus your app can't find it. Try adding the directory with the resources file in it to the classpath.

You might want to put in some defaults and let the application read in its configuration from a predefined path, to avoid having to mess with the classpath.