【问题标题】:Deploying a Spring application with Tomcat gradle plugin使用 Tomcat gradle 插件部署 Spring 应用程序
【发布时间】:2017-12-30 13:54:32
【问题描述】:

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

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

我的班级 build.gradle:

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

和我的申请:

@SpringBootApplication
public class Application {

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

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

非常感谢。

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

jul 25, 2017 8:53:37 AM org.apache.coyote.AbstractProtocol init
INFORMACIÓN: Initializing ProtocolHandler ["http-nio2-8080"]
jul 25, 2017 8:53:37 AM org.apache.coyote.AbstractProtocol init
INFORMACIÓN: Initializing ProtocolHandler ["ajp-nio2-8009"]
jul 25, 2017 8:53:37 AM org.apache.catalina.core.StandardService startInternal
INFORMACIÓN: Starting service [Tomcat]
jul 25, 2017 8:53:37 AM org.apache.catalina.core.StandardEngine startInternal
INFORMACIÓN: Starting Servlet Engine: Apache Tomcat/8.5.16
jul 25, 2017 8:53:37 AM org.apache.catalina.startup.ContextConfig getDefaultWebXmlFragment
INFORMACIÓN: No global web.xml found
jul 25, 2017 8:54:08 AM org.apache.jasper.servlet.TldScanner scanJars
INFORMACIÓN: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
jul 25, 2017 8:54:09 AM org.apache.jasper.servlet.TldScanner scanJars
INFORMACIÓN: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
jul 25, 2017 8:54:09 AM org.apache.catalina.core.ApplicationContext log
INFORMACIÓN: Spring WebApplicationInitializers detected on classpath: [org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration$JerseyWebApplicationInitializer@1f014eaa]
jul 25, 2017 8:54:09 AM org.apache.coyote.AbstractProtocol start
INFORMACIÓN: Starting ProtocolHandler ["http-nio2-8080"]
jul 25, 2017 8:54:09 AM org.apache.coyote.AbstractProtocol start
INFORMACIÓN: Starting ProtocolHandler ["ajp-nio2-8009"]
jul 25, 2017 8:55:31 AM org.apache.coyote.AbstractProtocol init
INFORMACIÓN: Initializing ProtocolHandler ["http-nio2-8080"]
jul 25, 2017 8:55:31 AM org.apache.coyote.AbstractProtocol init
INFORMACIÓN: Initializing ProtocolHandler ["ajp-nio2-8009"]
jul 25, 2017 8:55:31 AM org.apache.catalina.core.StandardService startInternal
INFORMACIÓN: Starting service [Tomcat]
jul 25, 2017 8:55:31 AM org.apache.catalina.core.StandardEngine startInternal
INFORMACIÓN: Starting Servlet Engine: Apache Tomcat/8.5.16
jul 25, 2017 8:55:31 AM org.apache.catalina.startup.ContextConfig getDefaultWebXmlFragment
INFORMACIÓN: No global web.xml found
jul 25, 2017 8:56:02 AM org.apache.jasper.servlet.TldScanner scanJars
INFORMACIÓN: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
jul 25, 2017 8:56:03 AM org.apache.jasper.servlet.TldScanner scanJars
INFORMACIÓN: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
jul 25, 2017 8:56:03 AM org.apache.catalina.core.ApplicationContext log
INFORMACIÓN: Spring WebApplicationInitializers detected on classpath: [org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration$JerseyWebApplicationInitializer@4988a907, tutorialDatabaseServer.Application@754b42bf]

【问题讨论】:

    标签: spring tomcat gradle web-applications deployment


    【解决方案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'
        }
    

    【讨论】:

    • 现在我收到错误:FAILURE: Build failed with an exception。 * 出了什么问题:任务 ':tomcatRun' 执行失败。 > java.lang.StackOverflowError(无错误消息) * 尝试:使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获得更多日志输出。 @taner
    • 能不能设置插件的 outputFile 来查看tomcat日志中打印的内容。
    • 我已经添加了tomcat日志@taner。再次感谢
    • 您好,依赖项不兼容。这就是它失败的原因。使用有效的 build.gradle 文件编辑了我的回复。它正在 gradle 3.5.1 上加载 Spring Boot 应用程序。我希望它对你有用。
    猜你喜欢
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 2018-09-03
    • 2011-08-10
    • 2016-01-16
    • 2014-12-25
    相关资源
    最近更新 更多