gpt4 book ai didi

java - 如何让gradle打包所有依赖jar?

转载 作者:行者123 更新时间:2023-11-30 05:44:05 51 4
gpt4 key购买 nike

我正在使用 Gradle 4.0 将项目打包到 jar。这是我的 build.gradle:

group 'dolphin'
version '1.0-SNAPSHOT'

buildscript {
ext {
springBootVersion = '1.4.5.RELEASE'
springVersion = '4.3.7.RELEASE'
springfoxVersion = '2.6.1'
jacksonVersion = '2.8.7'
lombokVersion = '1.16.14'
}
ext['tomcat.version'] = '8.0.35'

repositories {
mavenCentral()
jcenter{
url 'http://jcenter.bintray.com'
}
}

dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")

}
}

def getVersionCode() {
def versionFile = file("$rootDir/version.properties")
if (!versionFile.canRead()) {
throw new GradleException("Could not find version.properties!")
}
def versionProps = new Properties()
versionProps.load(new FileInputStream(versionFile))
def versionCode = versionProps['VERSION'].toString()
return versionCode
}

repositories {
mavenCentral()
}

allprojects {
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
repositories {
mavenCentral()
}
}


task wrapper(type: Wrapper) {
description = 'Generates gradlew[.bat] scripts'
gradleVersion = '4.0'
}


project(":common") {
description = ''

dependencies {
compile("org.springframework:spring-context:" + springVersion)
compile("commons-codec:commons-codec:1.10")
compile("org.apache.tomcat:tomcat-juli:" + property('tomcat.version'))
compile 'org.springframework.boot:spring-boot-starter-web'
compile("io.springfox:springfox-swagger2:${springfoxVersion}")
compile group: 'io.swagger', name: 'swagger-annotations', version: '1.5.20'
compile("org.projectlombok:lombok:${lombokVersion}")
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.5'
compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '1.3.0'
compile group: 'org.mybatis', name: 'mybatis', version: '3.4.4'
compile group: 'org.mybatis', name: 'mybatis-typehandlers-jsr310', version: '1.0.2'
}
}

/*project(":api") {

description = 'dolphin-api'

dependencies {
compile project(":business")
compile project(":data")
compile project(":composite")
compile project(":common")
compile group: 'org.apache.tomcat', name: 'tomcat-juli', version: property('tomcat.version')
compile("org.projectlombok:lombok:1.16.10")
compile("org.springframework.boot:spring-boot-devtools")
compile("org.springframework:spring-test:" + springVersion)
compile("org.springframework.boot:spring-boot-test:" + springBootVersion)
testCompile group: 'junit', name: 'junit', version: '4.12'
}
}*/

project(":composite") {

description = 'dolphin-composite'

dependencies {
compile project(":business")
compile project(":data")
compile("org.springframework:spring-context:" + springVersion)
}
}


project(":web") {

description = "web"

jar {
baseName = "dolphin-web-" + getVersionCode()
}

dependencies {
implementation project(':business')
implementation project(':api')
implementation project(':common')
implementation project(':data')
implementation project(':composite')
implementation("com.zaxxer:HikariCP:2.6.0")
implementation("mysql:mysql-connector-java:5.1.24")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter")
implementation group: 'org.apache.tomcat', name: 'tomcat-juli', version: property('tomcat.version')
implementation("org.projectlombok:lombok:1.16.14")
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.5'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.24'
implementation group: 'org.mybatis', name: 'mybatis', version: '3.4.2'
implementation group: 'org.mybatis', name: 'mybatis-typehandlers-jsr310', version: '1.0.2'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
}

project(":business") {

description = "business"

dependencies {
compile project(':data')
compile project(':common')
compile('org.springframework.boot:spring-boot-starter-web')
}
}

project(":data") {

description = "data"

dependencies {
compile project(':dolphin-mybatis')
compile project(':common')
compile("org.projectlombok:lombok:${lombokVersion}")
compile("com.zaxxer:HikariCP:2.6.0")
compile group: 'org.postgresql', name: 'postgresql', version: '42.1.4'
compile("org.hibernate:hibernate-validator:5.2.4.Final")
compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:1.1.1")
compile("org.apache.commons:commons-lang3:3.5")
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.24'
compile group: 'org.mybatis', name: 'mybatis', version: '3.4.2'
compile group: 'org.mybatis', name: 'mybatis-typehandlers-jsr310', version: '1.0.2'
testCompile group: 'junit', name: 'junit', version: '4.11'
compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion")

}
}

project(":dolphin-mybatis") {

description = "dolphin-mybatis"

dependencies {

}
}

当我使用命令将所有项目打包到单个 jar 时,目标 jar 文件不包含依赖项。只有 104KB。如何修复它?这是我的打包命令:

./gradlew -p web -x test build

java版本“1.8.0_112”

最佳答案

默认的 jar 任务仅捆绑目标项目中的已编译类。如果您想将其依赖项打包到所谓的 fat jar 中,您可以随时调整 jar 任务(或类型为Jar),从运行时配置中手动添加元素,如下所示:

jar {
// Will include every single one of your dependencies, project or not
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}

或者更好的是,使用专用插件,例如 Gradle Shadow :

buildscript {
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:4.0.2'
}
}

apply plugin: 'com.github.johnrengelman.shadow'

...

// Outputs to build/libs/dolphin-web-<version>.jar
shadowJar {
baseName = 'dolphin-web'
classifier = null
version = getVersionCode()
}

关于java - 如何让gradle打包所有依赖jar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55142587/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com