maven-jar-plugin 和传递依赖项

发布于 2024-07-25 18:35:13 字数 422 浏览 3 评论 0原文

我正在使用程序集和 jar 插件来部署我的应用程序。 我还使用 jar 插件来帮助我在清单文件中生成类路径,

<addClasspath>true</addClasspath>

虽然这似乎有效,但当我尝试执行 jar 时(它指定了正确的主类),问题就出现了 - 它将无法找到库实际上是一个传递依赖。 所以我的项目A依赖于项目B,项目B依赖于jar C。程序集插件将正确地压缩A、B和C,但jar插件没有在清单中包含C,从而导致ClassNotFoundException。

我在 maven-jar-plugin 中没有看到任何允许我指定需要传递依赖项的选项。

我的做法正确吗? 还有其他人设法将传递依赖项生成到清单中吗? 也许我做错了什么或者不按顺序做事。 任何帮助表示赞赏。

I'm using both the assembly and jar plugins to deploy my application. I'm also using the jar plugin to help me generate the classpath in the manifest file using

<addClasspath>true</addClasspath>

While that seems to work, the problem comes when I try executing the jar (it has a proper main class specified) - it will fail to locate a library that's actually a transitive dependency. So my project A depends on project B, and project B depends on jar C. The assembly plugin will correctly zip up A, B, and C, but the jar plugin did not include C in the manifest, causing a ClassNotFoundException.

I don't see any options in maven-jar-plugin that lets me specify that transitive dependencies are required.

Am I doing it the right way? Anyone else managed to get transitive dependencies generated into the manifest? Maybe I'm doing something wrongly or out of order. Any help appreciated.

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

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

发布评论

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

评论(3

手心的温暖 2024-08-01 18:35:13

我试图解决上述问题。 就我而言,它有效(maven-jar-plugin v2.2)。
我有一个名为 jarloading 的父项目,它有 3 个子项目:

  • main: 依赖于 a
  • a: 依赖于 b
  • b: 依赖于 a

调用发布它之后

mvn package

它的部署脚本

rm -r ~/Desktop/jarloading-bin
mkdir ~/Desktop/jarloading-bin
cp a/target/a-0.0.1-SNAPSHOT.jar ~/Desktop/jarloading-bin/
cp b/target/b-0.0.1-SNAPSHOT.jar ~/Desktop/jarloading-bin/
cp main/target/main-0.0.1-SNAPSHOT.jar ~/Desktop/jarloading-bin/

在使用包含更改目录

cd ~/Desktop/jarloading-bin

并运行

java -jar main-0.0.1-SNAPSHOT.jar

工作正常。


但实际上重点是,类路径如何在清单文件中列出:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: rschmid
Build-Jdk: 1.6.0_07
Main-Class: Main
Class-Path: a-0.0.1-SNAPSHOT.jar b-0.0.1-SNAPSHOT.jar

主项目的 pom.xml:

...
<build>
    <plugins>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <index>true</index>
                    <manifest>
                        <mainClass>Main</mainClass>
                        <addClasspath>true</addClasspath>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>ch.fiftynine.lab</groupId>
        <artifactId>a</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
...

a 项目的 pom.xml:

...
<dependencies>
    <dependency>
        <groupId>ch.fiftynine.lab</groupId>
        <artifactId>b</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
...

b 项目的 pom.xml 没有什么特别的。

以及附加的源代码和二进制文件:
源代码
二进制文件

i tried to solve the mentioned problem. in my case it worked (maven-jar-plugin v2.2).
i've got a parent project called jarloading that has 3 childs:

  • main: with dependency to a
  • a: with dependency to b
  • b: with dependency to a

after calling

mvn package

publishing it using a deploy script containing

rm -r ~/Desktop/jarloading-bin
mkdir ~/Desktop/jarloading-bin
cp a/target/a-0.0.1-SNAPSHOT.jar ~/Desktop/jarloading-bin/
cp b/target/b-0.0.1-SNAPSHOT.jar ~/Desktop/jarloading-bin/
cp main/target/main-0.0.1-SNAPSHOT.jar ~/Desktop/jarloading-bin/

changing to directory

cd ~/Desktop/jarloading-bin

and running

java -jar main-0.0.1-SNAPSHOT.jar

it worked fine.


but actually the point is, how the classpath is listed in manifest file:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: rschmid
Build-Jdk: 1.6.0_07
Main-Class: Main
Class-Path: a-0.0.1-SNAPSHOT.jar b-0.0.1-SNAPSHOT.jar

pom.xml of main project:

...
<build>
    <plugins>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <index>true</index>
                    <manifest>
                        <mainClass>Main</mainClass>
                        <addClasspath>true</addClasspath>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>ch.fiftynine.lab</groupId>
        <artifactId>a</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
...

pom.xml of a project:

...
<dependencies>
    <dependency>
        <groupId>ch.fiftynine.lab</groupId>
        <artifactId>b</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
...

pom.xml of b project contains nothing really special.

and attached source code and binaries:
source code
binaries

蓝眼睛不忧郁 2024-08-01 18:35:13

我无法让 maven-jar-plugin 工作,我不得不使用 maven-assemble-plugin。

示例:

I couldn't get the maven-jar-plugin to work, I had to use the maven-assembly-plugin.

Examples:

妖妓 2024-08-01 18:35:13

我设法解决了...但实际上并没有解决它。 我仔细检查了一下,仍然不知道为什么一些传递依赖项没有被拾取——它似乎跳过了它们,并且它们最终没有生成到 MANIFEST 中。

我仔细研究了一下并使用了 maven-dependency-plugin。 令人惊讶的是,配置 true 并将其绑定到 assembly: assembly 阶段解决了类路径问题。

I kinda managed to resolve by... not actually resolving it. I checked closer and still don't know why some transitive dependencies aren't getting picked up - it seems to skip of them and they end up not getting generated into the MANIFEST.

I dug around a bit and played with the maven-dependency-plugin. Surprisingly, configuring <attach>true</attach> and tying it to the assembly:assembly phase solved the classpath issue.

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