将第三方jar包含在Maven阴影jar中,而不将其添加到本地存储库 [英] Having a 3rd party jar included in Maven shaded jar without adding it to local repository

查看:178
本文介绍了将第三方jar包含在Maven阴影jar中,而不将其添加到本地存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Stack Overflow上找到了如何在项目中包含第三方JAR而不将其安装到本地存储库的答案:

I already found an answer here on Stack Overflow how to include a 3rd party JAR in a project without installing it to a "local repository":

我可以在不安装的情况下将jar添加到maven 2构建类路径他们?

但是,当我使用Maven Shade插件创建一个包含项目所有依赖项的JAR时,第三方JAR是不自动包含。

But, when I use the Maven Shade Plugin to create a JAR that includes all the dependencies of the project as well, the 3rd party JAR is not included automatically.

如何让Maven Shade插件将这样的第三方JAR添加到阴影JAR中?

How can I make the Maven Shade Plugin add such a 3rd party JAR in to the shaded JAR?

根据得到的答案,我做到了。我做的是,将这个片段添加到我的pom.xml的开头:

As per the answer gotten, I made it work. What I did was, added this snippet to the beginning of my pom.xml:

<repositories>
  <repository>
    <id>repo</id>
    <url>file://${basedir}/repo</url>
  </repository>
</repositories>

然后为我的项目添加了依赖项,也添加到pom.xml:

Then added a dependency for my project, also to pom.xml:

<dependencies>
  <dependency>
    <groupId>dummy</groupId>
    <artifactId>dummy</artifactId>
    <version>0.0.0</version>
    <scope>compile</scope>
  </dependency>
</dependencies>

然后运行命令行将包添加到'repo':

And then ran a command line to add a package to 'repo':

mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file
    -Dfile=<my-jar>.jar -DgroupId=dummy -DartifactId=dummy
    -Dversion=0.0.0 -Dpackaging=jar -DlocalRepositoryPath=`pwd`/repo/

(不确定repo路径是否需要完整路径,但不想冒险。)

(Not sure if the repo path needs to be a full path, but didn't want to take chances.)

repo子目录的内容现在是:

The contents of the repo subdirectory is now:

repo/dummy/dummy/0.0.0/dummy-0.0.0.jar
repo/dummy/dummy/0.0.0/dummy-0.0.0.pom
repo/dummy/dummy/maven-metadata-local.xml

现在我可以将其检入版本控制,并且没有本地或远程依赖项。

Now I can check this in to version control, and have no local or remote dependencies.

推荐答案


但是,当我使用Maven Shade插件创建包含项目所有依赖项的JAR时,第三方JA R不会自动包含。

But, when I use the Maven Shade Plugin to create a JAR that includes all the dependencies of the project as well, the 3rd party JAR is not included automatically.

是的,因为系统范围内的依赖关系是假设始终存在(这正是系统范围的内容),因此不会包含它们。人们实际上不明白系统范围依赖是什么,他们只是继续滥用它们(是的,这是滥用),然后得到副作用并想知道为什么(正如Brian指出的那样)在他的回答)。

Yes, because the system scoped dependencies are assumed to be always present (this is exactly what the system scope is about) so they won't be included. People actually don't understand what system scope dependencies are, they just keep abusing them (yes, this is abuse), and then get side effects and wonder why (as Brian pointed out in his answer).

我已写过很多很多真的 很多次次这是在SO和99%的情况下,系统范围依赖性shou应该避免。我将重复依赖范围迷你指南再说一次:

I already wrote many, many, really many times about this here on SO and in 99% of the cases, system scoped dependencies should be avoided. And I'll repeat what the Dependency Scopes mini guide says one more time:



  • system :在项目生命周期的某个阶段需要此依赖项,但这是系统特定的。 不鼓励使用此范围:这被认为是一种高级功能,只有在您真正了解其使用的所有后果时才能使用,如果实际上无法量化则可能非常困难。根据定义,此范围使您的构建不可移植。在某些边缘情况下可能是必要的。系统范围包括< systemPath> 元素,该元素指向此依赖项在本地计算机上的物理位置。因此,它用于指代预期存在于给定本地机器上而不是存储库中的某个工件;并且其路径可能因机器而异。 systemPath元素可以在其路径中引用环境变量:例如 $ {JAVA_HOME}

  • system: This dependency is required in some phase of your project's lifecycle, but is system-specific. Use of this scope is discouraged: This is considered an "advanced" kind of feature and should only be used when you truly understand all the ramifications of its use, which can be extremely hard if not actually impossible to quantify. This scope by definition renders your build non-portable. It may be necessary in certain edge cases. The system scope includes the <systemPath> element which points to the physical location of this dependency on the local machine. It is thus used to refer to some artifact expected to be present on the given local machine an not in a repository; and whose path may vary machine-to-machine. The systemPath element can refer to environment variables in its path: ${JAVA_HOME} for instance.

因此,不是使用系统范围,而是:

So, instead of using the system scope, either:


  • 通过 install:install-file 将库添加到本地存储库。这是一种快速而肮脏的方式来使事情正常工作,如果你是独自一人,它可能是一个选择,但它使你的构建不可移植。

  • 安装并运行Nexus,Archiva或Artifactory等企业存储库,并通过 deploy:deploy-file 添加库。这是理想方案。

  • 按照这个上一个答案并将你的库放在那里。如果您没有公司存储库但需要以团队形式工作并且不想牺牲可移植性,那么这是最佳折衷方案。

  • Add your libraries to your local repository via install:install-file. This is a quick and dirty way to get things working, it might be an option if you're alone but it makes your build non portable.
  • Install and run an "enterprise repository" like Nexus, Archiva, or Artifactory and add your libraries via deploy:deploy-file. This is the ideal scenario.
  • Setup a file based repository as described in this previous answer and put your libraries in there. This is the best compromise if you don't have a corporate repository but need to work as a team and don't want to sacrifice portability.

请停止使用系统范围。

这篇关于将第三方jar包含在Maven阴影jar中,而不将其添加到本地存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆