我有一个gradle项目,它成功构建了一个胖jar(即包含它自己的依赖项)。然而,我需要的是建造两个罐子:
除此之外,它们应该完全相同。
我可以在gradle中这样做吗?
我要求我只能拨打一次gradle(即不能为每个jar调用gradle一次)。
我的gradle文件的相关部分如下所示:
buildscript {
ext {
springBootVersion = '1.3.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:0.5.4.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: "io.spring.dependency-management"
jar {
baseName = 'demo'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
...
compile('org.postgresql:postgresql:42.1.1.jre7') // or compile('postgresql:postgresql:8.4-702.jdbc4')
...
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
...
task wrapper(type: Wrapper) {
gradleVersion = '2.9'
}
答案 0 :(得分:1)
如果这有助于任何人,我就是这样做的:
将现有jar提取到文件夹中:
task extractJar(type: Copy, dependsOn: [otherTasksThatBuildFirstJar]) {
from zipTree(jar.archivePath)
into 'build/jarContents'
}
删除我不想要的依赖项:
task deletePostgres(type: Delete, dependsOn: [extractJar]) {
delete fileTree('build/jarContents/lib') {
include '**/postgres*.jar'
}
}
添加我想要的依赖项:
task replacePostgres(type: Copy, dependsOn: [deletePostgres]) {
from 'lib/postgresql-8.4-702.jdbc4.jar'
into 'build/jarContents/lib'
}
构建第二个jar:
task buildSecondJar(type: Jar, dependsOn: [replacePostgres]) {
baseName 'my-second-jar'
entryCompression ZipEntryCompression.STORED //prevents compression of jars
from files('build/jarContents')
destinationDir project.rootDir
manifest {
attributes(
'Start-Class': 'com.somewhere.MyApplication',
'Spring-Boot-Classes': 'BOOT-INF/classes/',
'Spring-Boot-Lib': 'BOOT-INF/lib/',
'Spring-Boot-Version': '1.5.8.RELEASE',
'Main-Class': 'org.springframework.boot.loader.JarLauncher'
)
}
}
答案 1 :(得分:0)
如果可以为一个jar运行gradle一次,那么一个简单的命令行属性和一个if块就可以完成这项任务。另见:Passing properties to a gradle build
同时查看重复的问题:Gradle How to build several versions of a jar with different dependencies