ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mitch Gitman <mgit...@gmail.com>
Subject controlling ClassLoader when programmatically invoking Ant
Date Sat, 21 Jul 2012 05:37:35 GMT
Technically, this message is better suited for the ant-user list, but I'm
thinking I'm more apt to get an answer on this list. (I'm also thinking
this is the better place for me to cash in some chits for my having
submitted patches for three Ivy issues I mentioned recently on this list.
Subject: "extends and buildlist on 2.3.0-rc1....")

Here's my problem. I'm trying to do a JUnit test of some Ivy functionality
(actually, relating to the aforementioned Ivy bugs) by programmatically
creating and executing an Ant Project object. Everything runs just fine.
I'm able to execute targets and even tie in a BuildListener and a
BuildLogger to tap into output and error and check if the build failed.

The problem is, everything works a little too well. Take a look at the
following example build.xml:
<project xmlns:ivy="antlib:org.apache.ivy.ant" name="resolve-test"
default="build">

    <target name="build">
    <ivy:settings file="ivysettings.xml" />
    <ivy:resolve ... />
    </target>
</project>

I've abbreviated the ivy:resolve call, but take it from me that everything
works, including running the test directly from within an IDE. It
shouldn't. Nowhere am I specifying the classpath containing the JAR for Ivy
itself. Nowhere am I even doing the taskdef for Ivy.

And in fact, if I run this build.xml straight from the command line, it
fails like you would expect:
Problem: failed to create task or type antlib:org.apache.ivy.ant:settings
...

I can only think that the key method I want to take advantage of is this in
Project:
    public void setCoreLoader(ClassLoader coreLoader) {
        this.coreLoader = coreLoader;
    }

What's interesting is that the command-line entry point for Ant in
org.apache.tools.ant.Main ends up passing null to setCoreLoader, in which
case "the parent classloader should be used," according to the method's
Javadoc. At this point, I go take a look at the scripts that launch Ant and
my eyes get blurry. But I believe what I want to do is manually construct a
ClassLoader as if I were running "java -cp ..." with the contents of Ant's
lib directory. (I'm showing my ignorance of ClassLoaders here, I realize.)
Perhaps someone can point me to an elegant way to accomplish that.

If someone's going to say, "Hey, check what antunit does," I'd appreciate
if you could pinpoint what it's doing.

P.S. Perhaps somebody has a rational explanation too for why the taskdef of
Ivy is creeping in.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message