I had written some time ago a description of rules and pitfalls in using <import>.


It's some time since I used it now, so I don't know if it still applies as-is. IMHO it could be a nice addition to the Ant manual.

In any case, here it is.

                       -o-

"
 Each <import> will insert the imported file in the importing file.
 If there are name clashes, the importing file targets take precedence,
 and those imported targets is available as importedfilename.targetname.
 If there are multiple copies from the same target, the last definition
 takes precedence. Note that the target that takes precedence will be
 used in all dependencies, regardless to which buildfile they came from.
"

  Warning:
        The import task is not made to reuse current builfiles as
        building blocks, as it does not provide a way of shielding
        the files between themselves from side-effects of name
        clashes. Be sure to check the buildfile you import so
        that it does not have unintended sideeffects or needs special
        resources.

  Note: Multiple Inheritance Issues
        If you import two files that have a same target, and
        that target is not in your base file, the target used
        by both will be the last one to be declared.
        In other words, the last target definition with the same
        name is the one that will be used by *all* files.

  Suggestion:
        To prevent this, use fully qualified target names, or
        remember to redefine the target in your base buildfile
        in the manner you see fit.

  Example:

File a.xml

        <project name="first">
           <target name="a" depends="init">
              <echo value="inita">
           </target>
           <target name="init">
              <echo value="initb">
           </target>
        </project>

File b.xml

        <project name="second">
           <target name="b" depends="init">
              <echo value="b">
           </target>
           <target name="init">
              <echo value="inita">
           </target>
        </project>

file build.xml

        <project name="main" default="run">
           <import file="a.xml" />
           <import file="b.xml" />
           <target name="run" depends="a,b"/>
        </project>

Running build.xml will yield this sequence during import:

    import a.xml
       added target a
       added target first.a
       added target init
       added target first.init

[targets: run, a, first.a, init(==first.init), first.init ]

    import b.xml
       added target b
       added target second.b
   --> redefine target init with second.init <--
       added target second.init

[targets: run, a, first.a, first.init
               b, second.a, init(==second.init), second.init ]

Since dependencies are not redefined, the init target that the global buildfile will use is second.init.

To prevent this:

Filw a.xml

        <project name="first">
           <target name="a" depends="init">
              <echo value="inita"></target>
           <target name="my.organization.namespace.a.init">
              <echo value="inita"></target>
        </project>

File b.xml

        <project name="second">
           <target name="b" depends="init">
              <echo value="b"></target>
           <target name="my.organization.namespace.b.init">
              <echo value="initb"></target>
        </project>

file build.xml

        <project name="main" default="run">
           <import file="a.xml" />
           <import file="b.xml" />
           <target name="run" depends="a,b"/>
        </project>

Running build.xml will yield this sequence:

    import a.xml
       added target a
       added target first.a
       added target my.organization.namespace.a.init
       added target first.my.organization.namespace.a.init

[targets: run, a, first.a,
          my.organization.namespace.a.init,
          first.my.organization.namespace.a.init ]

    import b.xml
       added target b
       added target second.b
       added target my.organization.namespace.a.init
       added target first.my.organization.namespace.a.init

[targets: run, a, first.a,
          my.organization.namespace.a.init,
          first.my.organization.namespace.a.init
          b, second.b,
          my.organization.namespace.b.init,
          second.my.organization.namespace.b.init ]


-- Nicola Ken Barozzi [EMAIL PROTECTED] - verba volant, scripta manent - (discussions get forgotten, just code remains) ---------------------------------------------------------------------


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to