1 package org
.argeo
.slc
.ant
;
5 import java
.util
.Arrays
;
7 import java
.util
.Properties
;
8 import java
.util
.Vector
;
10 import org
.apache
.tools
.ant
.Project
;
11 import org
.apache
.tools
.ant
.ProjectHelper
;
12 import org
.apache
.tools
.ant
.helper
.ProjectHelper2
;
13 import org
.argeo
.slc
.SlcException
;
15 /** Run regular Ant script (that is, not SLC instrumented) */
16 public class AntRunner
{
17 private URL buildFile
;
18 private String
[] targets
;
19 private Properties properties
;
25 public AntRunner(URL buildFile
, String target
, Properties properties
) {
26 this(buildFile
, new String
[] { target
}, properties
);
29 public AntRunner(URL buildFile
, String
[] targets
, Properties properties
) {
30 this.buildFile
= buildFile
;
31 this.targets
= targets
;
32 this.properties
= properties
;
36 Project p
= new Project();
38 String path
= buildFile
.getFile();
39 p
.setUserProperty("ant.file", path
);
40 p
.setBaseDir(extractBaseDir(path
));
43 ProjectHelper projectHelper
= new ProjectHelper2();
44 p
.addReference(ProjectHelper
.PROJECTHELPER_REFERENCE
, projectHelper
);
45 projectHelper
.parse(p
, buildFile
);
47 if (properties
!= null) {
48 for (Map
.Entry
<Object
, Object
> entry
: properties
.entrySet()) {
49 p
.setUserProperty(entry
.getKey().toString(), entry
.getValue()
55 Throwable exception
= null;
57 if (targets
== null) {
58 p
.executeTarget(p
.getDefaultTarget());
60 p
.executeTargets(new Vector
<String
>(Arrays
.asList(targets
)));
62 } catch (Throwable e
) {
64 throw new SlcException("Could not run Ant script " + buildFile
, e
);
66 p
.fireBuildFinished(exception
);
71 private File
extractBaseDir(String path
) {
72 String baseDir
= null;
73 if (path
.length() > 1) {
74 int indx
= path
.lastIndexOf('/', path
.length() - 1);
75 if (indx
== -1 || indx
== 0) {
78 baseDir
= path
.substring(0, indx
) + "/";
83 File file
= new File(baseDir
);
87 return new File(System
.getProperty("user.dir"));