Java 如何从jar文件中排除属性文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2728993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:12:05  来源:igfitidea点击:

How to Exclude properties file from jar file?

javamaven

提问by Nisarg Mehta

I have a java application with the following project structure:

我有一个具有以下项目结构的 java 应用程序:

myProject
  |
  |----src
  |     |
  |     |--main
  |     |
  |     |--resources
  |           |   
  |           |--userConfig.properties
  |           |--log4j.properties
  |
  |---target

I am using Mavento build my project. I am using mavencommand to build jar file as follows:

我正在Maven用来构建我的项目。我正在使用maven命令来构建 jar 文件,如下所示:

mvn package -DMaven.test.skip=true

I want to exclude userConfig.propertiesfile from my JAR file so I have updated my pom.xmlas follows:

我想userConfig.properties从我的 JAR 文件中排除文件,所以我更新了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.propertiesfile.

但它从编译代码所在的目标文件夹中排除。并且该应用程序将无法运行,因为它无法找到该userConfig.properties文件。

Can anyone help me?

谁能帮我?

回答by Nisarg Mehta

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>

回答by wds

I think you're probably already doing it correctly, depending on where you configured that exclude (check with jar tfwhat 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.

我认为您可能已经正确执行此操作,具体取决于您配置排除的位置(检查jar tf您的 jar 中真正包含哪些文件)。最有可能的是,您遇到了问题,因为该文件不在您的类路径中,因此您的应用程序无法找到它。尝试将包含资源文件的目录添加到类路径中。

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.

您可能希望输入一些默认值并让应用程序从预定义的路径中读取其配置,以避免混淆类路径。

回答by NathanChristie

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

我也遇到过这种情况。基本上,您希望能够使用一些易于访问的userConfig.properties文件(例如 inside /src/main/resources. 此外,您希望提供带有外部userConfig.properties的已编译可执行 JAR ,允许用户在不破解 JAR 的情况下配置应用程序。

My implementation is as follows: running mvn clean installwill:

我的实现如下:运行mvn clean install将:

  • create executable JAR with the specified mainClass
  • exclude all .propertiesfiles located in src/main/resourcesfrom the JAR
  • copy project dependencies into a libfolder in your project root
  • copy all .propertiesfiles located in src/main/resourcesinto a conffolder 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 confdirectory. This confdirectory is effectively added to your runtime classpath via the manifest.
  • add this conffolder to the manifest, providing access to it from your executable JAR
  • 使用指定的创建可执行 JAR mainClass
  • 排除.properties位于src/main/resourcesJAR 中的所有文件
  • 将项目依赖项复制到lib项目根目录中的文件夹中
  • .properties位于项目根目录中的所有文件复制src/main/resources到一个conf文件夹中。请注意,对于 JAR 用户而言,此步骤是可选的方便操作。您可以要求在conf目录中显式创建此文件。该conf目录通过清单有效地添加到您的运行时类路径中。
  • 将此conf文件夹添加到清单,提供从您的可执行 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.

在 POM 配置中相互结合使用这些 Maven 插件将为您提供所需的东西。关于此解决方案的“最佳实践”;我不知道。

Using maven-dependency-pluginas 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-pluginas 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-pluginas 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 中运行,并为我的用户提供一个属性文件来配置,而没有属性相互影响。