简体   繁体   English

如何在pom.xml中添加jar文件依赖项

[英]How to add jar file dependency in pom.xml

I have an application that depends on 2 jar file : 我有一个依赖于2个jar文件的应用程序:

  • OperatorInterface.jar OperatorInterface.jar
  • Operator.jar Operator.jar

I want to build my project using Maven structure. 我想使用Maven结构构建我的项目。 I've followed the Maven site tutorial to create the structure like so: 我遵循Maven网站教程来创建如下结构:

mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=source.app -DartifactId=project 

I've put my source file FileProcess.java in E:\\project\\src\\main\\java\\source\\app. 我已将源文件FileProcess.java放在E:\\ project \\ src \\ main \\ java \\ source \\ app中。

FileProcess has dependency in 2 external .jar files but I don't know how to define this dependency in pom.xml. FileProcess在2个外部.jar文件中具有依赖项,但是我不知道如何在pom.xml中定义此依赖项。

Here is the content of the pom.xml so far: 到目前为止,这是pom.xml的内容:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>source.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Can anyone help me? 谁能帮我?

First thing you need to install your jars in your maven local repository. 首先,您需要在jar本地存储库中安装jar。 You can follow the official tutorial here: 您可以在此处遵循官方教程:

https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

During the installation you will have to choose your own artifactId, groupId and version. 在安装期间,您将必须选择自己的artifactId,groupId和版本。 You can choose whatever value you want but remember them because you will need those to include your jars in the pom. 您可以选择想要的任何值,但要记住它们,因为您需要那些值才能将罐子放入pom中。

After that you can include them in your pom.xml adding these lines under the tag dependencies for each library to include: 之后,您可以将它们包括在pom.xml中,在每个库的标签dependencies下添加以下行,以包括:

<dependency>
  <groupId>your_group_id</groupId>
  <artifactId>your_artifact_id</artifactId>
  <version>your_version</version>
</dependency>

In the case when the JARs in question are your own code rather than third-party, the best approach is to "mavenize" the projects that build them as well. 如果所涉及的JAR是您自己的代码而不是第三方的代码,则最好的方法是同时“破坏”构建它们的项目。 After you do that, running mvn install on those projects will place them in your Maven local repository where they will be available to other local Maven projects who declare them as dependencies. 完成此操作后,在这些项目上运行mvn install会将其放置在您的Maven本地存储库中,其他将其声明为依赖项的本地Maven项目将可以使用它们。

Avoid adding them to your pom.xml file straight. 避免将它们直接添加到pom.xml文件中。 When you add .jars using this process, they will automatically reflect in your pom.xml Steps are - 1. Right click on your project in the file explorer in your eclipse. 当您使用此过程添加.jars ,它们将自动反映在pom.xml步骤-1.在Eclipse中的文件资源管理器中右键单击您的项目。 2. Go to build Path option. 2.转到构建路径选项。 3. Select configure build path 4. Chose the Libraries tab in the window that appears. 3.选择configure build path 。4.在出现的窗口中选择“ Libraries选项卡。 5. Add your .jar file externally. 5.从外部添加您的.jar文件。 6. Click ok and come back to your project interface 6.单击确定,然后返回您的项目界面

Update your maven project by pressing Alt+F5 and restart eclipse. 通过按Alt+F5更新您的Maven项目,然后重新启动Eclipse。 Your problem should be solved. 您的问题应该得到解决。

I would take the following route since this is for corporate use. 我会选择以下路线,因为这是供公司使用的。 This is the hard and ultimately portable way that sets you up for future Maven usage as it is intended to be done. 这是一种艰巨且最终可移植的方法,可以按预期完成您的Maven使用设置。

1) make those dependent jars Maven projects (because then you can easily version-manage them too using Maven) 1)使那些依赖的jars成为Maven项目(因为那样的话,您也可以使用Maven轻松地对其进行版本管理)

2) use a local repository manager and deploy your own projects to it using Maven release management through either the mvn:release plugin, or use a build server such as Hudson to automate the release process with a simple button press which I can highly recommend setting up. 2)使用本地存储库管理器,并通过mvn:release插件使用Maven发布管理将自己的项目部署到该存储库管理器,或者使用Hudson之类的构建服务器通过简单的按钮按下来自动化发布过程,我强烈建议您设置起来

https://maven.apache.org/repository-management.html https://maven.apache.org/repository-management.html

http://maven.apache.org/maven-release/maven-release-plugin/ http://maven.apache.org/maven-release/maven-release-plugin/

3) mvn:release the dependency jars to your local repository manager so they will be available for other Maven projects 3)mvn:将依赖项jar释放给本地存储库管理器,以便它们可用于其他Maven项目

4) you're actually done, when you have a local repository where your deploy your own snapshot and release artifacts to, then your maven build can find your own maven modules and include them in the application dependencies - if you don't forget to configure the repository in the project's pom of course. 4)实际上,当您有一个本地存储库在其中部署了自己的快照并发布工件时,您实际上已经完成了,那么您的maven构建可以找到您自己的maven模块并将其包含在应用程序依赖项中-如果您不要忘记当然,在项目的pom中配置存储库。 And your build server if you have one can find them too. 如果您有构建服务器,也可以找到它们。


The easy/lazy route is as suggested to manually install the jars in your local .m2 folder where Maven caches dependencies that it downloads, but it is absolutely not a portable solution that will stand the test of time. 建议按照简单/懒惰的方式将jar手动安装在本地.m2文件夹中,在该文件夹中,Maven会缓存其下载的依赖项,但这绝对不是经受时间考验的便携式解决方案。 It won't work when somebody else needs to work on this project until they too install the jars locally. 当其他人需要在该项目上进行工作时,除非他们也将其安装在本地,否则它将无法工作。 Or if its only you, you need to redo it every time you checkout the project on another computer / as another user. 或者,如果只有您自己,则每次在另一台计算机上/以另一位用户签出项目时都需要重做一次。 Also you need to update the jars each and every time you make changes to them, everywhere the project is checked out. 此外,每次检出项目时,每次进行更改时,您都需要更新jar。 You may need to do specific setup steps to get it working in an IDE, should you inevitably choose to start to use one. 如果您不可避免地选择开始使用一个IDE,则可能需要执行特定的设置步骤才能使其在IDE中工作。

However if you are having a time-pressure problem, then I would certainly go ahead and do that as a temporary workaround solution to be able to get going. 但是,如果您遇到时间紧张的问题,那么我一定会继续并将其作为能够解决问题的临时解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM