javanoob
javanoob

Reputation: 6412

Read property from file and assign to a different name in ant?

I am trying to read a property from properties file and assign to a different name but it is not working. I am new to ant so I guess I am missing something basic.

build.properties:

USERNAME=deter_dangler

build.xml:

<project name="Simple Ant example" default="test" basedir=".">
    <property file="build.properties"/>
    <property environment="env"/>
    <property name="uname" value="${env.USERNAME}"/>
    <target name="test">
        <echo message="uname property value is ${uname}"/>
        <echo message="env.USERNAME property value is ${env.USERNAME}"/>
    </target>
</project>

The output when I run the build command:

javanoob@DELL:~/Desktop$ ant 
Buildfile: /Desktop/build.xml

test:
     [echo] uname property value is ${env.USERNAME}
     [echo] env.USERNAME property value is ${env.USERNAME}

BUILD SUCCESSFUL
Total time: 0 seconds

Upvotes: 0

Views: 536

Answers (2)

Rao
Rao

Reputation: 21389

I believe that it could be simple incorrect environment variable being used i.e., by default USER is available on the linux machine without requiring to be explicitly defined by user and you are using USERNAME. I assume this is what user is expecting as he is trying to print value from property and another one from system defined default.

So, just replacing the above mentioned changes in the build.properties and build.xml files.

build.properties

USER=deter_dangler

build.xml

<project name="Simple Ant example" default="test" basedir=".">
    <property file="build.properties"/>
    <property environment="env"/>
    <property name="uname" value="${USER}"/>
    <target name="test">
        <echo message="uname property value is ${uname}"/>
        <echo message="env.USER property value is ${env.USER}"/>
    </target>
</project>

Output

Buildfile: /home/apps/Documents/so/34553709/build.xml

test:
     [echo] uname property value is deter_dangler
     [echo] env.USER property value is apps

BUILD SUCCESSFUL
Total time: 1 second

In the above, if you notice, ${USER} is taken from property file and ${env.USER} from system logged in user.

Upvotes: 0

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 78011

Trying setting the environment variable as follows:

USERNAME=deter_dangler ant

Alternatively, if you want to use a properties file then simplify your ANT file as follows:

<project name="Simple Ant example" default="test" basedir=".">
    <property file="build.properties"/>

    <target name="test">
        <echo message="uname property value is ${USERNAME}"/>
    </target>
</project>

Upvotes: 1

Related Questions