java - How to Exclude properties file from jar file? -


i have java application following project structure:

myproject   |   |----src   |     |   |     |--main   |     |   |     |--resources   |           |      |           |--userconfig.properties   |           |--log4j.properties   |   |---target 

i using maven build project. using maven command build jar file follows:

mvn package -dmaven.test.skip=true 

i want exclude userconfig.properties file jar file have updated pom.xml follows:

<excludes>     <exclude>**/userconfig.properties</exclude> </excludes> 

but excludes target folder in compiled code resides. , application won't run because unable find userconfig.properties file.

can me?

i encountered scenario well. basically, want able run code locally eclipse using userconfig.properties file readily accessible, such inside /src/main/resources. additionally, want provide compiled executable jar external userconfig.properties allows user configure application without cracking jar.

my implementation follows: running mvn clean install will:

  • create executable jar specified mainclass
  • exclude .properties files located in src/main/resources jar
  • copy project dependencies lib folder in project root
  • copy .properties files located in src/main/resources conf folder in project root. note step optional convenience users of jar. can require explicitly create file in conf directory. conf directory added runtime classpath via manifest.
  • add conf folder manifest, providing access executable jar

using these maven plugins in conjunction each other in pom configuration give need. "best practices" of solution; i'm not sure.

using maven-dependency-plugin 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 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 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 project setup, can run in eclipse using 1 config, , provide users properties file configure, without properties stepping on each other.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -