Java 使用 IntelliJ IDEA 运行 Maven 项目

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

Run a Maven Project using IntelliJ IDEA

javamavenintellij-idea

提问by user1885868

I'm new to IntelliJ IDEA and I would like to run a simple Maven Quickstart project using it.

我是 IntelliJ IDEA 的新手,我想使用它运行一个简单的 Maven Quickstart 项目。

I followed all the instructions, the project was sucessfully built. But when I try to compile and run it , the Runbutton is not activated.

我按照所有说明进行操作,该项目已成功构建。但是当我尝试编译并运行它时,Run按钮没有被激活。

It looks like IntelliJ IDEA couldn't figure out where the main class is.

看起来 IntelliJ IDEA 无法弄清楚主类在哪里。

This is a picture of the project's hierarchy.

这是项目层次结构的图片。

enter image description here

在此处输入图片说明

What is the problem ? And how can I fix it ?

问题是什么 ?我该如何解决?

Thanks !

谢谢 !

采纳答案by CrazyCoder

Refresh the Maven project in the Maven Projectstool window (Reimport All Maven Projects), if it doesn't help, refer to this answer for diagnostics.

Maven Projects工具窗口(Reimport All Maven Projects)中刷新 Maven 项目,如果没有帮助,请参考此答案进行诊断

Reimport

重新导入

The issue is that your source roots were not configured correctly from the Maven model for some reason and the .javafile appears in a plain directory instead of the Source root.

问题是您的源根.java目录由于某种原因未从 Maven 模型正确配置,并且该文件出现在普通目录中而不是Source root 中

You can tell that by the color of the folders and by the icon of the file.

您可以通过文件夹的颜色和文件的图标来判断。

回答by hya

You need to add to project e.g. Jetty or Tomcat (compile to WAR & run Tomcat server with this war).

您需要添加到项目中,例如 Jetty 或 Tomcat(编译为 WAR 并使用此战争运行 Tomcat 服务器)。

If you will have Jetty maven plugin you will be able to run app via mvn jetty:run(Add this to Maven run configuration)

如果您将拥有 Jetty maven 插件,您将能够通过mvn jetty:run(将此添加到 Maven 运行配置)来运行应用程序

回答by CoDel

Have you imported the project as a maven projet ? If yes, click on the body of your fonction and then do CTRL+MAJ+F10 or MAJ+F10

您是否将项目作为 maven 项目导入?如果是,请单击您的函数体,然后执行 CTRL+MAJ+F10 或 MAJ+F10

回答by GreyGoblin

Seems like your javadirectory is not marked as Source root. Right click on it and click Mark directory as-> Source root

似乎您的java目录未标记为Source root. 右键单击它并单击Mark directory as->Source root

回答by LJP1203

You need to create a Java Project.

您需要创建一个 Java 项目。

File->New->Project->Java->Next->Next->Finish

文件->新建->项目->Java->下一步->下一步->完成

Then go to the top and do:

然后转到顶部并执行以下操作:

Right click parent folder of project->Add Framework Support->Click Maven->Ok

右键单击项目的父文件夹->添加框架支持->单击Maven->确定

Now Maven should be added to your project and also you should be able to run it

现在 Maven 应该添加到您的项目中,并且您应该能够运行它

If this doesn'tallow you to run the project, click the little drop down box to the left of the grayed out run button and click Edit Configurations.

如果没有允许您运行该项目,点击小下拉框的左侧,变灰运行按钮,然后单击编辑配置。

Then click the +and click Application and set your Main class to the class that has the main method in it, and then it should allow you to run the application.

然后单击+并单击应用程序并将您的 Main 类设置为其中包含 main 方法的类,然后它应该允许您运行该应用程序。

Hope that helps!

希望有帮助!

回答by Luke Machowski

My issue was that I was looking for the equivalent of calling "mvnw" from the console/terminal.

我的问题是我正在寻找相当于从控制台/终端调用“mvnw”的方法。

.mnvw (linux) OR mvnw (windows)

I didn't know where this was in IntelliJ. That's how I interpret part of the question that was asked.

我不知道这是在 IntelliJ 中的什么位置。这就是我解释被问到的部分问题的方式。

The solution wasn't obvious to me so I wanted to share it.

解决方案对我来说并不明显,所以我想分享它。

Conceptually, you want to run the default maven configuration (not a specific lifecycle stage). Therefore you need to select the root entry in the Maven panel:

从概念上讲,您希望运行默认的 maven 配置(不是特定的生命周期阶段)。因此,您需要在 Maven 面板中选择根条目:

Select Root Entry in Maven Panel

在 Maven 面板中选择根条目

Then you can play the green play button on the panel:

然后就可以播放面板上的绿色播放按钮了:

Run Maven Build

运行 Maven 构建

Alternatively, you can right click and run the maven build from the context menu.

或者,您可以右键单击并从上下文菜单中运行 Maven 构建。

Run from Context Menu

从上下文菜单运行