diendanyoi54
diendanyoi54

Reputation: 91

Gradle not creating a Tomcat deployable war

I'm running into an issue with building a war with Gradle 4.8.1. Here is the build.gradle:

buildscript {
    ext {
        springBootVersion = '2.0.3.RELEASE'
    }
    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
    }
}

apply plugin: 'idea'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

archivesBaseName = 'sample'

//war {
//    archiveName = 'sample.war'
//    group = 'my.group'
//    version = '2.0.0-SNAPSHOT'
//}

sourceCompatibility = 1.8

repositories {
    jcenter()
    maven { url "${nexusUrl}/content/groups/public" }
}

dependencies {

    // Spring
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'org.springframework.boot:spring-boot-starter-log4j2'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    compile 'org.springframework.boot:spring-boot-starter-web'
    testCompile 'org.springframework.boot:spring-boot-starter-test'

    // Lombok
    compile "org.projectlombok:lombok:1.18.0"

    // Data
    compile ('org.postgresql:postgresql') {
        exclude module: 'slf4j-simple'
    }

    // Http
    compile "org.apache.httpcomponents:httpclient:4.5.5"
}

configurations {
    all*.exclude module: 'spring-boot-starter-logging'
}

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "${nexusUrl}/content/groups/public/") {
                authentication(userName: nexusUsername, password: nexusPassword)
            }
            snapshotRepository(url: "${nexusUrl}/content/repositories/snapshots") {
                authentication(userName: nexusUsername, password: nexusPassword)
            }
            pom.version = '2.0.0-SNAPSHOT'
            pom.artifactId = 'sample'
            pom.groupId = 'my.group'
        }
    }
}

I also tried removing the 'maven' plugin, archivesBaseName, and the uploadArchive task while uncommenting the war task and I get the same result. When using uploadArchive the war deploys to the nexus server fine and I get no errors. When deploying the war to tomcat (in both instances) tomcat 7 and 8 throw no errors and I receive no logs from either catalina or the project, although archiveName inside the war task does not properly rename the war. I have tried this on two other machines/tomcat instances with the same result. When building this as a fat jar, or running this in IntelliJ, everything works as expected (including the logging).

Any help or direction would be greatly appreciated.

Upvotes: 0

Views: 1481

Answers (2)

Anand Vaidya
Anand Vaidya

Reputation: 1461

I was also facing the same issue. The issue is Spring application doesnt get initialized when the war is deployed on tomcat.

After a lot of struggle, I figured out that I needed to extend SpringBootServletInitializer in my application. So my effective code looks like

    public class SyncApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SyncApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(SyncApplication.class, args);
    }

}

Looks like this SpringBootServletInitializer directs war plugins generate bootstrapping code while building the war, and thus spring context is initialized while deploying the app.

Upvotes: 0

Jeff
Jeff

Reputation: 45

It sounds like it is not initializing the servlet at all. Make sure to extend the SpringBootServletInitializer for WAR deployment.

Upvotes: 2

Related Questions