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
.springframework
.context
.ApplicationContext
;
14 public class AntRunner
{
15 private ApplicationContext context
;
16 private ProjectHelper projectHelper
;
17 private URL buildFile
;
18 private String
[] targets
;
19 private Properties properties
;
25 public AntRunner(ApplicationContext context
, ProjectHelper projectHelper
,
26 URL buildFile
, String
[] targets
) {
28 this.context
= context
;
29 this.projectHelper
= projectHelper
;
30 this.buildFile
= buildFile
;
31 this.targets
= targets
;
34 public AntRunner(ApplicationContext context
, URL buildFile
, String target
) {
36 this.context
= context
;
38 BasicSlcProjectHelper basicSlcProjectHelper
= new BasicSlcProjectHelper();
39 this.projectHelper
= basicSlcProjectHelper
;
40 basicSlcProjectHelper
.setContext(context
);
42 this.buildFile
= buildFile
;
43 this.targets
= new String
[] { target
};
47 Project p
= new Project();
49 String path
= buildFile
.getFile();
50 p
.setUserProperty("ant.file", path
);
51 p
.setBaseDir(extractBaseDir(path
));
54 p
.addReference(ProjectHelper
.PROJECTHELPER_REFERENCE
, projectHelper
);
55 projectHelper
.parse(p
, buildFile
);
57 if (properties
!= null) {
58 for (Map
.Entry
<Object
, Object
> entry
: properties
.entrySet()) {
59 p
.setUserProperty(entry
.getKey().toString(), entry
.getValue()
65 Throwable exception
= null;
67 if (targets
== null) {
68 p
.executeTarget(p
.getDefaultTarget());
70 p
.executeTargets(new Vector
<String
>(Arrays
.asList(targets
)));
72 } catch (Throwable e
) {
74 throw new SlcAntException("Could not run Ant script " + buildFile
,
77 p
.fireBuildFinished(exception
);
82 private File
extractBaseDir(String path
) {
83 String baseDir
= null;
84 if (path
.length() > 1) {
85 int indx
= path
.lastIndexOf('/', path
.length() - 1);
86 if (indx
== -1 || indx
== 0) {
89 baseDir
= path
.substring(0, indx
) + "/";
94 File file
= new File(baseDir
);
98 return new File(System
.getProperty("user.dir"));
102 public static void main(String
[] args
) {
103 // TODO Auto-generated method stub