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

时间:2016-11-17 18:05:09

标签: java ant

我可以让我的scriptdef运行,但前提是我的属性使用了某些名称。显然,像“属性”和“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

1 个答案:

答案 0 :(得分:1)

属性是自动降低的。我发现了

self.log(attributes);

进入剧本。

因此,如果您将脚本更改为使用propn而非propN,则可以使用:

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

Scriptdef task documentation

中记录了这一点
  

注意:Ant会将所有属性和元素名称转换为全部小写名称,因此即使使用name="SomeAttribute",也必须使用"someattribute"从属性集合中检索属性的值。