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

发布于 2024-09-25 21:21:21 字数 1996 浏览 11 评论 0原文

我正在为我拥有的 Maven java 项目构建一个可执行 jar。它工作得很好,但我有一个问题,因为我希望我的配置目录位于 jar 之外,以便我可以在需要时轻松更改内容。

我已经到了这样的地步:我在没有配置包含的情况下构建了 jar,并且配置目录放置在与 jar 相同的目录中。所以一切看起来都不错。

当我运行 jar 时,它找不到配置目录。我的 Maven 程序集插件如下所示。你可以看到我正在添加。 .. ./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>

这导致我的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/

它仍然找不到目录。

在启动时读取 java.class.path 总是显示。

ClassPath : LimitsCache-1.0-SNAPSHOT-jar-with-dependencies.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 this. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戒ㄋ 2024-10-02 21:21:21

我刚刚通过配置 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>

您是否尝试过像这样使用 jar 插件?只是从您的 POM 猜测,您在尝试使用外部配置时似乎正在构建 ZIP,因此存档器插件可能只是丢弃您的清单配置。 (因为 ZIP 没有清单。)因此,您需要配置纯工件 JAR 以具有正确的类路径,而不仅仅是组装的类路径。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文