【问题标题】:Jar with dependencies spring boot gradle带有依赖项的jar spring boot gradle
【发布时间】:2019-07-26 11:43:47
【问题描述】:

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

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

除了构建 jar 的时间(大约 1 分钟)之外,这一切都很好,并且根据这个答案:Gradle: Build 'fat jar' with Spring Boot Dependencies 我不需要创建额外的任务,@987654326 就足够了@ 但我收到了上面提到的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'

提前谢谢你。

【问题讨论】:

  • 你得到的完整错误是什么?
  • 删除jarbootRepackage 任务。只要运行./gradlew build,你就会得到一个可执行的jar。
  • 在@M.Deinum 建议之后,我得到了同样的错误:Exception in thread "main" java.lang.NoClassDefFoundError: privateDependency 这是错误@AndyWilkinson @Phil 是的,我读过它,但适用于 spring 2.0.0,我' m 使用 spring 1.5.15
  • 如果您在运行常规 Spring Boot 任务时遇到此异常,您的依赖项不在 jar 中包含的配置之一中。请在您的问题中发布完整的堆栈跟踪(和更新的构建文件)。

标签: java spring-boot gradle build.gradle


【解决方案1】:

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

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

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

【讨论】:

  • 我刚刚将所有“实现”替换为“编译”(包括“testCompile”),现在我有了一个可以工作的独立 JAR。非常感谢! (我将 Gradle 6.7 与旧的 SpringBoot 1.5.9 一起使用)
猜你喜欢
  • 2016-02-08
  • 1970-01-01
  • 2016-09-17
  • 2020-03-31
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 2021-02-14
  • 2018-01-31
相关资源
最近更新 更多