我需要将应用程序打包为可执行的jar文件;其属性文件将位于其外部,位于同一目录中。最后,我将有类似的东西在文件系统:如何从Maven生成的jar-dependent-dependencies中排除INDEX.LIST?
. app.jar file1.properties file2.properties
但是,它的包装后,该应用程序不能访问属性文件。经过一番调查后,我想我知道是什么原因造成的,但我无法指示Maven按我的意愿去演出。
我使用Maven的组装插件来构建一个jar-有依赖性,如下显示:
<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类,我把下面的代码,就从罐子外面测试accessibiliy:
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文件?我试过<index>false</index>
但它没有奏效。
TIA,
FQL