简体   繁体   中英

Reading from .properties file in Ant build.xml

I need some help with using Ant's property files. I have the following:

  1. A build.properties file. This file contains one piece of information: on=1

  2. An ant.xml file. This file contains my build instructions.

I want to read the on attribute from my properties file and if the value is 1 I want to execute a task in my build file. Otherwise I want it to do nothing. Can anyone guide me on how to accomplish this?

This should be all you need to do:

1.Grab the latest version of ant-contrib JAR and place in lib folder of your Ant installation.

2.Include your properties in your build script

<property file="build.properties"/>

3.Add the following taskdef entry to your build script

<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

4.And then finally, define an if task like so:

<if>
 <equals arg1="${on}" arg2="1" />
 <then>
   <echo message="I am going to do something here" />
 </then>
 <else>
   <echo message="I am going to do nothing" />
 </else>
</if>

Note that you can prepend an identifier to properties you import from property files. So for example you could do your import like so:

<property file="build.properties" prefix="uniqueprefix"/>

And then you would reference in your file, 'uniqueprefix.on', instead of just 'on'.

<equals arg1="${uniqueprefix.on}" arg2="1" />

You can use the built-in conditional task from Ant, but I have a feeling that if you need it you are better off with the extra functions that ant-contrib brings to the table. Also, note that its standard to name your build file as ' build.xml ', and not ' ant.xml '. As it is, Ant will not be able to automatically locate it, given the name you have used. Good luck.

Seems to me that you want to implement something like below given task.

<property file="build.properties" />



<target name="default" description="Homeworks">
    <condition property="on">
        <equals arg1="{on}" arg2="1" />
    </condition>
    <antcall target="taska" />
    <antcall target="taskb" />
</target>

<target name="taska" if="on">
    <echo message="Testing task one" />
</target>
<target name="taskb" unless="on">
    <echo message="Testing task two" />
</target>

Let me know if you want a explanation, in details.

If you don't want to write your own Ant Task or use other libs, just "clean" ant, have a look at this one:

mybuild.properties:

on=on

Use on or true or something like that, 1 will not work.

build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="project" default="default">
    <property file="mybuild.properties"/>    

    <target name="default" depends="on, off" description="description">
        <echo>default</echo>
    </target>

    <target name="on" if="${on}">
      <echo>on</echo>
    </target>

    <target name="off" unless="${on}">
      <echo>off</echo>
    </target>
</project>

An approach, that looks difficult but is, in fact, pretty easy: write a custom ant task (one simple java class, <20 lines of code). The task will

  1. read the properties file (location/name can be passed as an attribute to the task)
  2. assign the value of on to an ant property

Then you can use that ant property for your flow control.

public class MyOwnTask extends Task {

  private String filename = "build.properties"; // some default value

  public void setFilename(String filename) {
    this.filename = filename;
  }

  public void execute() {   // the "main" method
    Properties p = new Properties();
    p.load(filename);
    String onValue = p.get("on");
    getProject().setProperty("ON_PROPERTY", onValue);
  }
}

Then you need some <taskdef> and that's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM