我试图制作一个具有依赖项的jar,因为用NoClassDefFoundError
启动jar时得到了java -Dspring.config.location=myProperties -jar myJar
,经过大量搜索后,我发现可以使用以下解决方案来实现此目的罐子块:
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'
谢谢。
答案 0 :(得分:0)
升级到Gradle 5并为依赖项使用“实现”而不是“编译”后,我也遇到同样的问题。
Gradle构建了一个没有子项目jar或依赖项(根本没有BOOT-INF / lib目录)的主jar。在父项目中将“实现”更改回“编译”仅解决了该问题(没有其他更改)。
因此,显然,Spring Boot 1.5.9 Gradle插件不适用于新的实现配置。请注意,Spring Boot 2和新的bootJar任务都可以正常工作,这个问题仅与旧的bootRepackage和新的实现配置有关。