You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by "Mikael Petterson (KI/EAB)" <mi...@ericsson.com> on 2005/10/18 12:49:48 UTC

set property values using my own task

Hi,

The purose of my task is to set the values for properties arch, product_number and product_revision.
The information is stored in a file called 'product.attributes'. I need to assign the properties the values ( line two)
in the product.attributes file and then use the property values in target, prepare-product-info. Is it possible ?
Any suggestions?

cheers,

//mikael


This is the content of product.attributes:

TARGET:PRODUCT_NUMBER:PRODUCT_REVISION
jvm:CXC1327714/22:R1A01
default:BAD_TARGET:R1A01


build.xml
=======

<project ........>
......
<property name="arch"                         value="jvm"/>
<property name="product_number"         value="BAD000"/>
<property name="product_revision"         value="P0"/>

.........

  <target name="product.attributes">
   
   <taskdef name="propertyread" 
	    classname="se.mycompany.PropertyReadTask"/>
	    <propertyread prefix="prod" file="product.attributes" delimiter=":"/>
   <echoproperties prefix="prod"/>
  </target>

 <target name="prepare-product-info" 
	depends="propertyread"
	description="Prepare product information.">
  <echo>Product name   : ${name}</echo>
  <echo>Product number : ${product_number}</echo>
  <echo>Product rev    : ${product_revision}</echo>
  <exec executable="cleartool"
        outputproperty="view">
    <arg line="pwv -short"/>
  </exec>
  <tstamp>
    <format property="datetime"
            pattern="yyyy-MM-dd HH:mm:ss z"/>
  </tstamp>

  <property name="product.attrs.line1"
            value="${user.dir} ${name} ${arch} ${view}"/>
  <property name="product.attrs.line2"
            value="${product_number} ${product_revision} ${datetime} ${identifier}"/>
</target>

</project>

PropertyReadTask file
================
public class PropertyReadTask extends org.apache.tools.ant.Task{
    
    private String prefix;
    private String file;
    private String delimiter;
    
    public void execute() throws BuildException {
      if (prefix==null || file==null) {
            throw new BuildException("prefix and file must be set");
        }
        try{
        // read the file
        Reader rdr = new java.io.FileReader(file);
        String content = FileUtils.readFully(rdr);
         // split into metadata and content data
        String lines[] = content.split( getProject().getProperty("line.separator") );
        String h1eader = lines[0];
        String body = lines[1];

        // get the metadata
        String metaData[] = header.split(delimiter);

        // get the content data and save as Ant properties
        String contentData[] = body.split(delimiter);
        for (int i=0; i<contentData.length; i++) {
            getProject().setNewProperty(prefix + "." + metaData[i], contentData[i]);
        }
        
        }catch(Exception e){
            throw new BuildException("Something happened");
        }
        
    }
    
    
    public void setDelimiter(String delimiter){
        this.delimiter = delimiter;
    }
    
    public void setFile(String file){
        this.file = file;
    }
    
    public void setPrefix(String prefix){
         this.prefix = prefix;
    }
    

    
}


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


Re: set property values using my own task

Posted by Juergen Hermann <jh...@web.de>.
On Tue, 18 Oct 2005 12:49:48 +0200, Mikael Petterson (KI/EAB) wrote:

>The purose of my task is to set the values for properties arch, 
product_number and product_revision.

Hmmm, why do you need a custom task for this?

        <loadproperties srcFile="product.attributes">
            <filterchain>
                <tokenfilter>
                    <filetokenizer/>
                    <replaceregex 
                        pattern="^([^:]+):([^:]+):([^\r\n]+)[\n\r]+([^:]+):
([^:]+):([^\r\n]+)[\n\r]+(.*)$"
                        replace="\1=\4&#10;\2=\5&#10;\3=\6"/>
                </tokenfilter>
                <prefixlines prefix="prod."/>
            </filterchain>
        </loadproperties>

		 <!-- Set default here if you need them! -->

        <echoproperties prefix="prod."/>

yields

[echoproperties] prod.PRODUCT_NUMBER=CXC1327714/22
[echoproperties] prod.TARGET=jvm
[echoproperties] prod.PRODUCT_REVISION=R1A01

Ciao, Jürgen



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