javaspringtomcatpom.xmlmaven-war-plugin

How configure maven-war-plugin for tomcat


I can not generate proper WAR file for Tomcat.

I am using MAVEN 3.6.1, Java 12.0.1 and IDE Eclipse. My app is working fine when I run it in eclipse (Run as > Spring Boot App) but the problem is when I am trying to run my WAR file after generate it.

I am doing java -jar .war and I am getting:

Error: Could not find or load main class com.blw.linemanager.Application
Caused by: java.lang.ClassNotFoundException: com.blw.linemanager.Application

I was googling and reading stackoverflow cause I found many post about it but still can not run it. What I am doing wrong?

After some reading I figured out that I have some how configure maven-war-plugin (am I right?) and in pom I did some changes but it does not help.

<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>

Also because on the beginning I was getting error 'no main manifest attribute' I add it and now it looks like this

Manifest-Version: 1.0
Created-By: Apache Maven ${maven.version}
Build-Jdk: ${java.version}

Is my way of think wrong? Should I be able to run .war file as java -jar .war or this is missunderstanding?

@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);
    }

}

Solution

  • It is better to get a working spring boot application from here to avoid versions conflicts and the like. For a war-file deployment (into a local tomcat for example), your application must extend 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);
          }
        }
    

    Change in your pom the packaging to war.

    <packaging>war</packaging>
    

    To generate a war-file you need just the spring-boot-maven-plugin:

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

    And yes you don't need the maven-war-plugin. Just for testing purposes let's annotate our application with @RestController and introduce a simple endpoint:

        @RestController
        @SpringBootApplication
        public class SpringBootTomcatApplication extends SpringBootServletInitializer {
        ...
          @RequestMapping(value = "/")
          public String hello() {
            return "Hello World from Tomcat";
          }
        }
    

    In the Local terminal (in eclipse Ctrl+Alt+T) just enter mvn package than copy the generated war-file from target folder, paste it under the webapps folder of your local Tomcat, request http://localhost:8080/{your-application-name} and you should see the message Hello World from Tomcat. Now you can add the dependencies you need and continue coding.