northmantif
northmantif

Reputation: 27

passing xml attributes to ant build.xml file

I use Ant to build my swf output file. I have got in project directory (along with project src/*.as files) build.xml, package.config and directory.properties files. The package.config file is created outside by Nuget, so I need to rely on values in it:

<packages>
    <package id="Lib1" version="1.0" />
    <package id="Lib2" version="1.0" />
</packages>

now in build.xml I'm adding file:

<property file="package.config" /> 

then I need to include libraries (compiler.include-libraries) containing in their names package.id and package.version i.e.:

<include name="package.id" + "package.version" + ".swc" />

Unfortunately I can not do it this way. I should probably store packages attributes somewhere and then use it passing to name property in tag. Thanks in advance for any clue.

Upvotes: 0

Views: 363

Answers (2)

David
David

Reputation: 2702

You probably want to have the package attributes in a .properties file. This will make your life easier with Ant, and won't require extra tools like XMLtask to scoop them out of the XML document you mentioned. Take a good read through Ant's documentation about how properties work. Properties get expanded from references that look like ${this}, and you don't need to concatenate things with the + sign like you're trying to do.

You'll end up writing something that looks more like this:

<include name="${package.id}${package.version}.swc"/>

Upvotes: 0

Rebse
Rebse

Reputation: 10377

One possible solution would be using xmlproperty task combined with script task using builtin (since Java 1.6.0_06) javascript engine.

Given xmlfile from Nuget :

<packages>
 <package id="Foo" version="1.0"/>
 <package id="Bar" version="1.0"/>
</packages>

Ant snippet using given xmlfile :

<project>

<xmlproperty file="yournugetfile.xml" collapseAttributes="true"/>

<!-- show properties from xmlpropertyfile -->
<echoproperties prefix="packages"/>

<!-- produce includes from those package properties -->
<script language="javascript">
<![CDATA[
 var pids = project.getProperty('packages.package.id').split(',');
 var pvers = project.getProperty('packages.package.version').split(',');
 var incl = "";
 for (i=0; i < pids.length; i++) {
  incl += pids[i] + pvers[i] + '.swc' + ',';
 }
 project.setProperty('cincludes', incl.substring(0, incl.length - 1));
]]>
</script>

<echo>$${cincludes} => ${cincludes}</echo>

<!-- demonstrate possible use -->
<echoxml>
 <compiler.library-path dir="some/path" append="true">
  <include name="${cincludes}"/>
 </compiler.library-path>
</echoxml>

</project>

output :

[echoproperties] #Ant properties
[echoproperties] #Tue May 20 16:37:54 CEST 2014
[echoproperties] packages.package=,
[echoproperties] packages.package.id=Foo,Bar
[echoproperties] packages.package.version=1.0,1.0
[echo] ${cincludes} => Foo1.0.swc,Bar1.0.swc
<?xml version="1.0" encoding="UTF-8"?>
<compiler.library-path append="true" dir="some/path">
  <include name="Foo1.0.swc,Bar1.0.swc" />
</compiler.library-path>

Upvotes: 2

Related Questions