Gradle:在编译时删除不需要的参数

时间:2018-07-19 15:34:10

标签: java gradle

我正在尝试使用Gradle编译Java应用程序。过去,它是使用JDK 1.3.1批量进行编译的,但是我试图将所有公司构建逻辑迁移到Gradle,以便我可以自动化并进行Continuos Integration and Delivery。

此应用程序在只能访问JRE 1.3的计算机上运行,​​并且无法升级。

该应用程序使用append关键字,并且在使用JDK 1.8进行以下编译时:

targetCompatibility = 1.3
sourceCompatibility = 1.3

程序将抛出java.lang.NoSuchMethodError。使用JDK 1.3.1编译后,应用程序运行良好。

首先尝试[不起作用]

我尝试设置:

compileJava {
    options.fork = true
    options.forkOptions.executable = "c:/Program Files (x86)/Java/jdk1.3.1_28/bin/javac
    options.encoding = "windows-1252"
}

但是当用Java 1.3编译时,我得到了错误

> Task :compileJava FAILED
javac: invalid flag: -source
Usage: javac <options> <source files>
where possible options include:
  -g                        Generate all debugging info
  -g:none                   Generate no debugging info
  -g:{lines,vars,source}    Generate only some debugging info
  -O                        Optimize; may hinder debugging or enlarge class file
  -nowarn                   Generate no warnings
  -verbose                  Output messages about what the compiler is doing
  -deprecation              Output source locations where deprecated APIs are used
  -classpath <path>         Specify where to find user class files
  -sourcepath <path>        Specify where to find input source files
  -bootclasspath <path>     Override location of bootstrap class files
  -extdirs <dirs>           Override location of installed extensions
  -d <directory>            Specify where to place generated class files
  -encoding <encoding>      Specify character encoding used by source files
  -target <release>         Generate class files for specific VM version

我尝试了所有删除-source arg的方法(即使使用了-target <ver>,在gradle文档上也声明了删除臭名昭著的arg),但无济于事。

第二次尝试[工作]

对于第二种实现,我禁用了任务compileJava,并创建了一个全新的任务,该任务使用类型Exec

进行编译
compileJava.enabled = false

//This is needed otherwise the javac command will get an error because it's missing the classes directory
task prepareBuildingDir(type: Exec) {
    def separator
    def shellType
    def shellArgs
    def args = "mkdir"
    if (OperatingSystem.current().isLinux()) {
        separator = "/"
        shellType = "sh"
        shellArgs = "-c"
        args += " -p"
    } else {
        separator = "\\"
        shellType = "cmd"
        shellArgs = "/c"
    }
    args += " .${separator}build${separator}classes${separator}java${separator}main"

    commandLine = [shellType, shellArgs, args]
}

//This is the new compiler task
task customCompile(type: Exec) {
    def appDir = workingDir.toString()
    workingDir "${j3home}bin"
    def separator
    def shellType
    def shellArgs
    if (OperatingSystem.current().isLinux()) {
        separator = "/"
        shellType = "sh"
        shellArgs = "-c"

    } else {
        separator = "\\"
        shellType = "cmd"
        shellArgs = "/c"
    }
    def classPathFiles = ""
    configurations.runtime.each { classPathFiles += "${it};" }
    def javaArgs = ".${separator}javac -O " +
            "-d ${appDir}${separator}build${separator}classes${separator}java${separator}main " +
            "-encoding windows-1252 " +
            "-g " +
            "-sourcepath \"\" " +
            "-classpath " +
                "${classPathFiles} " +
            "${sourceSets.main.allJava.files.toString().replace('[','').replace(']', '').replace(',', '')}"

    commandLine = [shellType, shellArgs, javaArgs]
}

//Here we set the new dependencies so triggering a job that requires the compilation will trigger the custom one
tasks.prepareBuildingDir.dependsOn clean
tasks.customCompile.dependsOn prepareBuildingDir
tasks.processResources.dependsOn customCompile

问题

第二种方法很冗长,容易出错,有一种方法可以删除compileJava传递的标准标志?

0 个答案:

没有答案