Maven:通过相对路径向 jar 添加依赖项
我有一个专有的 jar,我想将其作为依赖项添加到我的 pom 中。
但我不想将其添加到存储库中。原因是我希望我常用的 Maven 命令(例如 mvncompile
等)能够开箱即用。 (无需要求开发人员自行将其添加到某个存储库)。
我希望该 jar 位于源代码管理中的 3rdparty lib 中,并通过 pom.xml 文件中的相对路径链接到它。
这可以做到吗?如何?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果你真的想要这个(理解,如果你不能使用公司存储库),那么我的建议是使用项目本地的“文件存储库”,并且不使用
系统范围的依赖。应该避免
system
范围,这样的依赖关系在很多情况下(例如在汇编中)不能很好地工作,它们造成的麻烦多于好处。因此,相反,声明一个项目本地存储库:
使用
install:install-file
和localRepositoryPath
参数:更新: 使用 2.2 版插件时,
install:install-file
似乎会忽略localRepositoryPath
。但是,它适用于 2.3 版及更高版本的插件。因此,请使用插件的完全限定名称来指定版本:maven-install-plugin 文档
最后,像任何其他依赖项一样声明它(但没有
system
范围):恕我直言,这是比使用 < code>system 范围,因为您的依赖项将被视为好公民(例如,它将包含在程序集中等)。
现在,我不得不提的是,在企业环境中处理这种情况的“正确方法”(也许不是这里的情况)是使用企业存储库。
If you really want this (understand, if you can't use a corporate repository), then my advice would be to use a "file repository" local to the project and to not use a
system
scoped dependency. Thesystem
scoped should be avoided, such dependencies don't work well in many situation (e.g. in assembly), they cause more troubles than benefits.So, instead, declare a repository local to the project:
Install your third party lib in there using
install:install-file
with thelocalRepositoryPath
parameter:Update: It appears that
install:install-file
ignores thelocalRepositoryPath
when using the version 2.2 of the plugin. However, it works with version 2.3 and later of the plugin. So use the fully qualified name of the plugin to specify the version:maven-install-plugin documentation
Finally, declare it like any other dependency (but without the
system
scope):This is IMHO a better solution than using a
system
scope as your dependency will be treated like a good citizen (e.g. it will be included in an assembly and so on).Now, I have to mention that the "right way" to deal with this situation in a corporate environment (maybe not the case here) would be to use a corporate repository.
使用
system
范围。${basedir}
是您的 pom.xml 的目录。然而,建议您将 jar 安装在存储库中,而不是将其提交到 SCM - 毕竟这是 Maven 试图消除的。
Using the
system
scope.${basedir}
is the directory of your pom.However it is advisable that you install your jar in the repository, and not commit it to the SCM - after all that's what maven tries to eliminate.
这是除了我之前在 我可以将 jar 添加到 maven 2 构建类路径而不安装它们吗?
这将在使用多模块构建时绕过限制,特别是如果在父项目之外的子项目中引用下载的 JAR 。这还通过在构建过程中创建 POM 和 SHA1 文件来减少设置工作。它还允许文件驻留在项目中的任何位置,而无需修复名称或遵循 Maven 存储库结构。
这使用 maven-install-plugin。为此,您需要设置一个多模块项目,并有一个代表构建的新项目,以将文件安装到本地存储库中,并确保其中一个是第一个。
您的多模块项目 pom.xml 将如下所示:
然后,repository/pom.xml 文件将包含用于加载属于项目一部分的 JAR 的定义。以下是 pom.xml 文件的一些片段。
pom 打包阻止其进行任何测试或编译或生成任何 jar 文件。 pom.xml 的核心部分位于使用 maven-install-plugin 的构建部分。
要安装多个文件,只需添加更多执行即可。
This is another method in addition to my previous answer at Can I add jars to maven 2 build classpath without installing them?
This will get around the limit when using multi-module builds especially if the downloaded JAR is referenced in child projects outside of the parent. This also reduces the setup work by creating the POM and the SHA1 files as part of the build. It also allows the file to reside anywhere in the project without fixing the names or following the maven repository structure.
This uses the maven-install-plugin. For this to work, you need to set up a multi-module project and have a new project representing the build to install files into the local repository and ensure that one is first.
You multi-module project pom.xml would look like this:
The repository/pom.xml file will then contain the definitions to load up the JARs that are part of your project. The following are some snippets of the pom.xml file.
The pom packaging prevents this from doing any tests or compile or generating any jar file. The meat of the pom.xml is in the build section where the maven-install-plugin is used.
To install more than one file, just add more executions.
这对我有用:
假设我有这个依赖项
然后,手动添加系统依赖项的类路径,如下
完整配置:
This is working for me:
Let's say I have this dependency
Then, add the class-path for your system dependency manually like this
Full config:
基本上,将其添加到 pom.xml 中:
Basically, add this to the pom.xml:
我以前关于执行此操作的模式。
它与 Pascal 提出的解决方案非常相似,尽管它将所有此类依赖项移至专用存储库模块中,以便您不必在使用依赖项的所有地方重复它(如果是多模块构建)。
I've previously written about a pattern for doing this.
It is very similar to the solution proposed by Pascal, though it moves all such dependencies into a dedicated repository module so that you don't have to repeat it everywhere the dependency is used if it is a multi-module build.
我们切换到 gradle,这在 gradle 中效果更好;)。我们只需指定一个文件夹,我们可以将 jar 放入其中以应对类似的临时情况。我们仍然在典型的依赖管理部分定义了大部分 jars(即与 Maven 相同)。这只是我们定义的又一个依赖项。
所以基本上现在我们可以将任何我们想要的 jar 放入 lib 目录中进行临时测试(如果它不是 Maven 存储库中的某个地方)。
we switched to gradle and this works much better in gradle ;). we just specify a folder we can drop jars into for temporary situations like that. We still have most of our jars defined i the typicaly dependency management section(ie. the same as maven). This is just one more dependency we define.
so basically now we can just drop any jar we want into our lib dir for temporary testing if it is not a in maven repository somewhere.
Pascal 发布的解决方案的一个小补充
当我遵循此路线时,在安装 ojdbc jar 时在 maven 中出现错误。
添加-DpomFile后,问题解决。
One small addition to the solution posted by Pascal
When I followed this route, I got an error in maven while installing ojdbc jar.
After adding -DpomFile, the problem was resolved.
我遇到了同样的问题,只需删除 DlocalRepositoryPath 参数并从 Dfile 参数中的当前位置定义正确的路径即可:
注意:Apache Maven 3.8.6
I was facing with the same issue, and it works just removing the DlocalRepositoryPath parameter and defining the correct path from current location in the Dfile parameter:
Note: Apache Maven 3.8.6
您可以使用 eclipse 生成可运行的 Jar :
导出/可运行 Jar 文件
You can use eclipse to generate a runnable Jar :
Export/Runable Jar file