最近项目中用到了gradle很多高阶用法(对各位大神就无所谓了,对我是高阶了)。正好分享出来,与大家一起学习哦,如果有错误的地方,还请指正!若有看不懂的,也可以留言一起讨论哦!
先看一段自定义task,用来打war包
// 打包war包放置到build/libs(使用JAR来打,后缀为war,当然也可以直接使用war插件)
task assembleWar(type: Jar) {
baseName 'app'
version '1.0.0'
classifier ''
extension 'war'
into('WEB-INF') {
from('../app/WebContent/WEB-INF'){
include '**'
}
}
into('WEB-INF/classes') {
from('../app/build/classes'){
exclude 'container' // 去除一些不用的目录
exclude 'meus'
}
}
destinationDir file('build/libs')
}
有些项目比较复杂,比如我接触的这个,需要在一个项目中打包多个jar,一般情况就需要写多个task,每个task里面指定相应的文件。但一般项目的文件夹都是有规律放置的,就比目前这个项目,根据传递的jar名称的不同就可以定位相应的文件进行打包。
ext{
// jar name list
jarNameList =[
"module_bom",
"module_bom_detail",
"module_bom_feeding",
"module_component",
"module_dataprocess",
"module_parse",
"module_product_config_setting",
"module_product_setting",
"module_product_uph_setting",
"module_vendor",
"module_workorder",
"module_sap"
]
jarVersion= '1.0'
}
// 根据不同的jarName生成多个task, assembleDistributionTask 这个闭包只是用来生成task,并不执行哦
def assembleDistributionTask(jarName) {
// 也就是说在方法里面是可以定义task的哦
task "assembleDistributionJar-${jarName}" (type: Jar) {
baseName jarName.replace("_","-")
version jarVersion
into(baseName + '-' + version){
from('../app/epg'){
include "${jarName}.xml"
include "${jarName}_readme.md"
}
}
into(baseName + '-' + version + '/docs'){
from('../docs/APP API.docx')
include '**'
}
into(baseName + '-' + version + '/setup/others'){
from ('build/libs')
include 'app.war'
}
into(baseName + '-' + version + '/setup/epgs'){
from('../app/epg'){
include "${jarName}.xml"
}
}
into(baseName + '-' + version + '/setup/url-mapping'){
from('../app/url-mapping'){
def urlmapping = jarName.substring(4,jarName.length())
include "${urlmapping}_url_mapping.xml"
}
}
}
}
// 在打jar包前先打war包,因为task "assembleDistributionJar-${jarName}"有用到war包
afterEvaluate {
tasks.matching {
it.name.startsWith('assembleDistributionJar-')
}.each { tk ->
tk.dependsOn(assembleWar)
}
}
// gradle会自动执行这个闭包,生成多个task,这样一个方法就解决了多个task
jarNameList.each { jarName ->
this.assembleDistributionTask(jarName)
}
上面生成的task结果如下:
有时候呢,我们还需要一个批量的,毕竟一个一个还是太慢:
// 自定义任务assembleBatchDistribution,然后让他依赖以“assembleDistributionJar-”开头的所有任务就可以
task assembleBatchDistribution
assembleBatchDistribution.dependsOn {[
tasks.findAll { task ->
task.name.startsWith("assembleDistributionJar-")
}
]
}
结果如下:
那么接下来,对上述的jar上传到maven库中,采用publishing
publishing {
publications {
jarNameList.each {jarName ->
"${jarName}"(MavenPublication) {
groupId priv.allen
artifactId jarName
artifact source:new File("../app/${jarName}/${jarName}.jar"),extension:"jar"
artifact "assembleDistributionJar-${jarName}"{
extension "jar"
classifier "distribution"
}
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.compile.allDependencies.each {
if(null != it.group) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
}
repositories {
maven {
if (jarVersion.endsWith('-SNAPSHOT')) {
url = "snapshotPublishUrl" // 填写真实的
} else {
url = "releasesPublishUrl"
}
credentials {
username = "repoUserName"
password = "repoPassword"
}
}
}
}