在Eclipse IDE中使用Maven

Eclipse IDE has fantastic support for Maven. We can easily create maven projects, build, deploy, and run it using the Eclipse GUI based interface. In this tutorial, we will learn how to use maven in Eclipse. We will go through the following examples.

Eclipse IDE对Maven提供了出色的支持。 我们可以使用基于Eclipse GUI的界面轻松创建maven项目,构建,部署和运行它。 在本教程中,我们将学习如何在Eclipse中使用maven。 我们将通过以下示例。

  1. Creating a simple Maven project, build and run it

    创建一个简单的Maven项目,构建并运行它
  2. Creating a Java Web Project in Eclipse using Maven

    使用Maven在Eclipse中创建Java Web项目
  3. Converting a simple Java project to Maven

    将简单的Java项目转换为Maven

1.在Eclipse中创建一个简单的Maven项目 (1. Creating a Simple Maven Project in Eclipse)

Go to File -> New -> Project and it will open the new project wizard. Select “Maven Project” as shown in the below image and click on “Next” button.

转到文件->新建->项目 ,它将打开新项目向导。 如下图所示,选择“ Maven Project”,然后单击“下一步”按钮。

Eclipse New Maven Project

Eclipse New Maven Project

Eclipse New Maven项目

In the next screen, we have the option to specify the project location and add it to any working set. We will not make any changes to it.

在下一个屏幕中,我们可以选择指定项目位置并将其添加到任何工作集中。 我们不会对此进行任何更改。

There is also an option to skip archetype selection and create a simple project. But, we will use the archetype to create the template project.

还有一个选项可以跳过原型选择并创建一个简单的项目。 但是,我们将使用原型来创建模板项目。

In the next screen, we have to select the archetype of the project. This is the most important step in creating the project. We want a simple maven JAR based application. So, we will choose the “maven-archetype-quickstart” artifact to create the project.

在下一个屏幕中,我们必须选择项目的原型。 这是创建项目中最重要的步骤。 我们需要一个简单的基于Maven JAR的应用程序。 因此,我们将选择“ maven-archetype-quickstart”工件来创建项目。

In the next screen, we have to specify the groupId, artifactId, and base package of the project. You can use the values from the image below or enter any values you want in your project.

在下一个屏幕中,我们必须指定项目的groupId,artifactId和基本包。 您可以使用下图中的值,也可以在项目中输入所需的任何值。

After clicking the Finish button, the popup wizard will close and the project will get created. It will be visible in the project explorer. The below image shows all the directories and the pom.xml of the newly created project.

单击“完成”按钮后,弹出向导将关闭,并且将创建项目。 它将在项目浏览器中可见。 下图显示了新创建项目的所有目录和pom.xml。

Eclipse Maven Project Package Explorer

Eclipse Maven Project Package Explorer

Eclipse Maven项目包资源管理器

2.在Eclipse中构建Maven项目 (2. Building the Maven Project in Eclipse)

First of all, select the project and go to “Run As -> Maven Build”.

首先,选择项目并转到“ Run As-> Maven Build”。

Eclipse Run As Maven Build

Eclipse Run As Maven Build

Eclipse作为Maven构建运行

The “Edit Configuration” popup window will open. Enter the “Goals” as “package” to build the project and click on the Run button.

将打开“编辑配置”弹出窗口。 输入“目标”作为“包”以构建项目,然后单击“运行”按钮。

If you look at the Console messages, you will notice that the build failed with the compilation error messages as “Source option 5 is no longer supported. Use 7 or later“.

如果查看控制台消息,您会注意到构建失败,并显示编译错误消息,因为“ 不再支持源选项5。 使用7或更高版本

Eclipse Maven Compilation Failed Build Error

Eclipse Maven Compilation Failed Build Error

Eclipse Maven编译失败的构建错误

It’s because of the auto-generated pom.xml file, which is not compatible with the latest Java versions. We can fix it by using the latest version of maven-compiler-plugin and setting maven.compiler.release to the Java version we are using.

这是因为自动生成的pom.xml文件与最新的Java版本不兼容。 我们可以通过使用最新版本的maven-compiler-plugin并将maven.compiler.release设置为我们正在使用的Java版本来对其进行修复

<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<maven.compiler.release>13</maven.compiler.release>
</properties>
	
<build>
	<pluginManagement>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
			</plugin>
		</plugins>
	</pluginManagement>
</build>

3.在Eclipse中更新Maven项目 (3. Updating the Maven Project in Eclipse)

Since we have changed the pom.xml file, we have to update the maven project to use the new configurations. You will notice a cross mark in the project root in the package explorer.

由于我们已经更改了pom.xml文件,因此我们必须更新maven项目以使用新的配置。 您会在包资源管理器的项目根目录中看到一个十字标记。

The Problems view will also show the error that the project pom.xml is changed and we have to update the project.

“问题”视图还将显示错误,即项目pom.xml已更改,我们必须更新项目。

Select the project and go to “Maven > Update Project”.

选择项目,然后转到“ Maven>更新项目”。

Eclipse Maven Update Project

Eclipse Maven Update Project

Eclipse Maven更新项目

In the popup window, make sure the project is selected. Also, “Update project configuration from pom.xml” should be checked. If you want maven to download all the required dependencies again, check the “Force update of Snapshots/Releases” option.

在弹出窗口中,确保已选择项目。 另外,应选中“ 从pom.xml更新项目配置 ”。 如果希望maven重新下载所有必需的依赖项,请选中“ 强制更新快照/发布 ”选项。

Click on the Ok button and the project will get updated with the latest pom.xml configurations.

单击确定按钮,该项目将使用最新的pom.xml配置进行更新。

4.在Eclipse中构建和运行Maven项目 (4. Building and Running the Maven Project in Eclipse)

Now that we have fixed the pom.xml configurations, build the project once again as shown above. This time the Console should show the “BUILD SUCCESS” message.

现在,我们已经修复了pom.xml配置,再次如上所示构建项目。 这次控制台应显示“ BUILD SUCCESS”消息。

Eclipse Maven Project Build Success

Eclipse Maven Project Build Success

Eclipse Maven项目构建成功

To run the maven project, select it and go to “Run As > Java Application”.

要运行maven项目,请选择它并转到“运行方式> Java应用程序”。

Eclipse Run As Java Application

Eclipse Run As Java Application

Eclipse Java应用程序运行

In the next window, select the main class to execute. In this case, select the App class and click on the Ok button.

在下一个窗口中,选择要执行的主类。 在这种情况下,请选择App类,然后单击“确定”按钮。

Eclipse Select Java Application

Eclipse Select Java Application

Eclipse选择Java应用程序

The App class code is:

App类代码为:

package com.journaldev.classes;

public class App {

	public static void main(String[] args) {

		System.out.println("Hello World");
	}

}

You will see the “Hello World” output in the Console window.

您将在“控制台”窗口中看到“ Hello World”输出。

Eclipse Java Application Console Output

Eclipse Java Application Console Output

Eclipse Java应用程序控制台输出

5.使用Maven在Eclipse中创建Java Web项目 (5. Creating a Java Web Project in Eclipse using Maven)

The process of creating a java web project using Maven is almost the same. The only change is that we will use “maven-archetype-webapp” so that a web application project is created.

使用Maven创建Java Web项目的过程几乎相同。 唯一的变化是,我们将使用“ maven-archetype-webapp”,以便创建一个Web应用程序项目。

Eclipse Maven Java Web Project

Eclipse Maven Java Web Project

Eclipse Maven Java Web项目

The next step would be to fix the pom.xml file by using the latest version of the maven-compiler-plugin and using the latest java version.

下一步是使用最新版本的maven-compiler-plugin并使用最新的Java版本来修复pom.xml文件。

Then build the project with the “package” goal and you should see the “BUILD SUCCESSFUL” message in the console.

然后以“打包”目标构建项目,您应该在控制台中看到“ BUILD SUCCESSFUL”消息。

Eclipse Maven Java Web App Build Success

Eclipse Maven Java Web App Build Success

Eclipse Maven Java Web App构建成功

Notice the project build generates maven-example-webapp.war file in the target directory.

注意,项目构建在目标目录中生成了maven-example-webapp.war文件。

We can integrate Apache Tomcat in the Eclipse itself, but that is out of the scope of this tutorial. You can deploy the WAR file into any servlet container and you should see the following output.

我们可以将Apache Tomcat集成到Eclipse本身中,但这超出了本教程的范围。 您可以将WAR文件部署到任何servlet容器中,并且应该看到以下输出。

Eclipse Java Web App Output

Eclipse Java Web App Output

Eclipse Java Web App输出

6.将简单的Java项目转换为Maven (6. Converting a simple Java project to maven)

Sometimes we have a simple Java project. It’s recommended to use Maven for dependency and build management. We can easily convert a simple Java project to a maven based project.

有时我们有一个简单的Java项目。 建议使用Maven进行依赖和构建管理。 我们可以轻松地将简单的Java项目转换为基于Maven的项目。

Let’s say we have a simple Java project like the below image.

假设我们有一个简单的Java项目,如下图所示。

Eclipse Simple Java Project

Eclipse Simple Java Project

Eclipse简单Java项目

Select the project and go to “Configure > Convert to Maven Project”.

选择项目,然后转到“配置>转换为Maven项目”。

Eclipse Convert To Maven Project

Eclipse Convert To Maven Project

Eclipse转换为Maven项目

It will open up a new window to create a pom.xml file. We have to provide project configurations such as groupId, artifactId, packaging, etc.

它将打开一个新窗口以创建pom.xml文件。 我们必须提供项目配置,例如groupId,artifactId,包装等。

The below image shows the maven project structure and the newly generated pom.xml file.

下图显示了maven项目结构和新生成的pom.xml文件。

Eclipse Converted Maven Project Structure

Eclipse Converted Maven Project Structure

Eclipse转换的Maven项目结构

结论 (Conclusion)

Eclipse provides built-in support for Maven. It helps us in working with maven projects easily in Eclipse IDE.

Eclipse提供了对Maven的内置支持。 它帮助我们在Eclipse IDE中轻松地使用Maven项目。

参考资料 (References)

翻译自: https://www.journaldev.com/33601/maven-eclipse-ide

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值