Reputation: 38607
I am using Gradle 4.0 to package a project to jar.This is my 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 {
}
}
When I am using command to package all project to a single jar,the distination jar file does not contain dependencies.Only have 104KB.How to fix it?This is my package command:
./gradlew -p web -x test build
java version "1.8.0_112"
Upvotes: 6
Views: 11672
Reputation: 2509
With Gradle 7+ you can package the dependency (third-party) jars in your distribution archive like this:
configurations {
runtimeLib.extendsFrom implementation
}
task dist(type: Zip) {
into('MyProjectName') {
from "$workDir"
exclude "log/*", "temp"
into('lib') {
from configurations.runtimeLib
}
includeEmptyDirs = true
}
}
Alternatively you can use the Gradle Distribution plugin. It will collect the dependency jars and will also create start scripts (in project 'build/distributions' directory).
Upvotes: 2
Reputation: 2613
The default jar
task only bundles compiled classes from the target project. If you want to package its dependencies into a so-called fat jar, you can always tweak the jar
task (or a custom task of type Jar
), manually adding elements from the runtime configuration, something like this :
jar {
// Will include every single one of your dependencies, project or not
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
Or better yet, use a dedicated plugin, like 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()
}
Upvotes: 6