如何为tomcat配置Maven-war-plugin

时间:2019-05-28 09:37:05

标签: java spring tomcat pom.xml maven-war-plugin

我无法为Tomcat生成正确的WAR文件。

我正在使用MAVEN 3.6.1,Java 12.0.1和IDE Eclipse。当我在eclipse中运行它时,我的应用程序运行正常(运行方式> Spring Boot App),但问题是当我尝试在生成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

我正在谷歌搜索并阅读stackoverflow,原因是我发现了很多关于它的帖子,但仍然无法运行。我做错了什么?

经过一番阅读后,我发现我有一些如何配置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);
    }

}

2 个答案:

答案 0 :(得分:0)

在pom.xml中添加带有版本详细信息的包装类型,例如

<packaging>jar</packaging>

<packaging>war</packaging>

然后,附加构建标记以定义jar / war like的名称,

<build>
    <finalName>YOUR-APP_NAME</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

然后,以目标为目标运行您的应用程序

干净的包装

在目标文件夹中创建jar / war。

将您的War文件重命名为ROOT,例如ROOT.war

最后一步,将创建的jar / war复制到tomcat webapps文件夹中,然后启动tomcat。

答案 1 :(得分:0)

最好从here获取可运行的spring boot应用程序,以避免版本冲突等。 对于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文件,然后将其粘贴到{您的本地Tomcat的{1}}文件夹,请求http://localhost:8080/ {您的应用程序名称},您应该看到消息webapps。 现在,您可以添加所需的依赖项并继续编码。