首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java Ant build.xml问题

Java Ant build.xml问题
EN

Stack Overflow用户
提问于 2012-11-10 22:53:35
回答 3查看 1.8K关注 0票数 4

我正在为它的基本运行、清理和编译属性编写一个构建文件。这就是我所拥有的:

代码语言:javascript
复制
<?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>

我试着用不同的参数一次一个地运行每个问题。就像在prob1中,我想用第一个名字运行它,然后再用第二个名字运行,我该怎么做?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-10 23:55:27

是的,使用antlib是可能的,它提供了一系列的功能,比如for循环,if条件和宏。您可以像这样定义宏

代码语言:javascript
复制
<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>

然后你可以这样叫它

代码语言:javascript
复制
<call-cc target="unittests" param1="bla" param2="blabla"/>

您可以在the manual中阅读有关antlib的内容

票数 2
EN

Stack Overflow用户

发布于 2012-11-11 20:25:07

正如@Moataz和@jheddings所解释的那样,ANT macrodef是抽象这些重复性和参数化操作的最佳方式。

下面的工作示例还演示了对构建的一些进一步改进,例如使用属性来抽象项目文件的位置,以及使用ANT路径来控制运行时类路径。

代码语言:javascript
复制
<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>

最后,当您构建了一个在所有构建中使用的常用macodef存储库时,您可以考虑将它们打包为ANTlib。在我看来,在ANT中共享构建逻辑的最好方法。对于这个简单的例子来说,这将是过度杀伤力。

票数 1
EN

Stack Overflow用户

发布于 2012-11-11 00:07:10

您可以使用antcallmacrodef执行此操作,如下所示:

代码语言:javascript
复制
<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>

使用antcall的优点是您可以从命令行执行调用,如下所示:

代码语言:javascript
复制
ant -Dprob-class=prob3 -Dprob-args=2000 run-prob

使用macrodef的优点是,它在ant文件中看起来更干净、更明显。当然,您也可以混合使用这两种方法,并从antcall目标调用宏,反之亦然。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13323081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档