Java 将第 3 方 jar 包含在 Maven 着色 jar 中,而不将其添加到本地存储库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3642023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Having a 3rd party jar included in Maven shaded jar without adding it to local repository
提问by Nakedible
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":
我已经在 Stack Overflow 上找到了如何在项目中包含第 3 方 JAR 而不将其安装到“本地存储库”的答案:
Can I add jars to maven 2 build classpath without installing them?
我可以将 jars 添加到 maven 2 构建类路径而不安装它们吗?
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 Plugin 创建一个包含项目所有依赖项的 JAR 时,不会自动包含第 3 方 JAR。
How can I make the Maven Shade Plugin add such a 3rd party JAR in to the shaded JAR?
我怎样才能让 Maven Shade Plugin 将这样的 3rd 方 JAR 添加到阴影 JAR 中?
As per the answer gotten, I made it work. What I did was, added this snippet to the beginning of my pom.xml:
根据得到的答案,我让它工作了。我所做的是,将这个片段添加到我的 pom.xml 的开头:
<repositories>
<repository>
<id>repo</id>
<url>file://${basedir}/repo</url>
</repository>
</repositories>
Then added a dependency for my project, also to pom.xml:
然后为我的项目添加了一个依赖项,也添加到 pom.xml:
<dependencies>
<dependency>
<groupId>dummy</groupId>
<artifactId>dummy</artifactId>
<version>0.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
And then ran a command line to add a package to 'repo':
然后运行命令行将包添加到“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/
(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 子目录的内容现在是:
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.
现在我可以将其签入版本控制,并且没有本地或远程依赖项。
采纳答案by Pascal Thivent
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 Plugin 创建一个包含项目所有依赖项的 JAR 时,不会自动包含第 3 方 JAR。
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).
是的,因为system
假定作用域依赖项始终存在(这正是system
作用域的含义),所以它们不会被包含在内。人们实际上不了解system
范围依赖是什么,他们只是继续滥用它们(是的,这是滥用),然后得到副作用并想知道为什么(正如 Brian 在他的回答中指出的那样)。
I already wrote many, many, reallymanytimes about this here on SO and in 99% of the cases, system
scoped dependencies should be avoided. And I'll repeat what the Dependency Scopesmini guide says one more time:
我已经写很多,很多,真的很多次关于这个在这里SO和在99%的情况,system
范围的相关性都应当避免。我将再重复一遍Dependency Scopes迷你指南所说的内容:
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.
system
:在项目生命周期的某个阶段需要这种依赖关系,但它是特定于系统的。不鼓励使用此范围:这被认为是一种“高级”功能,只有在您真正了解其使用的所有后果时才应使用,如果实际上无法量化,这可能非常困难。根据定义,此范围使您的构建不可移植。在某些边缘情况下可能有必要。系统范围包括<systemPath>
指向本地机器上此依赖项的物理位置的元素。因此,它用于指代某些预期存在于给定本地机器上而不是存储库中的工件;并且其路径可能因机器而异。systemPath 元素可以在其路径中引用环境变量:${JAVA_HOME}
例如。
So, instead of using the system
scope, either:
因此,不要使用system
范围,要么:
- 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 idealscenario. - Setup a file based repository as described in this previous answerand put your libraries in there. This is the bestcompromise if you don't have a corporate repository but need to work as a team and don't want to sacrifice portability.
- 通过 将您的库添加到您的本地存储库
install:install-file
。这是一种快速而肮脏的工作方式,如果您独自一人,这可能是一种选择,但它会使您的构建不可移植。 - 安装并运行“企业存储库”,如 Nexus、Archiva 或 Artifactory,并通过
deploy:deploy-file
. 这是理想的场景。 - 如上一个答案中所述设置基于文件的存储库,并将您的库放在那里。如果您没有公司存储库但需要作为一个团队工作并且不想牺牲可移植性,那么这是最好的折衷方案。
Please, stop using the system
scope.
请停止使用system
示波器。
回答by Mark Butler
The Maven addjars plugin solves this problem - see
Maven addjars 插件解决了这个问题——见
http://code.google.com/p/addjars-maven-plugin/wiki/UsagePage
http://code.google.com/p/addjars-maven-plugin/wiki/UsagePage
回答by okagan
Used <resources> to include my lib with all jars. i.e:
使用 <resources> 将我的库包含在所有 jar 中。IE:
<build>
<resources>
<resource>
<directory>${project.basedir}</directory>
<includes>
<include>lib/*.jar</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>