Brian
Brian

Reputation: 13583

How to pass parameters from gradle to ant?

I have the following two files:

build.gradle:

ant.importBuild 'build.xml'

build.xml:

<project>
    <target name="hello">
        <echo>Hello, I'm ${testVar}</echo>
    </target>
</project>

How can I pass testVar variable from gradle to ant to trigger the ant build?

gradle -PtestVar=John hello outputs

> Task :hello
[ant:echo] Hello, I'm ${testVar}

This means that testVar isn't assigned a value.

I can pass testVar variable to ant directly by using ant -DtestVar=John -buildfile build.xml hello command.

How can I do the same thing using gradle?

Upvotes: 1

Views: 1770

Answers (2)

CAustin
CAustin

Reputation: 4614

-P is used for passing properties directly to your Gradle build. Your Gradle script will see these properties but your Ant script won't. The answer you provided technically works, but could become tedious as you'll have to make sure you're passing every property from Gradle to Ant in your Gradle script.

If you want to provide a property to Ant, use -D instead.

Upvotes: 1

Brian
Brian

Reputation: 13583

I've found that changing build.gradle file to

ant.importBuild 'build.xml'
ant.properties['testVar'] = testVar

solves the problem.

After the modification, gradle -PtestVar=Ken hello generates

> Task :hello
[ant:echo] Hello, I'm Ken

which is the result I want.

Upvotes: 1

Related Questions