On 2015-02-11, Jackie Xiao wrote:

> Is it possible for Ant to read build files from memory?

Yes, but it takes some effort.  The entry point for this would be a
custom ProjectHelper implementation[1].

Note that there are several parts in Ant that assume there is some sort
of File that defines the build - or at least a "base directory" so
relative paths can get resolved.  This means you will still need to set
the basedir of the Project instance you create.

My first instinct was to subclass Ant's own ProjectHelper2[2] but it
isn't really subclass friendly.  You'd need to override the three arg
parse method to set the InputSource from a stream reading from memory
(or a CipherInputStream wrapping the FileInputStream ;-).

Unfortunately the machinery of ProjectHelper2 relies on AntXMLContext
knowing the buildfile and you don't get hold of the context inside the
three arg parse method as there is no accessor for it in RootHandler.
There are ways that require knowledge of ProjectHelper2's internals.

One thing making things even more complex is that Ant itself loads some
antlibs when starting and uses the same ProjectHelper - these should be
read from the "real" URLs.

I ended up with a simple proof of concept, but wouldn't want to use
something like this in production.  You may want to open an enhancement
request for making ProjectHelper2 more sub-class friendly for your real
use case (wrapping the input stream).

Stefan

[1] http://ant.apache.org/manual/projecthelper.html

[2] 
https://git-wip-us.apache.org/repos/asf?p=ant.git;a=blob;f=src/main/org/apache/tools/ant/helper/ProjectHelper2.java;h=67e1decb9f01d7496a11b0745359d61b5c2694b8;hb=HEAD

[3] https://gist.github.com/bodewig/7909d63fc887b908a56a

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@ant.apache.org
For additional commands, e-mail: user-h...@ant.apache.org

Reply via email to