Java 如何使用构建类或 war 文件将我的 Spring MVC Web 应用程序部署到 Apache tomat 目录中?

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

How to deploy my Spring MVC web application using the build classes or war file into the Apache tomat dir?

javaspringtomcatspring-mvc

提问by Ashish Pancholi

I am new to Spring MVC Framework. Recently I have learned to deployed the simple web applicationcreated using JSP/Servetsin Apache tomcat. I have created a sample web applicationand successfully deployed it in Apache tomcat. For deploying I did following-

我是新手Spring MVC Framework。最近我学会web application了使用JSP/Servetsin部署简单的created Apache tomcat。我已经创建了一个示例web application并成功地将它部署到Apache tomcat. 为了部署,我做了以下-

I have downloaded the Apache Tomcatcore 64 bit zip from http://tomcat.apache.org/download-60.cgi. Copy that folder "apache-tomcat-6.0.37" to Z: Driveand extract the folder. I already had Java 1.7 installed on my system in C: Drive. Created System Environment of JAVA_HOMEand TOMCAT_HOME. Copy my sample web applicationinto apache-tomcat-6.0.37\webapps\You can see the structure in the below image.

我已经Apache Tomcathttp://tomcat.apache.org/download-60.cgi下载了核心 64 位 zip 。将该文件夹“apache-tomcat-6.0.37”复制到Z: Drive并解压缩该文件夹。Java 1.我的系统上已经在C: Drive 中安装了7 。创建了JAVA_HOME和 的系统环境TOMCAT_HOME。将我的示例复制web applicationapache-tomcat-6.0.37\webapps\您可以在下图中看到结构。

enter image description here

在此处输入图片说明

Now I have created the same above web application in Spring MVC Frameworkusing Net-beans IDE and I am able to run the application over the web browser using URL- http://localhost:8084/SpringWebSample/index.html.

现在我已经Spring MVC Framework使用 Net-beans IDE创建了与上面相同的 Web 应用程序,并且我能够使用 URL- 在 Web 浏览器上运行该应用程序http://localhost:8084/SpringWebSample/index.html

Now I would like to go live with this application and deploy it on my server into Tomcat directory according to above screen shot but I am not finding any easy deployment steps. Each link that I found showing steps to run the Ant build script to deploy the sample application.

现在我想使用这个应用程序并根据上面的屏幕截图将它部署到我的服务器上的 Tomcat 目录中,但我没有找到任何简单的部署步骤。我找到的每个链接都显示了运行 Ant 构建脚本以部署示例应用程序的步骤。

can I deploy my spring MVC web applicationin the same above way like I did to deploy the simple JSP/Servets web application?

我可以spring MVC web application像部署simple JSP/Servets web application.

Is there any way to deploy my Spring MVC web applicationusing the build classesor war fileinto the Apache tomat dir?

有什么方法可以将我的Spring MVC web application使用build classes或部署war fileApache tomat dir?

Please help.

请帮忙。

采纳答案by JB Nizet

A Spring MVC application is a regular Java webapp which happens to use Spring internally. It's deployed like any other web application. So yes, you can simply take the war file of your Spring MVC app and drop it in tomcat/webapps.

Spring MVC 应用程序是一个普通的 Java webapp,它恰好在内部使用 Spring。它的部署方式与任何其他 Web 应用程序一样。所以是的,您可以简单地获取 Spring MVC 应用程序的 war 文件并将其放入tomcat/webapps.

回答by Zhe

If you work with Spring 4 + gradle, you can leverage spring boot. you can simply run gradle bootRepackage.

如果您使用 Spring 4 + gradle,则可以利用 spring boot。你可以简单地运行 gradle bootRepackage。

More inforamation about packaging as WAR:

有关打包为 WAR 的更多信息:

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-gradle-packaging

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-gradle-packaging

To build and run a project artifact, you can type the following: $ gradle build $ java -jar build/libs/mymodule-0.0.1-SNAPSHOT.jar

要构建和运行项目工件,您可以键入以下内容: $ gradle build $ java -jar build/libs/mymodule-0.0.1-SNAPSHOT.jar

To build a war file that is both executable and deployable into an external container, you need to mark the embedded container dependencies as belonging to a configuration named “providedRuntime”, e.g: ...

要构建一个既可执行又可部署到外部容器中的 war 文件,您需要将嵌入式容器依赖项标记为属于名为“providedRuntime”的配置,例如:...

    apply plugin: 'war'

war {
    baseName = 'myapp'
    version =  '0.5.0'
}

repositories {
    jcenter()
    maven { url "http://repo.spring.io/libs-snapshot" }
}

configurations {
    providedRuntime
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    ...
}