gradle task 运行jar 并适配Windows和Lin

2018-09-14  本文已影响0人  Yellowtail

question

  1. 我现在有一个工程是需要用swagger 通过写好的json文件来生成Java代码
  2. 开发电脑是Windows的,构建机是Linux 的,所以想把task写成适配两种环境的

solution

运行jar 包,Windows环境可以看task genCode
linux 环境可以看task genCodeOnLinux
判断环境,可以看 isWindows

task genCode(type:Exec, description:"Windows生成Java代码") {
    commandLine  "cmd", "/c", "java -jar swagger-codegen-cli.jar generate -i data/swagger.json -l jaxrs-resteasy -o ./sms-java"
    
    println "generate java code success"
}

task genCodeOnLinux(type: Exec, description:"Linux环境生成Java代码") {
  executable "sh"
  args "-c", "java  -jar swagger-codegen-cli.jar generate -i data/swagger.json -l jaxrs-resteasy -o ./sms-java"
  
  println "generate java code success"
}

task copyFiles(type: Copy) {
    from 'sms-java/src/gen'
    into '../sms-web/src/gen'
    
    println "copy java code success"
}

task cleanGenCode(type: Delete) {
    println 'delete sms-java'
    delete 'sms-java'
}

def isWindows() {
    return org.gradle.internal.os.OperatingSystem.current().isWindows()
}

project.assemble.doFirst {
    println "assemble start"
    
    tasks.cleanGenCode.execute()   

    if(isWindows()) {
        println "now is on windows"
        tasks.genCode.execute()
    } else {
        println "now is on linux"
        tasks.genCodeOnLinux.execute()
    }
    
    tasks.copyFiles.execute()
}

参考

上一篇下一篇

猜你喜欢

热点阅读