如何在Eclipse中添加Maven依赖项?

时间:2023-01-18 11:23:45

I don't know how to use Maven at all. I've been developing for a couple years with Eclipse and haven't yet needed to know about it. However, now I'm looking at some docs that suggest I do the following:

我根本不知道如何使用Maven。我已经用Eclipse开发了几年,还不需要了解它。然而,现在我正在看一些建议我做以下事情的文档:

"To include it within your project, just add this maven dependency to your build."

“在您的项目中包含它,只需将这个maven依赖项添加到您的构建中。”

<repository>
   <id>jboss</id>
   <url>http://repository.jboss.org/maven2</url>
</repository>

   ...
<dependency>
   <groupId>org.jboss.resteasy</groupId>
   <artifactId>resteasy-jackson-provider</artifactId>
   <version>1.1.GA</version>
</dependency>

How do I do this with my Eclipse project?

如何使用Eclipse项目实现这一点?

Please assume I know nothing about Maven. I just figured out it might be installed on my computer by typing mvn on the command line, but that's seriously the extent of my knowledge. I would be happy to continue knowing nothing about Maven if there is an equivalent, non-Maven way of following these instructions with Eclipse.

请假设我对Maven一无所知。我刚刚发现它可以通过在命令行上输入mvn来安装在我的电脑上,但这确实是我知识的范围。如果有一种与Eclipse相同的、非Maven的方式来遵循这些指令,我很乐意继续对Maven一无所知。

5 个解决方案

#1


155  

  1. On the top menu bar, open Window -> Show View -> Other
  2. 在顶部的菜单栏,打开窗口->显示视图->其他。
  3. In the Show View window, open Maven -> Maven Repositories

    在显示视图窗口中,打开Maven -> Maven存储库

    如何在Eclipse中添加Maven依赖项?

  4. In the window that appears, right-click on Global Repositories and select Go Into

    在出现的窗口中,右键单击全局存储库并选择Go Into

  5. Right-click on "central (http://repo.maven.apache.org/maven2)" and select "Rebuild Index"

    右键单击“central (http://repo.maven.apache.org/maven2)”并选择“重建索引”

    • Note that it will take a while to complete the download
    • 请注意,完成下载需要一段时间。
  6. Once indexing is complete, Right-click on the project -> Maven -> Add Dependency and start typing the name of the project you want to import (such as "hibernate").

    一旦索引完成,右键单击项目-> Maven ->添加依赖项并开始键入要导入的项目的名称(例如“hibernate”)。

    • The search results will auto-fill in the "Search Results" box below.
    • 搜索结果将自动填充下面的“搜索结果”框。

#2


14  

In fact when you open the pom.xml, you should see 5 tabs in the bottom. Click the pom.xml, and you can type whatever dependencies you want.

事实上,当你打开pom的时候。xml,您应该在底部看到5个选项卡。单击砰的一声。您可以输入任何您想要的依赖项。

如何在Eclipse中添加Maven依赖项?

#3


8  

You need to be using a Maven plugin for Eclipse in order to do this properly. The m2e plugin is built into the latest version of Eclipse, and does a decent if not perfect job of integrating Maven into the IDE. You will want to create your project as a 'Maven Project'. Alternatively you can import an existing Maven POM into your workspace to automatically create projects. Once you have your Maven project in the IDE, simply open up the POM and add your dependency to it.

您需要为Eclipse使用Maven插件,以便正确地实现这一点。m2e插件内置在最新版本的Eclipse中,在将Maven集成到IDE中这一点上做得很好,甚至还不够完美。您将希望将项目创建为“Maven项目”。或者,您可以将一个现有的Maven POM导入到您的工作区中,以自动创建项目。在IDE中有了Maven项目之后,只需打开POM并向其添加依赖项。

Now, if you do not have a Maven plugin for Eclipse, you will need to get the jar(s) for the dependency in question and manually add them as classpath references to your project. This could get unpleasant as you will need not just the top level JAR, but all its dependencies as well.

现在,如果您没有用于Eclipse的Maven插件,您将需要获得有关依赖项的jar(s),并将它们作为类路径引用手工添加到项目中。这可能会令人不愉快,因为您不仅需要*JAR,还需要它的所有依赖项。

Basically, I recommend you get a decent Maven plugin for Eclipse and let it handle the dependency management for you.

基本上,我建议您为Eclipse提供一个像样的Maven插件,并让它为您处理依赖项管理。

#4


3  

Open the pom.xml file.

砰的一声打开。xml文件。

under the project tag add <dependencies> as another tag, and google for the Maven dependencies. I used this to search.

在项目标记下,将 添加为另一个标记,并为Maven依赖添加谷歌。我用这个来搜索。

So after getting the dependency create another tag dependency inside <dependencies> tag.

因此,在得到依赖项之后,在 标记中创建另一个标签依赖项。

So ultimately it will look something like this.

最终会是这样的。

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>doc-examples</groupId>
  <artifactId>lambda-java-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>lambda-java-example</name>
  <dependencies>
      <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-lambda-java-core -->
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-core</artifactId>
        <version>1.0.0</version>
    </dependency>
  </dependencies>
</project>

Hope it helps.

希望它可以帮助。

#5


1  

I have faced the similar issue and fixed by copying the missing Jar files in to .M2 Path,

我也遇到过类似的问题,并将丢失的Jar文件复制到.M2路径中,

For example: if you see the error message as Missing artifact tws:axis-client:jar:8.7 then you have to download "axis-client-8.7.jar" file and paste the same in to below location will resolve the issue.

例如:如果您将错误消息视为丢失的工件tws:ax -client:jar:8.7,那么您必须下载“ax -client-8.7”。jar文件和粘贴到下面的位置将解决这个问题。

C:\Users\UsernameXXX.m2\repository\tws\axis-client\8.7(Paste axis-client-8.7.jar).

C:\Users\ UsernameXXX.m2 \ repository \ tws \ axis客户机\ 8.7(粘贴axis-client-8.7.jar)。

finally, right click on project->Maven->Update Project...Thats it.

最后,右键单击project->Maven->更新项目…这是它。

happy coding.

快乐的编码。

#1


155  

  1. On the top menu bar, open Window -> Show View -> Other
  2. 在顶部的菜单栏,打开窗口->显示视图->其他。
  3. In the Show View window, open Maven -> Maven Repositories

    在显示视图窗口中,打开Maven -> Maven存储库

    如何在Eclipse中添加Maven依赖项?

  4. In the window that appears, right-click on Global Repositories and select Go Into

    在出现的窗口中,右键单击全局存储库并选择Go Into

  5. Right-click on "central (http://repo.maven.apache.org/maven2)" and select "Rebuild Index"

    右键单击“central (http://repo.maven.apache.org/maven2)”并选择“重建索引”

    • Note that it will take a while to complete the download
    • 请注意,完成下载需要一段时间。
  6. Once indexing is complete, Right-click on the project -> Maven -> Add Dependency and start typing the name of the project you want to import (such as "hibernate").

    一旦索引完成,右键单击项目-> Maven ->添加依赖项并开始键入要导入的项目的名称(例如“hibernate”)。

    • The search results will auto-fill in the "Search Results" box below.
    • 搜索结果将自动填充下面的“搜索结果”框。

#2


14  

In fact when you open the pom.xml, you should see 5 tabs in the bottom. Click the pom.xml, and you can type whatever dependencies you want.

事实上,当你打开pom的时候。xml,您应该在底部看到5个选项卡。单击砰的一声。您可以输入任何您想要的依赖项。

如何在Eclipse中添加Maven依赖项?

#3


8  

You need to be using a Maven plugin for Eclipse in order to do this properly. The m2e plugin is built into the latest version of Eclipse, and does a decent if not perfect job of integrating Maven into the IDE. You will want to create your project as a 'Maven Project'. Alternatively you can import an existing Maven POM into your workspace to automatically create projects. Once you have your Maven project in the IDE, simply open up the POM and add your dependency to it.

您需要为Eclipse使用Maven插件,以便正确地实现这一点。m2e插件内置在最新版本的Eclipse中,在将Maven集成到IDE中这一点上做得很好,甚至还不够完美。您将希望将项目创建为“Maven项目”。或者,您可以将一个现有的Maven POM导入到您的工作区中,以自动创建项目。在IDE中有了Maven项目之后,只需打开POM并向其添加依赖项。

Now, if you do not have a Maven plugin for Eclipse, you will need to get the jar(s) for the dependency in question and manually add them as classpath references to your project. This could get unpleasant as you will need not just the top level JAR, but all its dependencies as well.

现在,如果您没有用于Eclipse的Maven插件,您将需要获得有关依赖项的jar(s),并将它们作为类路径引用手工添加到项目中。这可能会令人不愉快,因为您不仅需要*JAR,还需要它的所有依赖项。

Basically, I recommend you get a decent Maven plugin for Eclipse and let it handle the dependency management for you.

基本上,我建议您为Eclipse提供一个像样的Maven插件,并让它为您处理依赖项管理。

#4


3  

Open the pom.xml file.

砰的一声打开。xml文件。

under the project tag add <dependencies> as another tag, and google for the Maven dependencies. I used this to search.

在项目标记下,将 添加为另一个标记,并为Maven依赖添加谷歌。我用这个来搜索。

So after getting the dependency create another tag dependency inside <dependencies> tag.

因此,在得到依赖项之后,在 标记中创建另一个标签依赖项。

So ultimately it will look something like this.

最终会是这样的。

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>doc-examples</groupId>
  <artifactId>lambda-java-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>lambda-java-example</name>
  <dependencies>
      <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-lambda-java-core -->
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-core</artifactId>
        <version>1.0.0</version>
    </dependency>
  </dependencies>
</project>

Hope it helps.

希望它可以帮助。

#5


1  

I have faced the similar issue and fixed by copying the missing Jar files in to .M2 Path,

我也遇到过类似的问题,并将丢失的Jar文件复制到.M2路径中,

For example: if you see the error message as Missing artifact tws:axis-client:jar:8.7 then you have to download "axis-client-8.7.jar" file and paste the same in to below location will resolve the issue.

例如:如果您将错误消息视为丢失的工件tws:ax -client:jar:8.7,那么您必须下载“ax -client-8.7”。jar文件和粘贴到下面的位置将解决这个问题。

C:\Users\UsernameXXX.m2\repository\tws\axis-client\8.7(Paste axis-client-8.7.jar).

C:\Users\ UsernameXXX.m2 \ repository \ tws \ axis客户机\ 8.7(粘贴axis-client-8.7.jar)。

finally, right click on project->Maven->Update Project...Thats it.

最后,右键单击project->Maven->更新项目…这是它。

happy coding.

快乐的编码。