在Ant中在运行时创建具有动态内容的Union和Macrodef样式元素

sea*_*ers 5 ant union element macrodef

我有一个用Ant构建的构建脚本,它有一个macrodef,它接受一些默认参数,target,root等,然后是一个可选的两个,extrasrc-f和extrasrc-c.在他们进来之后,我喜欢对所有相关资源进行最新检查,然后只在目标过期时才进行构建.

我现在拥有的是什么

<?xml version="1.0" encoding="UTF-8"?>
<project name="Custom build" default="default">

    <taskdef resource="net/sf/antcontrib/antlib.xml"
        classpath="C:/dev/ant/ant-contrib/ant-contrib-1.0b3.jar"/>

    <macrodef name="checkuptodate">
        <attribute name="target" />
        <element name="resource" />
        <sequential>
            <condition property="needbuild">
                <and>
                    <resourcecount when="greater" count="0"> <resource /> </resourcecount>
                    <not>
                        <uptodate targetfile="@{target}">
                            <srcresources> <resource /> </srcresources>
                        </uptodate>
                    </not>
                </and>
            </condition>
        </sequential>
    </macrodef>

    <macrodef name="projbuild">
        <attribute name="root" />
        <attribute name="target" />

        <element name="extrasrc-f" optional="true" />
        <element name="extrasrc-c" optional="true" />
        <sequential>
            <local name="needbuild" />
            <checkuptodate target="@{root}/bin/@{target}">
                <resource>
                    <union>
                        <extrasrc-f />
                        <fileset dir="@{root}/src" includes="**/*.java" />
                    </union>
                </resource>
            </checkuptodate>

            <if>
                <istrue value="${needbuild}" />
                <then>
                    <javac
                        srcdir="@{root}/src"
                        destdir="@{root}/bin"
                        includeantruntime="false"
                    >
                        <extrasrc-c />
                    </javac>
                </then>
            </if>

        </sequential>
    </macrodef>

    <target name="default">

        <projbuild root="." target="EntryPoint.class">
            <extrasrc-f>
                <fileset dir="Proj2/src" includes="**/*.java" />
                <fileset dir="Proj3/src" includes="**/*.java" />
            </extrasrc-f>
            <extrasrc-c>
                <classpath location="Proj2/src" />
                <classpath location="Proj3/src" />
            </extrasrc-c>
        </projbuild>

    </target>

</project>
Run Code Online (Sandbox Code Playgroud)

但正如你所看到的,在这个时间点,对我来说效率低,做我想做的事情,我要创建并传入至少一个文件集和多个类路径.我真正想要做的只是传入一个目录列表,然后根据这些信息动态创建extrasrc-f和extrasrc-c元素,但对于我的生活,我不知道我是怎么做的我能够做到这一点.

我已经阅读了很多关于Ant和Ant-Contrib时髦课程的内容,但是我没有读过任何可以让我做这样的事情,我觉得很奇怪,因为对我来说这看起来很明显.

我是以非常错误的方式接近这个,还是我缺少一些东西?如果我真的误用Ant,我会喜欢指向如何正确执行此操作的指针,在macrodef(或目标,如果这是唯一的方法)中创建一个catchall,模板构建,它测试多个源文件针对一个构建的文件,同时也传递额外的类或库路径,最好是在一个列表中.

mar*_*ton 0

也许您可以使用几个<scriptdef>任务来帮助分解这些宏。

第一种方法采用逗号分隔的目录列表并<union>从中生成 。您提供refid要用来引用联合的属性id。有可选的包含和排除。

<scriptdef name="dirs2union" language="javascript">
    <attribute name="dirs" />
    <attribute name="id" />
    <attribute name="includes" />
    <attribute name="excludes" />
    <![CDATA[
      var dirs = attributes.get( "dirs" ).split( "," );
      var includes = attributes.get( "includes" );
      var excludes = attributes.get( "excludes" );

      var union = project.createDataType( "union" );
      project.addReference( attributes.get( "id" ), union );

      for ( var i = 0; i < dirs.length; i++ ) {
          var fs = project.createDataType( "fileset" );
          fs.setDir( new java.io.File( dirs[i] ) );
          if ( includes )
              fs.setIncludes( includes );
          if ( excludes )
              fs.setExcludes( excludes );

          union.add( fs );
      }
    ]]>
</scriptdef>
Run Code Online (Sandbox Code Playgroud)

第二个 - 非常相似 - 脚本执行与路径生成相同的操作:

<scriptdef name="dirs2path" language="javascript">
    <attribute name="dirs" />
    <attribute name="id" />
    <![CDATA[
      var dirs = attributes.get( "dirs" ).split( "," );

      var path = project.createDataType( "path" );
      project.addReference( attributes.get( "id" ), path );

      for ( var i = 0; i < dirs.length; i++ ) {
          var pe = project.createDataType( "path" );
          pe.setLocation( new java.io.File( dirs[i] ) );
          path.add( pe );
      }
    ]]>
</scriptdef>
Run Code Online (Sandbox Code Playgroud)

一个示例用法可能类似于:

<property name="dirs" value="Proj2/src,Proj3/src" />

<dirs2union dirs="${dirs}" id="my.union" includes="**/*.java" />
<dirs2path  dirs="${dirs}" id="my.path" />

... later (e.g.) ...

<union refid="my.union" />
<classpath refid="my.path" />
Run Code Online (Sandbox Code Playgroud)

然后,您可以修改宏以获取dirs属性并在内部生成联合和类路径,或者可能在其他地方生成一次并仅传递引用。

我没有尝试@{root}在此图中包含目录,但应该可以对此进行调整。