使用Tomcat gradle插件部署Spring应用程序

时间:2017-07-24 20:44:45

标签: spring tomcat gradle web-applications deployment

我正在尝试使用bmuschko tomcat插件(https://github.com/bmuschko/gradle-tomcat-plugin)在tomcat上部署spring应用程序。

我的webapp文件夹中有一个index.jsp,显示正确。问题是我的应用程序没有被部署。 (但是Tomcat工作正常,因为我的index.jsp正确显示)。我认为它可能与它是一个弹簧应用程序有关,但我不知道如何克服这个问题(我没有得到任何错误,我不确定在哪里可以找到我的catalina日志)。

我的班级build.gradle:

March 3,March 5,March 6
March 1,March 2
Feb 27

和我的申请:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: 'com.bmuschko.tomcat'

jar {
    baseName = 'gs-accessing-data-jpa'
    version =  '0.1.0'
    manifest {
        attributes 'Main-Class': 'tutorialDatabaseServer.Application'
    }
}

repositories {
    mavenCentral()
    maven { 
        url "https://repository.jboss.org/nexus/content/repositories/releases"
    }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.postgresql:postgresql")
    testCompile("junit:junit")

    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-actuator")
    //    compile("org.postgresql:postgresql:9.4-1201-jdbc41")
    compile("com.fasterxml.jackson.core:jackson-databind")
    compile("org.liquibase:liquibase-core:3.3.3")
    compile("org.springframework.boot:spring-boot-starter-data-mongodb")
    compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
    compile("org.springframework.boot:spring-boot-starter-web")
    compile group: 'org.hibernate', name: 'hibernate-validator', version:'4.2.0.Final'
    compile group: 'javax.el', name: 'el-api', version: '2.2.1-b04'
    compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1212'

    def tomcatVersion = '8.5.16'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
        "org.apache.tomcat.embed:tomcat-embed-logging-juli:8.5.2",
        "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"

}

tomcat {
    httpProtocol = 'org.apache.coyote.http11.Http11Nio2Protocol'
    ajpProtocol  = 'org.apache.coyote.ajp.AjpNio2Protocol'
}


task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

buildscript {

    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.bmuschko:gradle-tomcat-plugin:2.3'
    }
}

非常感谢。

编辑:按照建议更改我的应用程序类后,tomcat日志如下:

@SpringBootApplication
public class Application {

private static final Logger log = 
LoggerFactory.getLogger(Application.class);

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

1 个答案:

答案 0 :(得分:1)

您需要扩展SpringBootServletInitializer并覆盖configure()方法以将其部署为war文件,请参阅spring boot docs

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

}

编辑:

您的build.gradle包含不兼容的日志记录依赖项。在启动时,类加载器无法加载slf4j类并使用java.lang.StackOverflowError退出。要解决这个问题,我必须像下面一样修改build.gradle。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.4.RELEASE")
        classpath 'com.bmuschko:gradle-tomcat-plugin:2.3'
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: 'com.bmuschko.tomcat'

jar {
    baseName = 'gs-accessing-data-jpa'
    version =  '0.1.0'
    manifest {
        attributes 'Main-Class': 'tutorialDatabaseServer.Application'
    }
}

repositories {
    mavenCentral()
    maven {
        url "https://repository.jboss.org/nexus/content/repositories/releases"
    }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile('ch.qos.logback:logback-classic:1.2.3')
    compile('org.slf4j:jcl-over-slf4j:1.7.25')

    compile('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }

    compile("org.springframework.boot:spring-boot-starter-data-jpa") {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }

    compile("org.springframework.boot:spring-boot-starter-actuator") {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }

    compile('org.springframework.boot:spring-boot-starter-thymeleaf') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }

    compile("com.fasterxml.jackson.core:jackson-databind")
    compile("org.liquibase:liquibase-core:3.3.3")
    compile('javax.el:el-api:2.2.1-b04')
    compile('org.postgresql:postgresql:9.4.1212')

    testCompile("junit:junit")

    def tomcatVersion = '8.5.15'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
           "org.apache.tomcat.embed:tomcat-embed-logging-juli:8.5.2",
           "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
    }

    tomcat {
        httpProtocol = 'org.apache.coyote.http11.Http11Nio2Protocol'
        ajpProtocol  = 'org.apache.coyote.ajp.AjpNio2Protocol'
    }

    task wrapper(type: Wrapper) {
        gradleVersion = '2.3'
    }