Reputation: 55
i have the same source code for 5 projects and i want to build each project with the same build file in ANT. i want to build the code using the name of each project.
Upvotes: 4
Views: 1146
Reputation: 145
I suggest that you define targets in your build.xml file called "DEV" and "PROD" and then invoke Ant as:
ant DEV or
ant PROD If you want to stick with your current approach of using a system property to select the target, then @krock's answer seems to be the way to go. (But I don't see any advantage in that approach though.)
Upvotes: 1
Reputation: 10377
Define a userproperty with your projectname like that :
ant -f build.xml -Dprojectname=foobar
and use it in your buildfile :
<project>
<echo>$${projectname} => ${projectname}</echo>
<!-- the real work ... -->
</project>
output :
[echo] ${projectname} => foobar
-- EDIT --
You may use some kind of loop, f.e. :
ant -f build.xml -Dprojectnames=project1,project2,project3
wrap your steps (checkout, compile ...) in a macrodef and call it for every project :
<project>
<macrodef name="foobar">
<attribute name="projectname"/>
<sequential>
<echo>Projectname : @{projectname}</echo>
<!-- the real work -->
</sequential>
</macrodef>
<script language="javascript">
<![CDATA[
var projects = project.getProperty('projectnames').split(',');
for (i=0; i < projects.length; i++) {
macro = project.createTask('foobar');
macro.setDynamicAttribute('projectname', projects[i]);
macro.perform();
}
]]>
</script>
</project>
output :
[echo] Projectname : project1
[echo] Projectname : project2
[echo] Projectname : project3
You may also create ant targets instead of macrodef.
Javascript engine is contained in Java >= 1.6.0_06, so no extra libraries needed.
Also there are more sophisticated approaches, f.e. using a xmlproperty file holding your projectnames combined with xmltask (= xmldriven build) which provides some looping mechanism via xpath expressions.
It depends upon the complexity of your buildscript.
Upvotes: 0