Ant: How to check if a target exists (and not name it if not)?

I have a set of assembly files, some of which are causing others to import them first. The end of lines may or may not have a specific purpose (for example, "copyother"). I want to call this from my main build file if this goal is defined at the end of the build script line. How can i do this?

Part of the calling script:

<!-- Import project-specific libraries and classpath -->
<property name="build.dir" value="${projectDir}/build"/>
<import file="${build.dir}/build_libs.xml"/>

...

<!-- "copyother" is a foreign target, imported in build_libs.xml per project -->
<target name="pre-package" depends="    clean,
                                        init,
                                        compile-src,
                                        copy-src-resources,
                                        copy-app-resources,
                                        copyother,
                                        compile-tests,
                                        run-junit-tests"/>

I do not want each project to define a copyother goal. How can I make a conditional call to ant?

+5
source share
3 answers

, "" build.xml. ( . Ant .)

, ant/ant, , , , .

, , native Ant. , , . , . Ant, , . :

1) project.getTargets() , . script ant/antcall , . Java , Ant .

2) , . -op. [ , ]

+3

. - .

: http://ant.1045680.n5.nabble.com/Checking-if-a-Target-Exists-td4960861.html ( vimil). scriptdef. (Jeanne Boyarsky), script .

<scriptdef name="hastarget" language="javascript">
    <attribute name="targetname"/>
    <attribute name="property"/>
    <![CDATA[
       var targetname = attributes.get("property");
       if(project.getTargets().containsKey(targetname)) {
            project.setProperty(attributes.get("property"), "true");
       }
     ]]>
</scriptdef>

<target name="check-and-call-exports">
    <hastarget targetname="exports" property="is-export-defined"/>
    <if>
        <isset property="is-export-defined"/>
        <then>
            <antcall target="exports"   if="is-export-defined"/>
        </then>
    </if>
</target>

<target name="target-that-may-run-exports-if-available" depends="check-and-call-exports">
+1

typefound, ANT 1.7. , , if antcontrib, , , taskdef - , :

<if>
   <typefound name="some-macrodef"/>
<then>
   <some-macrodef/>
   </then>
</if>

In this case, ANT files that have a macro definition with the name "some-macro-orde taskdef" will receive it, and other ANT files without it will not receive an error.

0
source

All Articles