简体   繁体   English

带有依赖项的Jar Spring Boot Gradle

[英]Jar with dependencies spring boot gradle

I was trying to make a jar with dependencies because I was getting a NoClassDefFoundError when starting the jar with java -Dspring.config.location=myProperties -jar myJar , after a lot of searching I found that I can achieve this using the following solution in the jar block: 我试图制作具有依赖项的jar,因为在通过java -Dspring.config.location=myProperties -jar myJar启动jar时遇到了NoClassDefFoundError ,经过大量搜索后,我发现可以使用以下解决方案来实现此目的罐子块:

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

And all good with this except for the amount of time when building the jar (1 minute aprox), and according with this answer: Gradle: Build 'fat jar' with Spring Boot Dependencies I don't need to create an additional task, is enough with the bootRepackage but I'm getting the error that I mentioned above with bootRepackage and I don't understand why. 一切都很好,除了构建jar的时间(大约1分钟),并根据以下答案: Gradle:使用Spring Boot Dependencies构建“胖jar”,我不需要创建其他任务,是对于bootRepackage来说已经足够了,但是我收到了我上面提到的关于bootRepackage的错误,我不明白为什么。

This is the content of my build.gradle and I'm using spring boot 1.5.15: 这是我的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'

Thank you in advance. 先感谢您。

I have this same problem after upgrading to Gradle 5 and using 'implementation' instead of 'compile' for my dependencies. 升级到Gradle 5并为依赖项使用“实现”而不是“编译”后,我遇到了同样的问题。

Gradle built a main jar with no sub-project jars or dependencies (no BOOT-INF/lib directory at all). Gradle构建了一个没有子项目jar或依赖项(根本没有BOOT-INF / lib目录)的主jar。 Changing 'implementation' back to 'compile' in the parent project only fixed the problem (with no other changes). 在父项目中将“实现”更改回“编译”仅解决了该问题(没有其他更改)。

So, apparently, the Spring Boot 1.5.9 Gradle plugin does not work with the new implementation configuration. 因此,显然,Spring Boot 1.5.9 Gradle插件不适用于新的实现配置。 Note that Spring Boot 2 and the new bootJar task work fine, this issue is only with the old bootRepackage and the new implementation configuration. 请注意,Spring Boot 2和新的bootJar任务可以正常工作,这个问题仅与旧的bootRepackage和新的实现配置有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM