简体   繁体   English

在manifest.mf中设置类路径以访问可执行jar之外的配置文件

[英]Set classpath in manifest.mf to access config files outside an executable jar

I'm building an executable jar for a maven java project I have. 我正在为我拥有的Maven Java项目构建可执行jar。 It works great but I have a problem in that I want my config directory to live outside the jar so that I can change things easily if needed. 它工作得很好,但是我有一个问题,就是我希望我的配置目录位于jar之外,以便我可以根据需要轻松地进行更改。

I've got to the point where I have the jar being built without the config include and the the config directory is placed in the same dir as the jar. 我已经到了要构建不带config include的jar的地步,并且config目录与jar放在相同的目录中。 So all looks good. 所以一切看起来都不错。

When I run the jar though it cannot find the config directory. 当我运行jar时,虽然找不到配置目录。 My maven assembly plugin looks like this. 我的maven程序集插件如下所示。 You can see I'm adding . 您可以看到我正在添加。 .. ./config and ../config to the classpath in an attempt to get it to work. .. ./config和../config到类路径,以尝试使其正常工作。

<plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-5</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.mypackage.Start</mainClass>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>./config/ .. . ../config/</Class-Path>
                    </manifestEntries>
                </archive>
                <descriptors>
                    <descriptor>src/main/assembly/buildCombinedJarWithoutConfig.xml</descriptor>
                    <descriptor>src/main/assembly/buildZipWithCombinedJarAndExternalConfig.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

This results in my manifest.mf looking like 这导致我的manifest.mf看起来像

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: Pete
Build-Jdk: 1.6.0_21
Main-Class: com.mypackage.Start
Class-Path: ./config/ .. . ../config/

It still doesn't find the dir though. 它仍然没有找到目录。

Reading java.class.path at startup always shows. 始终显示在启动时读取java.class.path。

ClassPath : LimitsCache-1.0-SNAPSHOT-jar-with-dependencies.jar

Is it possible to get the config dir on the classpath? 是否可以在类路径上获取配置目录?

I just successfully did this by configuring the maven-jar-plugin like so: 我只是通过像这样配置maven-jar-plugin来成功做到这一点:

         <configuration>
           <archive>
              <manifest>
                 <addClasspath>true</addClasspath>
                 <mainClass>org.apache.camel.spring.Main</mainClass>
              </manifest>
              <manifestEntries>
                 <Class-Path>config/</Class-Path>
              </manifestEntries>
           </archive>
        </configuration>

Did you ever try just using the jar plugin like this? 您是否曾经尝试过像这样使用jar插件? Just guessing from your POM, you seem to be building a ZIP when trying to use external config, so the archiver plugin probably just throws your manifest configuration away. 只是从您的POM猜测,您似乎在尝试使用外部配置时正在构建ZIP,因此Archiver插件可能只会丢弃您的清单配置。 (Since ZIPs don't have manifests.) Thus you need to configure your pure artifact JAR to have the proper classpath, not just the assembled one. (因为ZIP没有清单。)因此,您需要将纯工件JAR配置为具有正确的类路径,而不仅仅是已组装的类路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM