开发者

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. 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.

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. So all looks good.

When I run the jar though it cannot find the config directory. My maven assembly plugin looks like thi开发者_Go百科s. You can see I'm adding . .. ./config and ../config to the classpath in an attempt to get it to work.

<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-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.

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:

         <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? 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. (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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜