简体   繁体   English

Java Ant build.xml问题

[英]Java Ant build.xml issue

I am writing a build file for its basic run, clean and compile properties. 我正在为其基本运行,清理和编译属性编写构建文件。 Here is what I have: 这是我所拥有的:

<?xml version="1.0" encoding="ISO-8859-1"?>

<project default="compile">
   <description>
      Compile and run the Java files for Lab7
   </description>

   <target name="prob1" depends='compile'>
      <java classname='prob1'>
         <classpath path='./'/>
         <arg value='Gertrude'/>
         <arg value='Justin'/>
      </java>
   </target>

   <target name="prob2" depends='compile'>
      <java classname='prob2'>
         <classpath path='./'/>
         <arg value='28'/>
      </java>
   </target>

   <target name="prob3" depends='compile'>
      <java classname='prob3'>
         <classpath path='./'/>
         <arg value='2000'/>
      </java>
   </target>

   <target name="prob4" depends='compile'>
      <java classname='prob4'>
         <classpath path='./'/>
         <arg value='2'/>
      </java>
   </target>

  <target name="compile">
    <javac srcdir='./' includeantruntime="false"/>
  </target>

  <target name="clean">
    <delete>
        <fileset dir="./">
            <include name='*.class'/>
        </fileset>
    </delete>
  </target>
</project>

I am trying to run each prob with different arguments one at a time. 我试图一次用不同的参数运行每个概率。 Like in prob1 i want to run it with the first name then again with the second name, how do I do this? 就像在prob1中一样,我想以第一个名称运行,然后再次以第二个名称运行,我该如何做?

Yes something like that is possible using antlib, which offers a wide range of functionalities like for loops, if conditions and macros. 是的,使用antlib可以实现类似的功能,它提供了广泛的功能,例如for循环,条件和宏。 You can define a macro like 您可以定义一个宏

<macrodef name="call-cc">
   <attribute name="target"/>
   <attribute name="param1">
   <attribute name="param2">
   <element name="yourtask">
      <java classname='$name'>
        <classpath path='./'/>
        <arg value='@{param1}'/>
        <arg value='@{param2}'/>
      </java>
   </element>

</macrodef>

and then you can call it like this 然后你可以这样称呼它

<call-cc target="unittests" param1="bla" param2="blabla"/>

You can read about antlib in the manual 您可以在手册中阅读有关antlib的信息。

As explained by @Moataz and @jheddings, the ANT macrodef is the best way to abstract these kinds of repetitive and parameterized actions. 正如@Moataz和@jheddings所解释的那样,ANT macrodef是抽象此类重复性和参数化操作的最佳方法。

The following working example also demonstrates some further refinements to your build, such as using properties to abstract the location of your projects files and the use of ANT paths to control runtime classpaths. 以下工作示例还演示了对构建的进一步改进,例如使用属性抽象化项目文件的位置以及使用ANT路径来控制运行时类路径。

<project default="compile">

    <description>
    Example build file. Demonstrates the following
    - Properties
    - Build paths
    - Running java programs using a macrodef
    </description>

    <property name="src.dir"     location="src/main/java"/>
    <property name="build.dir"   location="build"/>
    <property name="classes.dir" location="build/classes"/>

    <path id="runtime.path">
        <pathelement location="${classes.dir}"/>
    </path>

    <macrodef name="call-prog">
        <attribute name="classname"/>
        <attribute name="arg1"/>
        <attribute name="arg2" default=""/>
        <sequential>
            <java classname='@{classname}' classpathref="runtime.path">
                <arg value='@{arg1}'/>
                <arg value='@{arg2}'/>
            </java>
        </sequential>
    </macrodef>

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac destdir="${classes.dir}" srcdir='${src.dir}' includeantruntime="false"/>
    </target>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

   <target name="prob1" depends='compile'>
        <call-prog classname="org.demo.App" arg1="Justin" arg2="Gertrude"/>
   </target>

   <target name="prob2" depends='compile'>
        <call-prog classname="org.demo.App" arg1="28"/>
   </target>

   ..
   ..   
</project>

Finally, when you've built up a store of common macodefs that you use in all your builds you can consider packaging them as an ANTlib . 最后,当您建立了在所有内部版本中使用的通用macodef的存储库时,可以考虑将它们打包为ANTlib Best way to share build logic in ANT in my opinion. 我认为,在ANT中共享构建逻辑的最佳方法。 For this simple example it would be overkill. 对于这个简单的例子,这将是多余的。

You can do this using antcall or macrodef like so: 您可以使用antcallmacrodef来执行此操作, antcall所示:

<target name="run-prob" depends="compile">
  <fail unless="prob-class" />
  <fail unless="prob-args" />

  <java classname="${prob-class}">
    <classpath path="./"/>
    <arg line="${prob-args}"/>
  </java>
</target>

<target name="prob1" depends="compile">
  <antcall target="run-prob">
    <param name="prob-class">prob1</param>
    <param name="prob-args">justin gertrude</param>
  </antcall>
</target>

<target name="prob2" depends="compile">
  <prob class="prob2">
    <arg value='28'/>
  </prob>
</target>

<macrodef name="prob">
  <attribute name="class" />
  <element name="args" implicit="true" />
  <sequential>
    <java classname="@{class}">
      <classpath path="./"/>
      <args />
    </java>
  </sequential>
</macrodef>

The advantage of using antcall is that you could execute calls from the command line, like so: 使用antcall的优点是您可以从命令行执行调用,如下所示:

ant -Dprob-class=prob3 -Dprob-args=2000 run-prob

The advantage of using macrodef is that is looks a bit cleaner and more obvious in the ant file. 使用macrodef的优点是在ant文件中看起来更清晰一些,也更明显。 Of course, you could also mix the two methods and call the macro from your antcall target or vice-versa. 当然,您也可以混合使用这两种方法,并从您的antcall目标调用宏,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM