ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Naresh Bhatia" <NBha...@sapient.com>
Subject RE: Deploying to multiple servers
Date Sat, 08 Mar 2003 18:45:11 GMT
Mike,

Thank you so much for the example. It makes the approach much more
clearer.

1) You were very close on the syntax for grabbing the value attribute -
only a "/" was needed:
     <xsl:value-of select='property[@name="target-dir"]/@value'/>

2) I put indent="yes" in my style sheet and the resulting output is very
readable:
     <xsl:output method="xml" indent="yes"/>

Thanks again.
Naresh

-----Original Message-----
From: Mike McNally [mailto:m5@works.com] 
Sent: Saturday, March 08, 2003 11:14 AM
To: Naresh Bhatia
Cc: Ant Users List
Subject: Re: Deploying to multiple servers


> I do need a little XSLT help with this solution. I am trying to 
> produce the following property in the output build file:
> 
>     <property name="server-name" value="server1"/>
> 
> Here's is roughly the XSL I want - the value attribute coming from my 
> input file. Obviously this is not valid XML, but I don't know how to 
> fix
> it:
> 
>     <property name="server-name" value="<xsl:value-of 
> select="@name"/>"/>
> 
> Another approach that did work for me is shown below, but this no 
> longer is a "template" and is not readable as a build file:
> 
>     <xsl:element name="property">
>       <xsl:attribute name="name">node-name</xsl:attribute>
>       <xsl:attribute name="value"><xsl:value-of 
> select="@name"/></xsl:attribute>
>     </xsl:element>
> 
> Any suggestions?

Sure.

Your "auxilliary" XML could look something like this:

<servers>
  <server name='server1'>
    <property name='target-dir' value='/usr/local'/>
    <property name='copy-mode' value='overwrite'/>
  </server>

  <server name='server2'>
    <property name='target-dir' value='/var/deploy'/>
    <property name='copy-mode' value='preserve'/>
  </server>

  <!-- and so on -->
</servers>

(You could write a little DTD to match that, though I don't think that
the <xslt> task validates when it parses; I could be wrong.)


You'd then have an XSL stylesheet that looks like this:

<xsl:template match='/servers'>
  <project>
    <xsl:attribute name='name'>server-install</xsl:attribute>
    <xsl:attribute name='default'>install</xsl:attribute>
    <xsl:attribute name='basedir'>.</xsl:attribute>

    <target>
      <xsl:attribute name='name'>install</xsl:attribute>
      <xsl:attribute name='depends'>
        <xsl:for-each select='server'>
          <xsl:if test='position() > 1'>
            ,
          </xsl:if>
          <xsl:value-of select='concat("install.", @name)'/>
        </xsl:for-each>
      </xsl:attribute>
    </target>

    <xsl:for-each select='server'>
      <target>
        <xsl:attribute name='name'>
          <xsl:value-of select='concat("install.", @name)'/>
        </xsl:attribute>

        <your-install-task-goes-here>
          <xsl:attribute name='sample'>
            <xsl:value-of select='property[@name="target-dir"]@value'/>
          </xsl:attribute>
        </your-install-task-goes-here>
      </target>
    </xsl:for-each>
  </project>
</xsl:template>


(I may be a bit off in getting the "value" attribute from the nested
"property" tag; it's something like that.)  Finally you use an <xslt>
rule to translate your auxilliary XML file into the Ant build file, 
which you then invoke with <ant>.

The generated build file will be hard to read, but you can use an XML
viewer to debug problems with your XSL translation.  I find this
technique to be very useful, as it fills the role of the macro languages
supported by "make" and its variants.  My entire build 
structure is based on simplified files that call out the things to be
build and the conceptual "build sub-process", and then my stylesheet
embodies the canonical ant sequences to achieve each of the
sub-processes.

--
[ you are my main foo ] Mike McNally -- m5@works.com

Mime
View raw message