Java 在 Maven 中添加依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/396245/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:11:34  来源:igfitidea点击:

Add a dependency in Maven

javamacosmaven-2dependencies

提问by Milhous

How do I take a jar file that I have and add it to the dependency system in maven 2? I will be the maintainer of this dependency and my code needs this jar in the class path so that it will compile.

如何获取我拥有的 jar 文件并将其添加到 maven 2 中的依赖项系统?我将成为这个依赖项的维护者,我的代码在类路径中需要这个 jar 以便它可以编译。

采纳答案by Hyman Leow

You'll have to do this in two steps:

您必须分两步执行此操作:

1. Give your JAR a groupId, artifactId and version and add it to your repository.

1. 为您的 JAR 提供 groupId、artifactId 和 version,并将其添加到您的存储库中。

If you don't have an internal repository, and you're just trying to add your JAR to your local repository, you can install it as follows, using any arbitrary groupId/artifactIds:

如果您没有内部存储库,而您只是想将 JAR 添加到本地存储库,则可以使用任意 groupId/artifactIds 按如下方式安装它:

mvn install:install-file -DgroupId=com.stackoverflow... -DartifactId=yourartifactid... -Dversion=1.0 -Dpackaging=jar -Dfile=/path/to/jarfile

You can also deploy it to your internal repository if you have one, and want to make this available to other developers in your organization. I just use my repository's web based interface to add artifacts, but you should be able to accomplish the same thing using mvn deploy:deploy-file ....

如果您有内部存储库,并且希望将其提供给组织中的其他开发人员,您也可以将其部署到内部存储库。我只是使用我的存储库的基于 Web 的界面来添加工件,但您应该能够使用mvn deploy:deploy-file ....

2. Update dependent projects to reference this JAR.

2. 更新依赖项目以引用此 JAR。

Then update the dependency in the pom.xml of the projects that use the JAR by adding the following to the element:

然后通过将以下内容添加到元素来更新使用 JAR 的项目的 pom.xml 中的依赖项:

<dependencies>
    ...
    <dependency>
        <groupId>com.stackoverflow...</groupId>
        <artifactId>artifactId...</artifactId>
        <version>1.0</version>
    </dependency>
    ...
</dependencies>

回答by kdgregory

I'll assume that you're asking how to push a dependency out to a "well-known repository," and not simply asking how to update your POM.

我假设您问的是如何将依赖项推送到“众所周知的存储库”,而不是简单地询问如何更新您的 POM。

If yes, then thisis what you want to read.

如果是,那么就是您想要阅读的内容。

And for anyone looking to set up an internal repository server, look here(half of the problem with using Maven 2 is finding the docs)

对于希望设置内部存储库服务器的任何人,请查看此处(使用 Maven 2 的一半问题是查找文档)

回答by user40032

I'd do this:

我会这样做:

  1. add the dependency as you like in your pom:

    <dependency>
            <groupId>com.stackoverflow...</groupId>
            <artifactId>artifactId...</artifactId>
            <version>1.0</version>
    </dependency>
    

  2. run mvn installit will try to download the jar and fail. On the process, it will give you the complete command of installing the jar with the error message. Copy that command and run it! easy huh?!

  1. 在你的 pom 中添加你喜欢的依赖项:

    <dependency>
            <groupId>com.stackoverflow...</groupId>
            <artifactId>artifactId...</artifactId>
            <version>1.0</version>
    </dependency>
    

  2. 运行mvn install它将尝试下载 jar 并失败。在此过程中,它将为您提供安装带有错误消息的 jar 的完整命令。复制该命令并运行它!容易吧?!

回答by Kieveli

Actually, on investigating this, I think all these answers are incorrect. Your question is misleading because of our level of understanding of maven. And I say our because I'm just getting introduced to maven.

实际上,在对此进行调查时,我认为所有这些答案都是不正确的。您的问题具有误导性,因为我们对maven. 我说我们是因为我刚刚被介绍给maven.

In Eclipse, when you want to add a jar file to your project, normally you download the jar manually and then drop it into the libdirectory. With maven, you don't do it this way. Here's what you do:

在 中Eclipse,当您想将 jar 文件添加到您的项目时,通常您手动下载 jar,然后将其放入lib目录中。使用 maven,你不会这样做。这是你要做的:

  • Go to mvnrepository
  • Search for the library you want to add
  • Copy the dependencystatement into your pom.xml
  • rebuild via mvn
  • 转到mvnrepository
  • 搜索您要添加的库
  • dependency语句复制到您的pom.xml
  • 重建通过 mvn

Now, mavenwill connect and download the jaralong with the list of dependencies, and automatically resolve any additional dependencies that jarmay have had. So if the jaralso needed commons-logging, that will be downloaded as well.

现在,maven将连接并下载jar依赖项列表,并自动解决jar可能具有的任何其他依赖项。所以如果jar还需要commons-logging,那也将被下载。

回答by Frederic Morin

You can also specify a dependency not in a maven repository. Could be usefull when no central maven repository for your team exist or if you have a CIserver

您还可以指定不在 Maven 存储库中的依赖项。当您的团队不存在中央 Maven 存储库或您有CI服务器时可能很有用

    <dependency>
        <groupId>com.stackoverflow</groupId>
        <artifactId>commons-utils</artifactId>
        <version>1.3</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/commons-utils.jar</systemPath>
    </dependency>