>我无法为Tomcat生成正确的WAR文件。
我正在使用MAVEN 3.6.1,Java 12.0.1和IDE Eclipse。当我在 eclipse(以 Spring 启动应用程序运行方式(中运行时,我的应用程序运行>工作正常,但问题是当我在生成它后尝试运行我的 WAR 文件时。
我正在做java -jar .war,我得到:
Error: Could not find or load main class com.blw.linemanager.Application
Caused by: java.lang.ClassNotFoundException: com.blw.linemanager.Application
我正在谷歌搜索并阅读堆栈溢出,因为我发现了很多关于它的帖子,但仍然无法运行它。我做错了什么?
经过一些阅读,我发现我有一些如何配置 maven-war-plugin(我是对的吗?(,在 pom 中我做了一些更改,但它没有帮助。
<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>bwl</groupId>
<artifactId>LineManager</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<start-class>com.blw.linemanager.Application</start-class>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
<build>
<finalName>LineManager</finalName>
<outputDirectory>${project.build.directory}</outputDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>12</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/META-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources/META-INF</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<archive>
<manifest>
<classpathPrefix>${project.build.directory}/WEB-INF/classes</classpathPrefix>
<addClasspath>true</addClasspath>
<mainClass>com.blw.linemanager.Application</mainClass>
</manifest>
<manifestFile>${project.build.directory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
也因为一开始我收到错误"没有主要清单属性"我添加了它,现在看起来像这样
Manifest-Version: 1.0
Created-By: Apache Maven ${maven.version}
Build-Jdk: ${java.version}
我的思维方式是错误的吗?我应该能够将.war文件作为java -jar .war运行,还是这是误解?
@SpringBootApplication
public class Application extends org.springframework.boot.web.support.SpringBootServletInitializer {
public static void main(String [] args) {
SpringApplication app = new SpringApplication(Application.class);
app.run(args);
}
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
最好从这里获取一个工作的 spring 启动应用程序,以避免版本冲突等。对于war
文件部署(例如,进入本地 tomcat(,应用程序必须扩展SpringBootServletInitializer
:
@SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringBootTomcatApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringBootTomcatApplication.class);
}
}
更改pom
packaging
以war
。
<packaging>war</packaging>
要生成war
文件,您只需要spring-boot-maven-plugin
:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
是的,你不需要maven-war-plugin
.仅出于测试目的,让我们用@RestController
注释我们的应用程序,并引入一个简单的端点:
@RestController
@SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
...
@RequestMapping(value = "/")
public String hello() {
return "Hello World from Tomcat";
}
}
在Local terminal
中(在 eclipse Ctrl+Alt+T 中(,只需输入mvn package
然后从文件夹中复制生成的war
-target
file,将其粘贴到本地 Tomcat 的 webapps
文件夹下,请求 http://localhost:8080/{您的应用程序名称},您应该会看到消息Hello World from Tomcat
.现在,您可以添加所需的依赖项并继续编码。
在 pom 中添加带有版本详细信息的打包类型.xml例如
<packaging>jar</packaging>
或
<packaging>war</packaging>
然后,附加构建标记以定义jar/war的名称,例如,
<build>
<finalName>YOUR-APP_NAME</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后,以目标运行应用程序
清洁包装
在目标文件夹中创建 jar/war。
将您的战争文件重命名为 ROOT,如 ROOT.war
最后一步是在tomcat网络应用程序文件夹中复制您创建的jar/war并启动tomcat。