问题
Is it possible to access resources from within Apache Ant macros defined in an Antlib?
For instance, within my antlib.xml
, I have a macro that performs some XSLT. Ideally I would like the XSLT file to be packaged in the same JAR as the antlib.xml
, but I have no idea how to specify the location of the XSLT.
Here is the (simplified) code:
<antlib xmlns:tibant="antlib:org.windyroad.tibant">
<macrodef name="configure-ear">
<attribute name="xml" />
<attribute name="out" />
<sequential>
<xslt in="@{xml}"
out="@{out}"
style="...what to put here...">
</xslt>
</sequential>
</macrodef>
</antlib>
The problem is that whatever I put in the style
attribute is relative to the basedir
for the project using the antlib and I can't find any way to specify a path relative to the antlib.xml.
Any ideas?
I can ship the XSLT as a separate file, but then I would need to give users some way to specify the location of the XSLT, which is not ideal (e.g. setting a tibant.home
property). I could also use echoxml
to write out the XSLT to a temp file, but IMO that's a hack.
回答1:
Instead of using the style
attribute, try a nested <style>
element, which will allow you to specify a javaresource as the style sheet. You can then put the stylesheet next to your antlib.xml in the jar, and it will be available on the classpath.
<xslt in="@{xml}"
out="@{out}">
<style>
<javaresource name="your/package/structure/style.xslt" />
</style>
</xslt>
回答2:
The first thing I would look at is to load XSL from the classloader as a resource. You should be able to accomplish this with LoadResource task (http://ant.apache.org/manual/Tasks/loadresource.html). The next I would look at options that XSLT task gives you for the specifying style. It doesn't look like it has any ability to take literal contents of XSLT. You can work around this by writing out the XSLT content to a temporary file and then giving the path to the temp file to the XSLT task.
So...
- Load XSLT text from the classloader.
- Acquire a temporary file using Tempfile task (http://ant.apache.org/manual/Tasks/tempfile.html).
- Write out XSLT text to the temp file using Echo task.
- Invoke XSLT with reference to the temp file.
来源:https://stackoverflow.com/questions/5159858/access-antlib-resources-from-within-apache-ant-macros