如何从 jar 文件中排除属性文件? [英] How to Exclude properties file from jar file?
问题描述
我有一个具有以下项目结构的 Java 应用程序:
I have a java application with the following project structure:
myProject
|
|----src
| |
| |--main
| |
| |--resources
| |
| |--userConfig.properties
| |--log4j.properties
|
|---target
我正在使用 Maven
来构建我的项目.我正在使用 maven
命令来构建 jar 文件,如下所示:
I am using Maven
to build my project. I am using maven
command to build jar file as follows:
mvn package -DMaven.test.skip=true
我想从我的 JAR 文件中排除 userConfig.properties
文件,所以我更新了我的 pom.xml
如下:
I want to exclude userConfig.properties
file from my JAR file so I have updated my pom.xml
as follows:
<excludes>
<exclude>**/userConfig.properties</exclude>
</excludes>
但它从编译代码所在的目标文件夹中排除.应用程序将无法运行,因为它无法找到 userConfig.properties
文件.
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 文件从 Eclipse 本地运行您的代码,例如在 /src/main/resources
中.此外,您希望提供一个编译后的可执行 JAR,其中包含一个外部 userConfig.properties,允许用户在不破解 JAR 的情况下配置应用程序.
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.
我的实现如下:运行mvn clean install
会:
My implementation is as follows: running mvn clean install
will:
- 使用指定的
mainClass
创建可执行的JAR - 从 JAR 中排除位于
src/main/resources
中的所有.properties
文件 - 将项目依赖项复制到项目根目录中的
lib
文件夹中 - 将位于
src/main/resources
中的所有.properties
文件复制到项目根目录中的conf
文件夹中.请注意,对于 JAR 用户而言,此步骤是可选的方便操作.您可以要求在conf
目录中明确创建此文件.这个conf
目录通过清单有效地添加到您的运行时类路径中. - 将此
conf
文件夹添加到清单,提供从可执行 JAR 对其的访问
- create executable JAR with the specified
mainClass
- exclude all
.properties
files located insrc/main/resources
from the JAR - copy project dependencies into a
lib
folder in your project root - copy all
.properties
files located insrc/main/resources
into aconf
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 theconf
directory. Thisconf
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
在您的 POM 配置中结合使用这些 Maven 插件将为您提供所需的东西.关于此解决方案的最佳实践";我不确定.
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.
使用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>
使用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>
使用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>
使用此项目设置,我可以使用一个配置在 Eclipse 中运行,并为我的用户提供一个属性文件进行配置,而无需属性相互影响.
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.
这篇关于如何从 jar 文件中排除属性文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!