具有依赖项的 Maven 2 程序集:作用域“system”下的 jar不包括在内
我正在使用 maven-assemble 插件创建我的应用程序的 jar,包括其依赖项,如下所示:(
<assembly>
<id>macosx</id>
<formats>
<format>tar.gz</format>
<format>dir</format>
</formats>
<dependencySets>
<dependencySet>
<includes>
<include>*:jar</include>
</includes>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
我省略了一些与问题无关的其他内容)
到目前为止,这工作得很好,因为它创建了一个 lib< /code> 包含所有依赖项的目录。但是,我最近添加了一个新的依赖项,其范围是
system
,并且它不会将其复制到 lib
输出目录。我一定缺少一些基本的东西,所以我寻求帮助。
我刚刚添加的依赖项是:
<dependency>
<groupId>sourceforge.jchart2d</groupId>
<artifactId>jchart2d</artifactId>
<version>3.1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/external/jchart2d-3.1.0.jar</systemPath>
</dependency>
我能够包含此依赖项的唯一方法是将以下内容添加到 assembly 元素:
<files>
<file>
<source>external/jchart2d-3.1.0.jar</source>
<outputDirectory>lib</outputDirectory>
</file>
</files>
但是,这迫使我在重命名此 jar 时更改 pom 和程序集文件(如果有的话)。而且,这似乎是错误的。
我尝试过在 dependencySets
和
中使用
> 没有运气。
那么如何将 system
作用域的 jar 包含到 Maven 2 中的程序集文件中呢?
多谢
I am using maven-assembly plugin to create a jar of my application, including its dependencies as follows:
<assembly>
<id>macosx</id>
<formats>
<format>tar.gz</format>
<format>dir</format>
</formats>
<dependencySets>
<dependencySet>
<includes>
<include>*:jar</include>
</includes>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
(I omitted some other stuff that is not related to the question)
So far this has worked fine because it creates a lib
directory with all dependencies. However, I recently added a new dependency whose scope is system
, and it does not copy it to the lib
output directory. i must be missing something basic here, so I call for help.
The dependency that I just added is:
<dependency>
<groupId>sourceforge.jchart2d</groupId>
<artifactId>jchart2d</artifactId>
<version>3.1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/external/jchart2d-3.1.0.jar</systemPath>
</dependency>
The only way I was able to include this dependency was by adding the following to the assembly element:
<files>
<file>
<source>external/jchart2d-3.1.0.jar</source>
<outputDirectory>lib</outputDirectory>
</file>
</files>
However, this forces me to change the pom and the assembly file whenever this jar is renamed, if ever. Also, it seems just wrong.
I have tried with <scope>runtime</scope>
in the dependencySets
and <include>sourceforge.jchart2d:jchart2d</include>
with no luck.
So how do you include a system
scoped jar to your assembly file in maven 2?
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我对没有添加系统范围依赖项并不感到惊讶(毕竟,具有系统范围的依赖项必须通过定义显式提供)。实际上,如果您确实不想将该依赖项放入本地存储库中(例如,因为您想将其作为项目的一部分进行分发),那么我会这样做:
我会在我的
pom.xml
中声明该存储库,如下所示:我会在没有
system
范围的情况下声明工件,这只是麻烦的根源:我不能 100% 确定这会满足您的需求,但我认为它是比使用系统范围更好的解决方案。
更新:我应该在原来的答案中提到这一点,我现在正在修复它。要在基于文件的存储库中安装第三方库,请使用
install:install-file
和localRepositoryPath
参数:您可以将其按原样粘贴到 *nix shell 中。在 Windows 上,删除“
\
”并将所有内容放在一行中。I'm not surprised that system scope dependencies are not added (after all, dependencies with a system scope must be explicitly provided by definition). Actually, if you really don't want to put that dependency in your local repository (for example because you want to distribute it as part of your project), this is what I would do:
I would declare that repository in my
pom.xml
like this:I would just declare the artifact without the
system
scope, this is just a source of troubles:I'm not 100% sure this will suit your needs but I think it's a better solution than using the system scope.
Update: I should have mentioned that in my original answer and I'm fixing it now. To install a third party library in the file-based repository, use
install:install-file
with thelocalRepositoryPath
parameter:You can paste this as is in a *nix shell. On windows, remove the "
\
" and put everything on a single line.顺便说一句,您可以将其自动化并使其成为您的 Maven 构建的一部分。以下命令将在编译之前将您的 jar 安装到本地存储库中:
Btw you can automate it and make it a part of your maven build. The following will install your jar into your local repository before compilation:
如果你创建 jar,我找到简单的解决方案
I find easy solution in case you creating jar
您还可以通过在 dependencySets 中添加补充 dependencySet 来处理此问题。
最好的办法是使用存储库管理器(如 Nexus、Artifactory、Archiva)并在特定存储库中安装此类依赖项。之后,您可以使用诸如简单依赖项之类的东西。这将简化您的生活。
文档:
You can also handle this via adding a supplemental dependencySet in your dependencySets.
The best thing would be to use a Repository Manager (like Nexus, Artifactory, Archiva) and install this kind of dependency in a particular repository. After that you can use such things as a simple dependency. This will simplify your life.
Docs:
编辑:抱歉,我没有意识到 alx 也提到了干净的生命周期解决方法。
基于alx提供的解决方案,您可以在clean阶段执行安装文件步骤。但由于 clean 阶段不在默认生命周期中,因此您必须在第一时间执行
mvn clean
以确保 jar 在本地存储库中准备就绪。例如:mvn clean; mvn包
Edited: Sorry that i didn't realize alx also mentioned about the clean life cycle workaround.
Base on the solution provided by alx, you can execute the install file step at clean phase. but since the clean phase is not in the default life cycle, you have to execute
mvn clean
at the first time to ensure the jar is ready in the local repo.ex: mvn clean; mvn package
一个简单的解决方案是将其添加到本地 Maven 存储库中。
一种方法是通过 mvn install 命令,如上一篇文章中建议的那样。
另一个简单的方法是,
1) 在 Eclipse IDE 中右键单击项目选择 Maven 选项。
2) 选择安装或部署工件到 Maven 存储库选项,然后单击下一步。
3)单击“Artifact file”复选框旁边的“浏览”选择你的jar文件
4)输入GroupId和ArtifactId以及版本确保生成pom&创建校验和并进行检查包装是罐子,
点击完成即可!您的工作已完成,jar 已添加到您的本地存储库中,您可以在setting.xml 或 m2 目录中定义该存储库
现在只需根据 GroupId、ArtifactId & 添加简单的 Maven 依赖项即可。您根据导入输入的 jar 版本,这就是您的外部 jar 将由 maven 打包的。
A simple solution for this is to add it into local maven repository
One way to do is via mvn install commands as suggested in previous post .
Another easy way is ,
1) In your eclipse ide right click on project select Maven option .
2) Select Install or deploy an artifact to a maven repository option and click on next.
3)Click on browse next to the Artifact file checkbox & select your jar file
4)Enter the GroupId and ArtifactId and version ensure generate pom & create checksum are checked & packaging is jar
Click on finish and that's it ! Your job is done the jar is added in your local repository which you can define in setting.xml or m2 directory
Now just add the simple maven dependency as per the GroupId,ArtifactId & jar version that you have entered as per the import and that's it your external jar will be packaged by maven.
它在我的解决方案中以更简单的方式工作:
从依赖项中删除:
然后也在 pom.xml 中添加 maven-install-plugin 。
it has worked in a easier way on my solution :
remove from your dependency :
Then add the maven-install-plugin in the pom.xml as well.
简单的解决方案是将 jar 复制到不同的目录
easy solution is to copy jar to a different directory