Problems of popular approaches (流行方法的问题)
Most of the answers you'll find around the internet will suggest you to either install the dependency to your local repository or specify a "system" scope in the pom
and distribute the dependency with the source of your project. (您将在Internet上找到的大多数答案都建议您将依赖项安装到本地存储库中,或者在pom
指定“系统”范围,然后将依赖项与项目源一起分发。) But both of these solutions are actually flawed. (但是,这两种解决方案实际上都是有缺陷的。)
Why you shouldn't apply the "Install to Local Repo" approach (为什么不应该应用“安装到本地存储库”方法)
When you install a dependency to your local repository it remains there. (当您将依赖项安装到本地存储库时,它将保留在那里。) Your distribution artifact will do fine as long as it has access to this repository. (只要您可以访问此存储库,您的分发工件就可以正常工作。) The problem is in most cases this repository will reside on your local machine, so there'll be no way to resolve this dependency on any other machine. (问题是在大多数情况下,此存储库将驻留在您的本地计算机上,因此将无法解决对任何其他计算机的依赖性。) Clearly making your artifact depend on a specific machine is not a way to handle things. (显然,使您的工件依赖于特定的机器不是处理事情的方法。) Otherwise this dependency will have to be locally installed on every machine working with that project which is not any better. (否则,必须将此依赖项本地安装在与该项目一起工作的每台计算机上,这并没有更好的效果。)
Why you shouldn't apply the "System Scope" approach (为什么不应该应用“系统范围”方法)
The jars you depend on with the "System Scope" approach neither get installed to any repository or attached to your target packages. (您使用“系统范围”方法依赖的jar既不会安装到任何存储库中,也不会附加到您的目标软件包中。) That's why your distribution package won't have a way to resolve that dependency when used. (这就是为什么您的分发软件包在使用时将无法解决该依赖性的原因。) That I believe was the reason why the use of system scope even got deprecated. (我认为这是什至不赞成使用系统范围的原因。) Anyway you don't want to rely on a deprecated feature. (无论如何,您都不想依赖不推荐使用的功能。)
The static in-project repository solution (静态项目内存储库解决方案)
After putting this in your pom
: (将其放入pom
:)
<repository>
<id>repo</id>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>file://${project.basedir}/repo</url>
</repository>
for each artifact with a group id of form xyz
Maven will include the following location inside your project dir in its search for artifacts: (对于每个组ID为xyz
形式的工件,Maven在搜索工件时将在项目目录中包含以下位置:)
repo/
| - x/
| | - y/
| | | - z/
| | | | - ${artifactId}/
| | | | | - ${version}/
| | | | | | - ${artifactId}-${version}.jar
To elaborate more on this you can read this blog post . (要对此进行详细说明,可以阅读此博客文章 。)
Use Maven to install to project repo (使用Maven安装到项目仓库)
Instead of creating this structure by hand I recommend to use a Maven plugin to install your jars as artifacts. (我建议不要使用手动创建此结构,而是使用Maven插件将jar安装为工件。) So, to install an artifact to an in-project repository under repo
folder execute: (因此,要将工件安装到repo
文件夹下的项目内存储库中,请执行以下操作:)
mvn install:install-file -DlocalRepositoryPath=repo -DcreateChecksum=true -Dpackaging=jar -Dfile=[your-jar] -DgroupId=[...] -DartifactId=[...] -Dversion=[...]
If you'll choose this approach you'll be able to simplify the repository declaration in pom
to: (如果选择这种方法,则可以将pom
的存储库声明简化为:)
<repository>
<id>repo</id>
<url>file://${project.basedir}/repo</url>
</repository>
A helper script (辅助脚本)
Since executing installation command for each lib is kinda annoying and definitely error prone, I've created a utility script which automatically installs all the jars from a lib
folder to a project repository, while automatically resolving all metadata (groupId, artifactId and etc.) from names of files. (由于对每个库执行安装命令有点烦人并且肯定容易出错,因此我创建了一个实用程序脚本 ,该脚本会自动将所有jar从lib
文件夹安装到项目存储库,同时自动解析所有元数据(groupId,artifactId等)。来自文件名。) The script also prints out the dependencies xml for you to copy-paste in your pom
. (该脚本还打印出依赖项xml,供您复制粘贴到pom
。)
Include the dependencies in your target package (在目标包中包含依赖项)
When you'll have your in-project repository created you'll have solved a problem of distributing the dependencies of the project with its source, but since then your project's target artifact will depend on non-published jars, so when you'll install it to a repository it will have unresolvable dependencies. (创建项目内存储库后,将解决分配项目及其源代码的依赖关系的问题,但是从那时起,项目的目标工件将依赖于未发布的jar,因此在安装时它到存储库中,它将具有无法解决的依赖关系。)
To beat this problem I suggest to include these dependencies in your target package. (为了解决这个问题,我建议在目标软件包中包括这些依赖项。) This you can do with either the Assembly Plugin or better with the OneJar Plugin . (您可以使用Assembly插件,也可以使用OneJar插件更好地做到这一点 。) The official documentaion on OneJar is easy to grasp. (OneJar上的官方文档很容易掌握。)