在 tomcat 中部署 WAR [英] Deploying a WAR in tomcat
问题描述
我已经在 archlinux 上安装了 tomcat,我尝试了 tomcat7 和 tomcat8.根据包括官方文档在内的多个消息来源,部署 WAR 文件就像将其放入 webapps 文件夹(在我的例子中是/var/lib/tomcat7/webapps)一样简单.WAR 文件被炸毁.我无法弄清楚的是如何访问我的 Web 应用程序.在 localhost:8080 上有一个 tomcat 网页.我也试过 localhost:8080/name-of-the-war-file,但这只会让 HTTP 状态 404.
我用于测试的应用程序是关于 Spring Boot 的第一个指南:http://spring.io/guides/gs/spring-boot/>
我修改了 Maven 构建文件 pom.xml 以在运行mvn package"时生成一个 WAR 文件:
<modelVersion>4.0.0</modelVersion><groupId>org.springframework</groupId><artifactId>gs-spring-boot</artifactId><version>0.1.0</version><父母><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.2.2.RELEASE</version></父母><包装>战争</包装><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><范围>提供</范围></依赖></依赖项><属性><java.version>1.7</java.version></属性><构建><插件><插件><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></插件></plugins></build></项目>
除了修改 pom.xml 生成 WAR 文件外,还需要为 web 应用配置 tomcat.这以前是通过 web.xml 文件完成的.如今,这可以从应用程序本身内部归档.SpringBootServletInitializer 和 WebApplicationInitializer 是要寻找的线索.我发现让我全部运行的最简单方法是按以下方式修改 Application.java:
包你好;导入 java.util.Arrays;导入 org.springframework.boot.SpringApplication;导入 org.springframework.boot.autoconfigure.SpringBootApplication;导入 org.springframework.context.ApplicationContext;导入 org.springframework.boot.builder.SpringApplicationBuilder;导入 org.springframework.boot.context.web.SpringBootServletInitializer;@SpringBootApplication公共类应用程序扩展 SpringBootServletInitializer {@覆盖受保护的 SpringApplicationBuilder 配置(SpringApplicationBuilder 应用程序){返回 application.sources(Application.class);}公共静态无效主(字符串 [] args){ApplicationContext ctx = SpringApplication.run(Application.class, args);System.out.println("让我们检查一下 Spring Boot 提供的 bean:");String[] beanNames = ctx.getBeanDefinitionNames();Arrays.sort(beanNames);for (String beanName: beanNames) {System.out.println(beanName);}}}
现在我可以看到来自 Spring Boot 的问候!"http://localhost:8080/gs-spring-boot-0.1.0/(gs-spring-boot-0.1.0.war 是已部署的 WAR 文件的名称).
I've installed tomcat on archlinux, I tried both tomcat7 and tomcat8. According to several sources, including the official documentation, deploying a WAR file is as easy as dropping it in the webapps folder (which in my case is /var/lib/tomcat7/webapps). The WAR file gets exploded. What I can't figure out though is how to access my web application. On localhost:8080 there is a tomcat webpage. I also tried localhost:8080/name-of-the-war-file, but that only let to a HTTP Status 404.
The application I've used for testing is the first guide on spring boot: http://spring.io/guides/gs/spring-boot/
I've modified the maven build file pom.xml to produce a WAR file when running 'mvn package':
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-spring-boot</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.2.RELEASE</version>
</parent>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.7</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Apart from modifying the pom.xml to produce a WAR file, it is also neccessary to configure tomcat for the web application. This was formerly done via a web.xml file. Nowadays this can be archieved from within the application itself. SpringBootServletInitializer and WebApplicationInitializer are the cues to look for. The easiest way I found to get I all running was to modify Application.java in the following way:
package hello;
import java.util.Arrays;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
}
}
Now I can see "Greetings from Spring Boot!" on http://localhost:8080/gs-spring-boot-0.1.0/ (with gs-spring-boot-0.1.0.war being the name of the deployed WAR file).
这篇关于在 tomcat 中部署 WAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!