Java 使用 Intellij 构建 War 并部署到 Tomcat

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

Using Intellij to Build War and Deploy to Tomcat

javamaventomcatweb-applicationsintellij-idea

提问by Joey

My basic workflow is in terminal mvn clean installon my base directory. This creates a target directory with a WAR file. I copy that war file into the webapps directory of my Tomcat.

我的基本工作流程mvn clean install在我的基本目录的终端中。这将创建一个带有 WAR 文件的目标目录。我将该 war 文件复制到 Tomcat 的 webapps 目录中。

If I make changes I obviously have to repeat the process which is tedious. I am using Intellij Ultimate 13.1.6 and I know it is possible to have all of this done through the IDE based on this guide.

如果我进行更改,我显然必须重复这个乏味的过程。我正在使用 Intellij Ultimate 13.1.6,我知道可以通过基于本指南的 IDE 完成所有这些

So before I begin I run mvn cleanon my base which removes the target directory.

所以在我开始之前,我mvn clean在我的基础上运行,它删除了目标目录。

Now to start I when into the Project Structure --> Artifacts and created a new Web Application: Archive. I named it project_name and it had a different output directory by default (baseDir/out/artifacts/project_name vs baseDir/target) which I left everything default and hit ok.

现在开始我进入项目结构 --> Artifacts 并创建一个新的 Web 应用程序:存档。我将它命名为 project_name 并且默认情况下它有一个不同的输出目录(baseDir/out/artifacts/project_name vs baseDir/target),我将所有内容保留为默认值并点击确定。

Now I go to Run --> Edit Configurations and create a new Local Tomcat Server using the following settingsenter image description here

现在我转到 Run --> Edit Configurations 并使用以下设置创建一个新的本地 Tomcat 服务器enter image description here

and in the Deployment Tab I add the artifact which is built in my Before launch as seen in my picture. Pretty straightforward.

在部署选项卡中,我添加了在我的启动前内置的工件,如我的图片所示。很简单。

However, whenever I run the server through Intellij it successfully starts but nothing is deployed. I get the error:

但是,每当我通过 Intellij 运行服务器时,它都会成功启动但没有部署任何内容。我收到错误:

Artifact project_name: com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionException: /Users/path/to/workspace/project_name/out/artifacts/project_name/project_name.war not found for the web module.

Artifact project_name: com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionException: /Users/path/to/workspace/project_name/out/artifacts/project_name/project_name.war not found for the web module.

And I double check my directory and as seen in the error, nothing is created. Not even the out directory.

我仔细检查了我的目录,如错误所示,没有创建任何内容。甚至没有out目录。

What am I doing wrong? I know one workaround is to go back to terminal, run mvn clean installand with that .war file that is created, put that into the deployment configuration. I felt like that was more of a hack since I am still doing it through command line and if I don't have it already built I can't set the target as a directory to find the war file.

我究竟做错了什么?我知道一种解决方法是返回终端,运行mvn clean install并使用创建的 .war 文件,将其放入部署配置中。我觉得这更像是一种黑客行为,因为我仍在通过命令行进行操作,如果我还没有构建它,我就无法将目标设置为目录来查找 war 文件。

Any assistance is appreciated.

任何帮助表示赞赏。

采纳答案by xaviert

Checklist of things you can do to verify where things go wrong:

你可以做的事情清单来验证哪里出了问题:

1) Manually build the artifacts using Build > Build artifacts.

1) 使用 Build > Build artifacts 手动构建工件。

2) Verify if the artifact is in the expected output directory (out/artifacts/xxx).

2)验证工件是否在预期的输出目录(out/artifacts/xxx)中。

3) If not, go to File > Project structure > Artifacts

3)如果没有,去文件>项目结构>工件

4) Select the artifact file in question (project_name in this case).

4) 选择有问题的工件文件(在本例中为 project_name)。

5) Verify if the output directory is correct. Optionally try to create this directory yourselves in case IntelliJ doesn't do it (although this would surprise me)

5) 验证输出目录是否正确。可以选择尝试自己创建这个目录,以防 IntelliJ 不这样做(尽管这会让我感到惊讶)

6) Verify if the contents are correct.

6) 验证内容是否正确。

7) Optionally check "Build on make" to always have the latest version of your code deployed whenever you run the server.

7) 可选择选中“Build on make”,以便在运行服务器时始终部署最新版本的代码。

Note that for deploying WAR files to a Tomcat, you should prefer to use the exploded WAR. This makes deploying/testing/debugging significantly faster.

请注意,要将 WAR 文件部署到 Tomcat,您应该更喜欢使用分解的 WAR。这使得部署/测试/调试速度显着加快。

回答by vovahost

I got this error when I tried to launch an app by using Run. The problem was that for some reason the WAR file wasn't deployed even after selecting Build->Build Artifacts. Solution: I closed Idea then reimported the project and choose as artifact war-exploded. This time the deploy worked.

当我尝试使用 Run 启动应用程序时出现此错误。问题是,由于某种原因,即使在选择Build->Build Artifacts之后也没有部署 WAR 文件。解决方案:我关闭了 Idea 然后重新导入该项目并选择作为 artifact war-exploded。这次部署成功了。

回答by aircraft

I get this issue, the reason is in Project Structure, the Artifacts' Output directory is not really exists:

我遇到了这个问题,原因是在项目结构中,Artifacts 的输出目录实际上并不存在:

enter image description here

enter image description here



Solutions:

解决方案:

  1. Goto Project Structure:
  1. 转到项目结构:

File -> Project Structure

File -> Project Structure

  1. Then click the directory button
  1. 然后点击目录按钮

enter image description here

enter image description here

  1. You create the directory one by one according to the Output directory shows:
  1. 你根据Output目录显示一一创建目录:

enter image description here

enter image description here

  1. Then restart your Project.
  1. 然后重新启动您的项目。

回答by Rammgarot

Check the output directory path. NOTE that using ~in the path won't work.

检查输出目录路径。请注意,~在路径中使用将不起作用。

回答by Niamat Zawad

I tried the above solutions but the WAR file didn't work when I deployed it to the tomcat server. Instead of the above solutions, simply type mvn cleanand then mvn installin the terminal window. Your WAR file is now located in

我尝试了上述解决方案,但是当我将其部署到 tomcat 服务器时,WAR 文件不起作用。代替上述解决方案,只需在终端窗口中输入mvn clean和即可mvn install。您的 WAR 文件现在位于

ProjectName/target/

回答by isaac.hazan

I had the same problem and none of the above solutions solved the problem. Sharing it here in case someone else encounters the same issue.

我遇到了同样的问题,上述解决方案都没有解决问题。在这里分享,以防其他人遇到同样的问题。

The Tomcat server in my case was configured to run the JMX port on 18099 instead of 1099. Therefore the artifact was never deployed since the JMX port used in the IntelliJ Tomcat configuration by default is 1099.

在我的例子中,Tomcat 服务器被配置为在 18099 而不是 1099 上运行 JMX 端口。因此,由于 IntelliJ Tomcat 配置中默认使用的 JMX 端口是 1099,因此从未部署过工件。

Changing it to 18099 or removing that particular Tomcat configuration solved the issue.

将其更改为 18099 或删除该特定的 Tomcat 配置解决了该问题。

The bottom line: if you want your artifact deployed, make sure the JMX port configured in IntelliJ matches the JMX port that is actually used by your Tomcat server.

底线:如果您希望部署工件,请确保 IntelliJ 中配置的 JMX 端口与 Tomcat 服务器实际使用的 JMX 端口匹配。