Java 将本地依赖项添加到 Maven 项目的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28352732/
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
Best way to add local dependency to Maven project
提问by ktm5124
There are a lot of questions about this, but the answers seem to contradict each other. So I wanted to ask it for my version of Maven (3.0.4).
关于这个问题有很多,但答案似乎相互矛盾。所以我想询问我的 Maven (3.0.4) 版本。
I have a JAR file that's not part of any maven repository. It's a local dependency. I understand there are two ways to add it to my Maven project.
我有一个 JAR 文件,它不属于任何 Maven 存储库。这是一个本地依赖。我知道有两种方法可以将它添加到我的 Maven 项目中。
- Add it as a dependency and specify the file path in the
<systemPath>
property. (No other work needed.) - Create a local repository
<my-project>/repo
and install the JAR in this repository. Then, add the new repository in my pom.xml file and add the new dependency.
- 将其添加为依赖项并在
<systemPath>
属性中指定文件路径。(不需要其他工作。) - 创建本地存储库
<my-project>/repo
并在此存储库中安装 JAR。然后,在我的 pom.xml 文件中添加新的存储库并添加新的依赖项。
I'm curious which way is better practice? I've heard some negative things about the <systemPath>
property, although that way looks faster.
我很好奇哪种方法更好?我听说过一些关于该<systemPath>
物业的负面消息,尽管这种方式看起来更快。
采纳答案by wassgren
The answer is, it depends...
答案是,这取决于...
If you add it as a system dependency it is likely to become path dependent which makes it more difficult to share among other developers. Sure you can also distribute the 3rd party jar relative to your POM via your SCM but the intention of
systemPath
is more for dependencies that are provided by the JDK or the VM. From the docsaboutsystemPath
:They are usually used to tell Maven about dependencies which are provided by the JDK or the VM. Thus, system dependencies are especially useful for resolving dependencies on artifacts which are now provided by the JDK, but where available as separate downloads earlier.
To install the jar in the local repo is also not friendly for sharing. It requires all developers to "upload" the jar to their local repo before building. You can of course add a script that handles this for you but it is always easy to forget to upload and IMO a better solution is point 3 below. The local install process is described here.
If you are working in an organization where the developers share the POM you should upload the 3rd party jar to a local repository manager. That way you can easily work with the 3rd party jar as if using any other maven enabled jar. Check this linkfor a comprehensive list on repository managers.
如果您将其添加为系统依赖项,它可能会成为路径依赖项,这使得在其他开发人员之间共享变得更加困难。当然,您也可以通过 SCM 分发与 POM 相关的 3rd 方 jar,但其目的
systemPath
更多是针对 JDK 或 VM 提供的依赖项。从该文档有关systemPath
:它们通常用于告诉 Maven 由 JDK 或 VM 提供的依赖项。因此,系统依赖关系对于解决现在由 JDK 提供的工件的依赖关系特别有用,但之前可以单独下载。
将 jar 安装在本地 repo 中也不利于共享。它要求所有开发人员在构建之前将 jar“上传”到他们的本地存储库。您当然可以添加一个脚本来为您处理此问题,但总是很容易忘记上传,而 IMO 更好的解决方案是下面的第 3 点。此处描述了本地安装过程。
如果您在开发人员共享 POM 的组织中工作,您应该将第 3 方 jar 上传到本地存储库管理器。这样您就可以轻松地使用 3rd 方 jar,就像使用任何其他启用 Maven 的 jar 一样。检查此链接以获取有关存储库管理器的完整列表。
To summarize, if you are sharing the POM with others I would suggest that you upload it to a local repository manager. Otherwise the second option can work for you. As a last option, I would fall back to option number 1.
总而言之,如果您与其他人共享 POM,我建议您将其上传到本地存储库管理器。否则,第二个选项可以为您工作。作为最后一个选项,我会回到选项 1。
Also note that this has nothing to do with which version of Maven you are running.
另请注意,这与您运行的 Maven 版本无关。
回答by Sergey Vinichenko
You can add jar to your local maven repository. Usually it located at:
您可以将 jar 添加到本地 Maven 存储库。通常它位于:
$home_directory/.m2/repository
For example you have expample.jar and you want to add it to your pom as:
例如你有 example.jar 并且你想把它添加到你的 pom 中:
<dependency>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>1.0</version>
</dependency>
Then you must add example.jar to:
然后你必须将 example.jar 添加到:
$home_directory/.m2/repository/com/example/1.0/example.jar
In my case NetBeans do it for me.
就我而言,NetBeans 为我做这件事。
回答by Haytham Salhi
The best way I see so far is to use install:install-file goal with maven. You can use the mvn command line to execute it directly with appropriate parameters, or if you are using eclipse EE, you can do so by leveraging the embedded maven and creating a Run Configuration as follows:
到目前为止我看到的最好的方法是在 maven 中使用 install:install-file 目标。您可以使用 mvn 命令行以适当的参数直接执行它,或者如果您使用的是 Eclipse EE,则可以通过利用嵌入式 maven 并创建运行配置来执行此操作,如下所示:
Then, you include the jar as follows:
然后,按如下方式包含 jar:
<dependency>
<groupId>mylocal.weka</groupId>
<artifactId>weka</artifactId>
<version>1.0</version>
</dependency>
Of course adjust the parameters as per your needs.
当然,根据您的需要调整参数。
Best, Haytham
最好的,海瑟姆