java - 带有依赖项的 jar spring boot gradle

标签 java spring-boot gradle build.gradle

我试图制作一个带有依赖项的 jar,因为在使用 java -Dspring.config.location=myProperties -jar myJar 启动 jar 时遇到 NoClassDefFoundError,经过大量搜索后,我发现我可以在 jar block 中使用以下解决方案来实现此目的:

from{
        configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it)}
    }

除了构建 jar 的时间(大约 1 分钟)之外,这一切都很好,并且根据这个答案:Gradle: Build 'fat jar' with Spring Boot Dependencies我不需要创建额外的任务,使用 bootRepackage 就足够了,但我收到了上面提到的 bootRepackage 错误,我不明白为什么.

这是我的 build.gradle 的内容,我使用的是 spring boot 1.5.15:

/*
 * This file was generated by the Gradle 'init' task.
 */
buildscript {
    ext.springBootVersion = '1.5.15.RELEASE'
    ext.managementVersion = '1.0.6.RELEASE'
    ext.axis2Version = '1.7.9'
    repositories {
        mavenCentral()
        mavenLocal()

    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
        //classpath "io.spring.gradle:dependency-management-plugin:${managementVersion}"
        //classpath "com.intershop.gradle.wsdl:wsdl-gradle-plugin:2.0.1"
    }
}

plugins {
    id 'java'
    id 'maven'
    id 'org.springframework.boot' version '1.5.15.RELEASE'
    id 'io.spring.dependency-management' version '1.0.6.RELEASE'

} 

configurations{
    implementation{
        exclude group: 'javax.servlet', module: 'servlet-api'
    }
}

dependencies {

    implementation "org.springframework.boot:spring-boot-starter:${springBootVersion}"
    implementation 'org.springframework.integration:spring-integration-mongodb'
    implementation 'org.springframework.integration:spring-integration-amqp'


    testImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
    testImplementation 'org.mockito:mockito-core:2.6.1'

    implementation 'org.apache.ws.commons.axiom:axiom-jaxb:1.2.20'    

    implementation('org.apache.axis2:axis2-kernel:1.7.9'){
        exclude group: 'javax.servlet', module: 'servlet-api'
        //The exclude above don't work
    }

    implementation "org.apache.axis2:axis2-kernel:${axis2Version}"

    implementation "org.apache.axis2:axis2-wsdl2code-maven-plugin:${axis2Version}"
    implementation "org.apache.axis2:axis2-transport-http:${axis2Version}"


}

wsdl {
    axis2 {
        genNameAxis2 {
           //someAxis2Tasks
        }
    }
}

wsdl2java {

    //someWsdlTasks   
}

wsdl2javaExt {
    cxfVersion = "3.2.1"
}

jar {
    manifest{
        attributes ('Main-Class': 'dummy.Application')
    }

    from{
        configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it)}
    }
    archiveBaseName = 'projectName'
    archiveVersion = '1.0.0'
}

bootRepackage{
    mainClass = 'dummy.Application'
    //classifier = 'boot' I'm getting an error with this argument
}

repositories {
    mavenLocal()    
}


group = 'dummy.group'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'

提前谢谢您。

最佳答案

升级到 Gradle 5 并使用“实现”而不是“编译”作为我的依赖项后,我遇到了同样的问题。

Gradle 构建了一个主 jar,没有子项目 jar 或依赖项(根本没有 BOOT-INF/lib 目录)。在父项目中将“实现”更改回“编译”只能解决问题(没有其他更改)。

因此,显然,Spring Boot 1.5.9 Gradle 插件不适用于新的实现配置。请注意,Spring Boot 2 和新的 bootJar 任务工作正常,此问题仅与旧的 bo​​otRepackage 和新的实现配置有关。

关于java - 带有依赖项的 jar spring boot gradle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54995494/

相关文章:

java - 无法使用Gradle分发获取 'BuildEnvironment'类型的模型

java - 对数组元素的奇数索引求和

java - 在自定义应用程序类中保存对象存储是一种好习惯吗

java - 按值对 Map<Key, Value> 进行排序

mysql - 如何在 Heroku 上为 ClearDB (MySQL) 设置 UTF8 编码

java - $ 不被识别为内部或外部命令

java - 带有自定义编辑器的 SWT 表在调整大小时变得困惑

java - 如何在 Spring Boot 中使用特定日期范围和聚合从 MongoDB 数据库检索数据?

spring-boot - Spring boot - 嵌入式 Tomcat HTTP 到 HTTPS 重定向

android - 多个 dex 文件定义 Landroid/support/v7/util/ThreadUtil$MainThreadCallback 错误