1 package org
.argeo
.slc
.ant
;
5 import java
.util
.Arrays
;
6 import java
.util
.Collections
;
8 import java
.util
.Properties
;
9 import java
.util
.Vector
;
11 import org
.springframework
.context
.ApplicationContext
;
13 import org
.apache
.tools
.ant
.Project
;
14 import org
.apache
.tools
.ant
.ProjectHelper
;
16 public class AntRunner
{
17 private ApplicationContext context
;
18 private ProjectHelper projectHelper
;
19 private URL buildFile
;
20 private String
[] targets
;
21 private Properties properties
;
27 public AntRunner(ApplicationContext context
, ProjectHelper projectHelper
,
28 URL buildFile
, String
[] targets
) {
30 this.context
= context
;
31 this.projectHelper
= projectHelper
;
32 this.buildFile
= buildFile
;
33 this.targets
= targets
;
36 public AntRunner(ApplicationContext context
, URL buildFile
, String target
) {
38 this.context
= context
;
40 BasicSlcProjectHelper basicSlcProjectHelper
= new BasicSlcProjectHelper();
41 this.projectHelper
= basicSlcProjectHelper
;
42 basicSlcProjectHelper
.setContext(context
);
44 this.buildFile
= buildFile
;
45 this.targets
= new String
[] { target
};
49 Project p
= new Project();
51 String path
= buildFile
.getFile();
52 p
.setUserProperty("ant.file", path
);
53 p
.setBaseDir(extractBaseDir(path
));
56 p
.addReference(ProjectHelper
.PROJECTHELPER_REFERENCE
, projectHelper
);
57 projectHelper
.parse(p
, buildFile
);
59 if (properties
!= null) {
60 for (Map
.Entry
<Object
, Object
> entry
: properties
.entrySet()) {
61 p
.setUserProperty(entry
.getKey().toString(), entry
.getValue()
67 Throwable exception
= null;
69 if (targets
== null) {
70 p
.executeTarget(p
.getDefaultTarget());
72 p
.executeTargets(new Vector
<String
>(Arrays
.asList(targets
)));
74 } catch (Throwable e
) {
76 throw new SlcAntException("Could not run Ant script " + buildFile
,
79 p
.fireBuildFinished(exception
);
84 private File
extractBaseDir(String path
) {
85 String baseDir
= null;
86 if (path
.length() > 1) {
87 int indx
= path
.lastIndexOf('/', path
.length() - 1);
88 if (indx
== -1 || indx
== 0) {
91 baseDir
= path
.substring(0, indx
) + "/";
96 File file
= new File(baseDir
);
100 return new File(System
.getProperty("user.dir"));
104 public static void main(String
[] args
) {
105 // TODO Auto-generated method stub