我在gradle构建脚本中有多个类似的任务。每个任务都将构建一个jar。任务看起来像这样:
task fatJarAndServicePackageXXX(type: Jar) {
baseName = 'adapter-XXX'
def mainClass = 'Gateway'
def specialFlags = ['-Xmx1g']
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
def outPackageName = '../build/' + baseName + '-package.tar.gz'
def filesToBeIncluded = [
baseName + '/' + archiveName,
baseName + '/' + baseName + '.service',
baseName + '/Makefile',
baseName + '/Makefile.properties' ]
def serviceLogPath = '/var/log/' + baseName
def commonFlags = [
"-DsleepMicros=" + "10000"
]
doLast {
copy {
from outputs.files.asPath
into '../build/' + baseName
}
copy {
from '../deployment/template.service'
into '../build/' + baseName
expand(
"JAR_NAME": archiveName,
"SPECIAL_FLAGS": specialFlags.join(" "),
"COMMON_FLAGS": commonFlags.join(" "),
"SERVICE_NAME": baseName,
"MAIN_CLASS": mainClass
)
rename('template.service', baseName + '.service')
}
}
}
我为不同的XXX提供了大量此类任务,因此我决定创建一个自定义任务类,这将有助于删除重复的代码。该类如下所示:
class FatJarAndServicePackage extends Jar {
String baseName
String mainClass
private String heapSize = '1g'
List<String> specialFlags = []
String serviceLogPath = '/var/log/' + baseName
List<String> commonFlags = [
"-DsleepMicros=" + 10000
]
void setHeapSize(final String size) {
heapSize = size
}
@Override
Task doLast(final Action<? super Task> action) {
from {
project.configurations.compile.collect { it.isDirectory() ? it : project.zipTree(it) }
}
with jar
project.copy {
from outputs.files.asPath
into project.projectDir.toPath().resolve("build").resolve(baseName)
}
project.copy {
from '../deployment/template.service'
into '../build/' + baseName
expand(
"JAR_NAME": archiveName,
"SPECIAL_FLAGS": specialFlags.join(" ") + " -Xmx" + heapSize,
"COMMON_FLAGS": commonFlags.join(" "),
"SERVICE_NAME": baseName,
"MAIN_CLASS": mainClass
)
rename('template.service', baseName + '.service')
}
project.copy {
from '../deployment/Makefile.properties'
into '../build/' + baseName
expand(
"JAR_NAME": archiveName,
"BASE_NAME": baseName
)
}
project.copy {
from '../deployment/Makefile'
into '../build/' + baseName
}
makeTar()
return super.doLast(action)
}
所以它不起作用。问题是我不知道该在哪里放置线:
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
如何在自定义任务类中实现这两行?感谢任何帮助。