Java 部署 Spring MVC 项目

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

Deploying Spring MVC project

javaspringmaventomcat

提问by MChan

I've developed a small MVC project using Spring MVC, Hibernate, MySQL, Maven and Tomcat. I can run and test the application (locally) smoothly.

我使用 Spring MVC、Hibernate、MySQL、Maven 和 Tomcat 开发了一个小型 MVC 项目。我可以顺利运行和测试应用程序(本地)。

Now I need to publish/deploy this project on an (online) server that have only Tomcat installed on it. How can I publish/deploy the project online? Is there any special build I should do? What files I shall upload and to where?

现在我需要在只安装了 Tomcat 的(在线)服务器上发布/部署这个项目。如何在线发布/部署项目?有什么我应该做的特殊构建吗?我将上传哪些文件以及上传到哪里?

采纳答案by Ernestas Kardzys

There are several types of development options available.

有多种类型的开发选项可用。

For development on localhost EAR (Exploded ARchive) type of project is usually used (because you can easily make hot deploy on servery). But for production WAR (Web ARchive) is used (basically it's the same EAR archive, but compressed using ZIP algorithm).

通常使用 localhost EAR(Exploded ARchive)类型的项目进行开发(因为您可以轻松地在服务器上进行热部署)。但是对于生产 WAR(Web 存档)使用(基本上它是相同的 EAR 存档,但使用 ZIP 算法压缩)。

If you want to deploy your project to remote Tomcat server then make your project as WAR archive and upload it to Tomcat's webapps directory. Then you might need to restart Tomcat. But it's manual way of deploying.

如果要将项目部署到远程 Tomcat 服务器,请将项目作为 WAR 存档并将其上传到 Tomcat 的 webapps 目录。然后您可能需要重新启动Tomcat。但它是手动部署方式。

Better option is to use automated build tools (like Maven) which can compile your project, run unit tests, deploy on web server (local or remote) etc.

更好的选择是使用自动构建工具(如Maven),它可以编译您的项目、运行单元测试、部署在 Web 服务器(本地或远程)等。

This one is a great example of how to deploy your project on Tomcat server by using Maven's tomcat-maven-plugin: http://www.mkyong.com/maven/how-to-deploy-maven-based-war-file-to-tomcat/

这是如何使用 Maven 的tomcat-maven-plugin在 Tomcat 服务器上部署项目的一个很好的例子:http: //www.mkyong.com/maven/how-to-deploy-maven-based-war-file- to-tomcat/

Good luck ;)

祝你好运 ;)

回答by vigneshre

Do a mvn clean install and you will get a .war file in your target directory of web module. Copy it and paste it in tomcat_home/webapps directory and restart tomcat. Thats it. now, you can access it in whatever configured port (eg: http://localhost:8080/<your webapp war name>). lets say your war name is myapp.war, then tomcat would have extracted it into myapp folder in webapps. so your url will be http://localhost:8080/myapp

执行 mvn clean install,您将在 web 模块的目标目录中获得一个 .war 文件。将其复制并粘贴到 tomcat_home/webapps 目录中并重新启动 tomcat。就是这样。现在,您可以在任何配置的端口(例如:)中访问它http://localhost:8080/<your webapp war name>。假设您的战争名称是 myapp.war,那么 tomcat 会将其解压缩到 webapps 中的 myapp 文件夹中。所以你的网址将是 http://localhost:8080/myapp

回答by egemen

With maven deploy command, usually gets errors for various reasons. if you work in Unix/Linux system, I recommend using "rsync" method on console. (You can write own shell script to manage easily). It helps not only deploying without a problem but also helps to get time while redeploying (only uploading changed / new files). Because maven deploy / redeploy uploads your project as a bundle in jar/war. However "rysnc" method uploads your project files one by one.

使用 maven deploy 命令,通常会因为各种原因出现错误。如果您在 Unix/Linux 系统中工作,我建议在控制台上使用“rsync”方法。(您可以编写自己的shell脚本来轻松管理)。它不仅有助于毫无问题地部署,而且有助于在重新部署时获得时间(仅上传更改的/新文件)。因为 maven deploy / redeploy 将你的项目作为一个包上传到 jar/war 中。但是“rysnc”方法会一一上传您的项目文件。

Before using it, you should sure that two conditions.

在使用它之前,你应该确定两个条件。

1- your project is built in target folder (Spring Tool Suite)

1-您的项目内置在目标文件夹中(Spring Tool Suite)

2- you have access to tomcat via ssh

2- 您可以通过 ssh 访问 tomcat

example code :(v_ : prefix which is variable(customizable))

示例代码:(v_:前缀是可变的(可定制))

rsync -avz v_your_project_in_target root@v_ip:v_tomcat_name/webapps/v_project_name

(Second sharing)

(第二次分享)