转载

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

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

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

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

mvn package -DMaven.test.skip=true

我想从我的JAR文件中排除userConfig.properties文件,所以我更新了我的pom.xml,如下所示:

<excludes\>
    <exclude\>\*\*/userConfig.properties</exclude\>
</excludes\>

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

有人可以帮我吗?


我也遇到这种情况。基本上,您希望能够使用一些易于访问的userConfig.properties文件(例如/ src / main / resources)中的内容从Eclipse运行代码。另外,你想提供一个编译的可执行文件JAR与一个外部userConfig.properties,允许用户配置应用程序而不破解JAR。

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

使用指定的mainClass创建可执行JAR
JAR中排除位于src / main / resources中的所有.properties文件
将项目依赖项复制到项目根目录中的lib文件夹中
src / main / resources中的所有.properties文件复制到项目根目录中的conf文件夹中。请注意,此步骤为您的JAR用户提供了可选的便利。您可以要求在conf目录中显式创建此文件。该conf目录通过清单有效地添加到您的运行时类路径。
将此conf文件夹添加到清单,提供从可执行JAR访问它

在POM配置中使用这些Maven插件可以让您满足您的需要。关于这个解决方案的“最佳做法”我不确定。

使用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中运行,并为我的用户提供一个属性文件进行配置,没有属性彼此分离。

转载:http://www.voidcn.com/article/p-xejbgddy-bta.html

正文到此结束
本文目录