[英]How to Exclude properties file from jar file?
I have a java application with the following project structure: 我有一个具有以下项目结构的java应用程序:
myProject
|
|----src
| |
| |--main
| |
| |--resources
| |
| |--userConfig.properties
| |--log4j.properties
|
|---target
I am using Maven
to build my project. 我正在使用
Maven
来构建我的项目。 I am using maven
command to build jar file as follows: 我正在使用
maven
命令来构建jar文件,如下所示:
mvn package -DMaven.test.skip=true
I want to exclude userConfig.properties
file from my JAR file so I have updated my pom.xml
as follows: 我想从我的JAR文件中排除
userConfig.properties
文件,所以我更新了我的pom.xml
,如下所示:
<excludes>
<exclude>**/userConfig.properties</exclude>
</excludes>
But it excludes from the target folder in which the compiled code resides. 但它从编译代码所在的目标文件夹中排除。 And the application won't run because it is unable to find the
userConfig.properties
file. 并且应用程序将无法运行,因为它无法找到
userConfig.properties
文件。
Can anyone help me? 谁能帮我?
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
. 基本上,您希望能够使用一些易于访问的userConfig.properties文件从Eclipse本地运行代码,例如在
/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. 此外,您希望使用外部userConfig.properties提供已编译的可执行JAR,以允许用户配置应用程序而不破解JAR。
My implementation is as follows: running mvn clean install
will: 我的实现如下:运行
mvn clean install
会:
mainClass
mainClass
创建可执行JAR .properties
files located in src/main/resources
from the JAR src/main/resources
所有.properties
文件 lib
folder in your project root lib
文件夹中 .properties
files located in src/main/resources
into a conf
folder in your project root. src/main/resources
所有.properties
文件复制到项目根目录中的conf
文件夹中。 Note that this step is an optional convenience for your users of the JAR. conf
directory. conf
目录中显式创建此文件。 This conf
directory is effectively added to your runtime classpath via the manifest. conf
目录通过清单有效地添加到运行时类路径中。 conf
folder to the manifest, providing access to it from your executable JAR conf
文件夹添加到清单,从可执行JAR提供对它的访问 Using these Maven plugins in conjunction with each other in your POM configuration will give you what you need. 在POM配置中将这些Maven插件相互结合使用可以满足您的需求。 As to the "best practices" of this solution;
至于这个解决方案的“最佳实践”; I'm not sure.
我不确定。
Using maven-dependency-plugin as follows: 使用maven-dependency-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>
Using maven-jar-plugin as follows: 使用maven-jar-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>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: 使用maven-resources-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>
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. 使用这个项目设置,我可以使用一个配置在Eclipse中运行,并为我的用户提供一个配置的属性文件,而不会相互踩踏。
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). 我认为你可能已经正确地做了,取决于你配置排除的位置(用
jar tf
检查你的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. 您可能希望放入一些默认值,让应用程序从预定义的路径读取其配置,以避免混淆类路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.