java - Ant对属性名称有什么要求?

标签 java ant

我可以让我的 scriptdef 运行,但前提是我为我的属性使用某些名称。显然,“property”、“prop1”、“prop2”等名称是可以的,但大多数其他名称(包括“propN”)是不行的。哪些是可以的,为什么?

这有效(使用属性名称prop2):

<project name="Ant Is Weird">
  <property name="hostname" value="abc-de-fghijk0.lmn.opqr.stu"/>

  <scriptdef name="hostSubstring" language="javascript">
    <attribute name="text" />
    <attribute name="prop1" />
    <attribute name="prop2" />
    <![CDATA[
      var hn = attributes.get("text");
      project.setProperty(attributes.get("prop1"), hn.replace(/[^0-9]/g,""));
      project.setProperty(attributes.get("prop2"), hn.replace(/[^0-9]/g,""));
      ]]>
  </scriptdef>

  <target name="test" description="helps me learn about scriptdef">
    <echo message="hostname is ${hostname}"/>
    <hostSubstring text="${hostname}" prop1="firstProp" prop2="secondProp"/>
    <echo message="firstProp is ${firstProp}" />
    <echo message="secondProp is ${secondProp}" />
  </target>
</project>

输出:

$ ant test
Buildfile: /apps/antTest/build.xml

test:
     [echo] hostname is abc-de-fghijk0.lmn.opqr.stu
     [echo] firstProp is 0
     [echo] secondProp is 0

BUILD SUCCESSFUL
Total time: 0 seconds

但这失败了(使用属性名称propN):

<project name="Ant Is Weird">
  <property name="hostname" value="abc-de-fghijk0.lmn.opqr.stu"/>

  <scriptdef name="hostSubstring" language="javascript">
    <attribute name="text" />
    <attribute name="prop1" />
    <attribute name="propN" />
    <![CDATA[
      var hn = attributes.get("text");
      project.setProperty(attributes.get("prop1"), hn.replace(/[^0-9]/g,""));
      project.setProperty(attributes.get("propN"), hn.replace(/[^0-9]/g,""));
      ]]>
  </scriptdef>

  <target name="test" description="helps me learn about scriptdef">
    <echo message="hostname is ${hostname}"/>
    <hostSubstring text="${hostname}" prop1="firstProp" propN="secondProp"/>
    <echo message="firstProp is ${firstProp}" />
    <echo message="secondProp is ${secondProp}" />
  </target>
</project>

输出:

$ ant test
Buildfile: /apps/antTest/build.xml

test:
     [echo] hostname is abc-de-fghijk0.lmn.opqr.stu

BUILD FAILED
/apps/antTest/build.xml:17: java.lang.NullPointerException
        at java.util.Hashtable.containsKey(Hashtable.java:335)
        at org.apache.tools.ant.PropertyHelper.setProperty(PropertyHelper.java:640)
        at org.apache.tools.ant.Project.setProperty(Project.java:538)
        at jdk.nashorn.internal.scripts.Script$\^eval\_.:program(<eval>:8)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:446)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:403)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:399)
        at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
        at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tools.ant.util.ReflectUtil.invoke(ReflectUtil.java:108)
        at org.apache.tools.ant.util.ReflectWrapper.invoke(ReflectWrapper.java:81)
        at org.apache.tools.ant.util.optional.JavaxScriptRunner.evaluateScript(JavaxScriptRunner.java:103)
        at org.apache.tools.ant.util.optional.JavaxScriptRunner.executeScript(JavaxScriptRunner.java:67)
        at org.apache.tools.ant.taskdefs.optional.script.ScriptDef.executeScript(ScriptDef.java:350)
        at org.apache.tools.ant.taskdefs.optional.script.ScriptDefBase.execute(ScriptDefBase.java:50)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
        at org.apache.tools.ant.Task.perform(Task.java:348)
        at org.apache.tools.ant.Target.execute(Target.java:390)
        at org.apache.tools.ant.Target.performTasks(Target.java:411)
        at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
        at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
        at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
        at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
        at org.apache.tools.ant.Main.runBuild(Main.java:809)
        at org.apache.tools.ant.Main.startAnt(Main.java:217)
        at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
        at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)

Total time: 0 seconds

如果重要的话:

$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.8 (Santiago)
$ ant -version
Apache Ant(TM) version 1.8.2 compiled on December 20 2010

最佳答案

属性自动小写。我通过放置发现了这一点

self.log(attributes);

进入脚本。

因此,如果您将脚本更改为使用 propn 而不是 propN,它就会起作用:

project.setProperty(attributes.get("propn"), hn.replace(/[^0-9]/g,""));

这记录在 Scriptdef task documentation 中:

Note: Ant will turn all attribute and element names into all lowercase names, so even if you use name="SomeAttribute", you'll have to use "someattribute" to retrieve the attribute's value from the attributes collection.

关于java - Ant对属性名称有什么要求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40662002/

相关文章:

java - 我尝试使用 Hashmap get() 返回类数据类型,但出现错误,提示 "must return value of type"。请指教

java - NetBeans + Microsoft SQL Server 2012 jbdc 驱动程序

java - 如何指定在 Ivy 中使用哪个 jar 签名者

java - 从命令行使用 Eclipse ant 插件?

java - 使用 Maven Ant 任务执行 jetty stop 时未授予权限 (java.lang.RuntimePermission exitVM)

java - jar 内的 NoClassDefFoundError

java - Imgproc.drawContours(....) 未显示

java - 电话号码分割(国际版)java

java - 如何显示txt文件之间的不同单词?

java - 使用 Ant 构建工具 "can' t 打开文件 'C:\Program' : [Errno 2] No such file or directory"构建错误