美文网首页
gradle task 运行jar 并适配Windows和Lin

gradle task 运行jar 并适配Windows和Lin

作者: Yellowtail | 来源:发表于2018-09-14 16:25 被阅读0次

    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()
    }
    

    参考

    相关文章

      网友评论

          本文标题:gradle task 运行jar 并适配Windows和Lin

          本文链接:https://www.haomeiwen.com/subject/wuwogftx.html