【问题标题】:Gradle not creating a Tomcat deployable warGradle 未创建 Tomcat 可部署战争
【发布时间】:2018-12-21 05:59:54
【问题描述】:

我遇到了与 Gradle 4.8.1 建立战争的问题。这是 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'
        }
    }
}

我还尝试删除“maven”插件、archivesBaseName 和 uploadArchive 任​​务,同时取消注释战争任务,我得到了相同的结果。使用uploadArchive 时,战争可以很好地部署到nexus 服务器,并且我没有收到任何错误。将战争部署到 tomcat(在这两种情况下)时,tomcat 7 和 8 都没有抛出错误,并且我没有收到来自 catalina 或项目的日志,尽管战争任务中的 archiveName 没有正确重命名战争。我已经在其他两台机器/tomcat 实例上尝试过,结果相同。将其构建为胖 jar 或在 IntelliJ 中运行时,一切都按预期工作(包括日志记录)。

任何帮助或指导将不胜感激。

【问题讨论】:

    标签: spring maven tomcat gradle war


    【解决方案1】:

    听起来它根本没有初始化 servlet。确保扩展 SpringBootServletInitializer 以进行 WAR 部署。

    【讨论】:

    • 好电话,我确实忘记包含 ServletInitializer 但添加后,战争仍然没有正确添加端点。我的一个端点以 php 结尾,因为它最初是用 php 编写的,当我点击位于资源/静态中的 index.html 时,我得到了文件未找到而不是错误请求,并且什么也没有。
    • 如果提取战争的内容,是否有合适的目录结构?另外,您现在是否在 Tomcat 中看到任何日志表明已注册任何端点?
    • 抱歉这么久没有回复,还有很多其他的东西我不得不跳。实际上,我还必须对这个项目进行一些更新,并且在使用 IntelliJ 中的模块检查 gradle 分支时遇到了问题。所以我想我会从头开始并用 Kotlin 重写,但不能再上传了。查看此处的文档docs.gradle.org/current/userguide/war_plugin.html,看起来我需要实现一个类似 maven 的结构,但使用 Spring Boot 认为这是非常不必要的。惊讶的 maven 更容易使用
    【解决方案2】:

    我也面临同样的问题。问题是当战争部署在tomcat上时,Spring应用程序没有被初始化。

    经过一番挣扎,我发现我需要在我的应用程序中扩展SpringBootServletInitializer。所以我的有效代码看起来像

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

    看起来像这样SpringBootServletInitializer 指示战争插件在构建战争时生成引导代码,因此在部署应用程序时初始化弹簧上下文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 2012-11-27
      • 1970-01-01
      相关资源
      最近更新 更多