Ant vs. tasks

2020-05-30 03:30发布

问题:

I now see Ant has both an <include> task and an <import> task.

According to the descriptions:

Include

Include another build file into the current project.

and

Import

Imports another build file into the current project.

So, why use one over the other?

Here's my actual problem:

In our current build system, we are concatenating a bunch of JavaScripts and then minimizing them. The JavaScripts are located in a dozen different directories, and we are taking batches from each directory and concatenating them into five or six super minimized JavaScripts. Some of these files are copied into multiple super JavaScripts.

In order to make debugging easier, and the build a bit more flexible, I want to copy all of the files into the target/work/resources2 directory with each sub-directory under there representing a different super minimized JavaScript. For debugging purposes, we'll include the non-minimized super JavaScript and the originals. The build script isn't complex, but the whole section is taking up a lot of lines. I was thinking of putting the <copy> stuff into a separate XML file, so the whole thing looks like this:

<target name="process-resources"
     description="Concatenate and minimize the JavaScripts (using Maven lifecycle names for our targets">
     <!-- The following include the copying stuff -->
     <here.be.dragons file="${basedir}/reservations.xml"/>
     <here.be.dragons file="${basedir}/date.xml"/>
     <here.be.dragons file="${basedir}/select.xml"/>

     <for param="concat.dir">
         <fileset dir="${work.dir]/resources2"/>
         <sequential>
            <here.I.am.concatenating.and.minimizing/>
         </sequential>
    </for>
</target>

I see there are four possibilities:

  • Use <ant/> to call the files that do the copying
  • Use <import/> (which might not work because it might not be able to be included inside a target)
  • Use <include/> (which might not work because it might not be able to be included inside a target)
  • Use the Entity Include.

I am never crazy about using <ant/> or <antcall> although this might be a good time to do this. The Entity Include idea will work, but that's something most people don't understand, and I am afraid it will cause confusion for people who have to support what I'm doing. The <import> and <include> may not be able to be used in this situation, but I'm still curious what the differences are.

回答1:

The documentation of import explains the difference:

How is import different from include?

The short version: Use import if you intend to override a target, otherwise use include.

When using import the imported targets are available by up to two names. Their "normal" name without any prefix and potentially with a prefixed name (the value of the as attribute or the imported project's name attribute, if any).

When using include the included targets are only available in the prefixed form.

When using import, the imported target's depends attribute remains unchanged, i.e. it uses "normal" names and allows you to override targets in the dependency list.

When using include, the included targets cannot be overridden and their depends attributes are rewritten so that prefixed names are used. This allows writers of the included file to control which target is invoked as part of the dependencies.

It is possible to include the same file more than once by using different prefixes, it is not possible to import the same file more than once.