本文介绍了如何从jar文件中排除属性文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下项目结构的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

我想排除 userConfig。属性来自我的JAR文件的文件,所以我更新了我的 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.

任何人都可以帮助我吗?

Can anyone help me?

推荐答案

我也遇到过这种情况。基本上,您希望能够使用一些易于访问的 userConfig.properties 文件从Eclipse本地运行代码,例如在 / src / main / resources 。此外,您希望使用外部 userConfig.properties 提供已编译的可执行JAR,以允许用户配置应用程序而不破解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排除位于 .properties 文件code> src / main / resources 从JAR

  • 复制项目依赖项到 lib 项目根目录中的文件夹

  • 复制位于 src / main / resources .properties 文件c $ c>到项目根目录中的 conf 文件夹中。请注意,此步骤是JAR用户的可选方便。您可以要求在 conf 目录中显式创建此文件。此 conf 目录通过清单有效地添加到运行时类路径中。

  • 添加此 conf 清单的文件夹,提供从可执行文件JAR的访问权限

  • 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

将这些Maven插件相互结合使用您的POM配置将为您提供所需的功能。至于这个解决方案的最佳实践;我不确定。

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文件中排除属性文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 00:15