]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/ant/AntRunner.java
TreeTestResult persistence independent from TreeSPath and registry (at last!)
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / ant / AntRunner.java
1 package org.argeo.slc.ant;
2
3 import java.io.File;
4 import java.net.URL;
5 import java.util.Arrays;
6 import java.util.Collections;
7 import java.util.Map;
8 import java.util.Properties;
9 import java.util.Vector;
10
11 import org.springframework.context.ApplicationContext;
12
13 import org.apache.tools.ant.Project;
14 import org.apache.tools.ant.ProjectHelper;
15
16 public class AntRunner {
17 private ApplicationContext context;
18 private ProjectHelper projectHelper;
19 private URL buildFile;
20 private String[] targets;
21 private Properties properties;
22
23 public AntRunner() {
24
25 }
26
27 public AntRunner(ApplicationContext context, ProjectHelper projectHelper,
28 URL buildFile, String[] targets) {
29 super();
30 this.context = context;
31 this.projectHelper = projectHelper;
32 this.buildFile = buildFile;
33 this.targets = targets;
34 }
35
36 public AntRunner(ApplicationContext context, URL buildFile, String target) {
37 super();
38 this.context = context;
39
40 BasicSlcProjectHelper basicSlcProjectHelper = new BasicSlcProjectHelper();
41 this.projectHelper = basicSlcProjectHelper;
42 basicSlcProjectHelper.setContext(context);
43
44 this.buildFile = buildFile;
45 this.targets = new String[] { target };
46 }
47
48 public void run() {
49 Project p = new Project();
50
51 String path = buildFile.getFile();
52 p.setUserProperty("ant.file", path);
53 p.setBaseDir(extractBaseDir(path));
54
55 p.init();
56 p.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, projectHelper);
57 projectHelper.parse(p, buildFile);
58
59 if (properties != null) {
60 for (Map.Entry<Object, Object> entry : properties.entrySet()) {
61 p.setUserProperty(entry.getKey().toString(), entry.getValue()
62 .toString());
63 }
64 }
65
66 p.fireBuildStarted();
67 Throwable exception = null;
68 try {
69 if (targets == null) {
70 p.executeTarget(p.getDefaultTarget());
71 } else {
72 p.executeTargets(new Vector<String>(Arrays.asList(targets)));
73 }
74 } catch (Throwable e) {
75 exception = e;
76 throw new SlcAntException("Could not run Ant script " + buildFile,
77 e);
78 } finally {
79 p.fireBuildFinished(exception);
80 }
81
82 }
83
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) {
89 baseDir = "/";
90 } else {
91 baseDir = path.substring(0, indx) + "/";
92 }
93 } else {
94 baseDir = "/";
95 }
96 File file = new File(baseDir);
97 if (file.exists()) {
98 return file;
99 } else {
100 return new File(System.getProperty("user.dir"));
101 }
102 }
103
104 public static void main(String[] args) {
105 // TODO Auto-generated method stub
106
107 }
108
109 }