【问题标题】:How to exclude INDEX.LIST from maven generated jar-with-dependencies?如何从 Maven 生成的 jar-with-dependencies 中排除 INDEX.LIST?
【发布时间】:2013-05-07 23:18:01
【问题描述】:

我需要将应用程序打包为可执行 jar;它的属性文件将位于它之外的同一目录中。最后,我会在文件系统中有类似的东西:

. 应用程序.jar 文件1.properties 文件2.properties

但是,打包后,应用程序无法访问属性文件。经过一番研究,我想我知道是什么原因造成的,但我无法指示 Maven 按我的意愿执行。

我正在使用 maven-assembly-plugin 构建一个 jar-with-dependencies,如下所示:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <index>false</index>                
            <manifest>
                <mainClass>main.Main</mainClass>
            </manifest>
            <manifestEntries>
                <Class-Path>.</Class-Path> 
            </manifestEntries>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

当我尝试运行 jar 时,我得到一个异常,表明 jar 文件之外的属性没有被加载。

在 main.Main 类中,我放了以下代码,只是为了从 jar 外部测试可访问性:

ClassLoader cl = ClassLoader.getSystemClassLoader();
URL systemResource = ClassLoader.getSystemResource("file1.properties");
System.out.println("File 1 " + systemResource);
systemResource = ClassLoader.getSystemResource("insideJarFile.xml");
System.out.println("insideJarFile.xml " + systemResource);

insideJarFile.xml 是一个打包在 jar 中的文件。这是上面代码的结果:

File 1 null
insideJarFile.xml jar:file:/D:/test/application-jar-with-dependencies.jar!/insideJarFile.xml

研究了几个小时后,我发现原因可能是 INDEX.LIST 文件。我在 7-zip 中打开了 jar 文件,我在 META-INF 文件夹中找到了它。我从 jar 中删除它并再次执行它;结果是:

File 1 file:/D:/test/file1.properties
insideJarFile.xml jar:file:/D:/test/application-jar-with-dependencies.jar!/insideJarFile.xml

问题:我如何告诉 maven 不要创建 INDEX.LIST 文件?我试过&lt;index&gt;false&lt;/index&gt;,但没用。

TIA,

FQL

【问题讨论】:

    标签: maven jar


    【解决方案1】:

    我放弃了使用 maven-assembly-plugin。

    相反,我使用André Aronsen's solution,只添加了以下代码:

    <manifestEntries>
        <Class-Path>.</Class-Path>
    </manifestEntries>
    

    我这样做是为了在类路径中插入 jar 的当前目录,这样我就可以根据需要将我的属性文件放在它之外。

    为了以后的读者,这里是最终代码:

    <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>
    
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                    <mainClass>theMainClass</mainClass>
                </manifest>
                <manifestEntries>
                    <Class-Path>.</Class-Path>
                </manifestEntries>
            </archive>
        </configuration>
    </plugin> 
    

    这样,我不必担心很多问题,同时 André Aronsen 的解决方案非常简单,尽管它没有创建一个包含所有依赖项的 jar。

    感谢阅读,感谢 André Aronsen,希望这对其他人也有帮助。

    【讨论】:

      【解决方案2】:

      另一种解决方法是在创建 jar 后删除 Index.List 文件。这可以在TrueZIP Maven Plugin 的帮助下完成。这样就不必切换 maven-assembly-plugin。

      以下示例取自here。只需在 maven-assembly-plugin 之后的 pom.xml 中插入 sn-p。

      <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>truezip-maven-plugin</artifactId>
      <version>1.1</version>
      <executions>
          <execution>
              <id>remove-a-file-in-sub-archive</id>
              <goals>
                  <goal>remove</goal>
              </goals>
              <phase>package</phase>
              <configuration>
                  <fileset>
                      <directory>target/my-jar.jar/META-INF</directory>
                      <includes>
                          <include>INDEX.LIST</include>
                      </includes>
                  </fileset>
              </configuration>
          </execution>
      </executions>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-02
        • 2011-09-06
        • 2012-01-31
        • 1970-01-01
        • 2011-07-13
        • 1970-01-01
        • 1970-01-01
        • 2011-05-06
        相关资源
        最近更新 更多