gradle - 在Gradle Task/Groovy函数中访问Ant属性?

标签 gradle ant

我正在尝试在gradle函数中使用ant.uptodate函数,以检查某些文件是否已更改。

task generateXMLBeans {
    ant.uptodate(property: "xsdFileChanges", targetfile: "lib/xmlBeans.jar") {
            srcfiles(dir: 'protocol') {
                include(name: '*.xsd')
            }
    }
    if(ant.properties.xsdFileChanges == "false") {
        ant.taskdef(name: 'xmlbean', classname: 'org.apache.xmlbeans.impl.tool.XMLBean',
            classpath: configurations.xmlbeans.asPath)
            ant.xmlbean(
                javasource: "1.5", 
                failonerror: "true",
                fork: "yes",
                memoryMaximumSize: "512M",
                memoryInitialSize: "64M",
                destfile: "lib/xmlBeans.jar",
                classpath: configurations.xmlbeans.asPath){
                    fileset(dir: 'protocol') {
                        include(name: '*.xsdconfig')
                        include(name: 'hmiprotocol.xsd')
                    }
                }    
    }
}

但是变量ant.properties.xsdFileChanges null

我试图将ant.uptodate函数放在一个单独的函数中,结果相同。

当我使用--debug函数调用任务时,我可以看到ant.uptodate函数通常可以正常工作。
14:40:50.947 [DEBUG] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:uptodate] hmiprotocol_systemlog.xsd omitted as D:\workspace_201912\ProjectX\lib\xmlBeans.jar is up to date.
14:40:50.948 [DEBUG] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:uptodate] hmiprotocol_testdetails.xsd omitted as D:\workspace_201912\ProjectX\lib\xmlBeans.jar is up to date.

有谁知道如何正确访问ant属性?

最佳答案

这与在Gradle中使用Ant无关;这就是Ant的uptodate属性的工作方式。如果文件不是最新的,则指定的属性未设置为false。根本没有设置。其他一些Ant任务(例如condition)也使用相同的约定。

假设您在同一目录中同时拥有一个build.xml和build.gradle文件,可以尝试使用Ant本身的一个简单示例:

<target name="default">
    <uptodate property="uptodate1" targetfile="build.xml" srcfile="build.gradle" />

    <echo>${uptodate1}</echo>

    <uptodate property="uptodate2" targetfile="build.gradle" srcfile="build.xml" />

    <echo>${uptodate2}</echo>
</target>

输出(可以根据修改的时间取反):
default:
     [echo] true
     [echo] ${uptodate2}

关于gradle - 在Gradle Task/Groovy函数中访问Ant属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60183055/

相关文章:

java - Ant loadproperties 失败(bcel 错误?)

android - 将应用内消息传递SDK添加到Gradle后出错

java - Eclipse 上的 Gradle,钻石运算符无法识别?

ant - 如何使用jaxb和Ant xjc任务处理多个xsd模式?

eclipse - 检测 Eclipse Ant 构建器中的错误

java - 如何让 Ant 在 javac 中使用 JAXB 2.2.x 而不是 Java 6 SE JAXB 类?

javac 无法在 .jar 中找到类,即使它们存在?

selenium-webdriver - 什么是cumingle依赖于cumulium.api.Scenario

gradle - 获取 SourceSet 完整路径字符串或路径目录

Android Studio 3.0 构建变体与风格不匹配