2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.ant
;
20 import java
.util
.ArrayList
;
21 import java
.util
.HashMap
;
22 import java
.util
.List
;
24 import java
.util
.Vector
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.apache
.tools
.ant
.BuildEvent
;
29 import org
.apache
.tools
.ant
.BuildListener
;
30 import org
.apache
.tools
.ant
.Project
;
31 import org
.apache
.tools
.ant
.ProjectHelper
;
32 import org
.apache
.tools
.ant
.helper
.ProjectHelper2
;
33 import org
.argeo
.slc
.SlcException
;
34 import org
.springframework
.core
.io
.Resource
;
36 public class AntRun
implements Runnable
{
37 private final static Log log
= LogFactory
.getLog(AntRun
.class);
39 private Resource buildFile
;
42 private List
<String
> targets
= new ArrayList
<String
>();
43 private Map
<Object
, Object
> properties
= new HashMap
<Object
, Object
>();
46 Project project
= new Project();
49 String path
= buildFile
.getURL().getPath();
50 project
.setUserProperty("ant.file", path
);
51 project
.setBaseDir(extractBaseDir(path
));
54 ProjectHelper projectHelper
= new ProjectHelper2();
55 project
.addReference(ProjectHelper
.PROJECTHELPER_REFERENCE
,
57 projectHelper
.parse(project
, buildFile
.getURL());
58 } catch (Exception e
) {
59 throw new SlcException("Could not parse " + buildFile
, e
);
62 if (properties
!= null) {
63 for (Map
.Entry
<Object
, Object
> entry
: properties
.entrySet()) {
64 project
.setUserProperty(entry
.getKey().toString(), entry
65 .getValue().toString());
69 project
.fireBuildStarted();
70 Throwable exception
= null;
72 project
.addBuildListener(new LoggingListener());
73 if (targets
.size() == 0) {
74 project
.executeTarget(project
.getDefaultTarget());
76 project
.executeTargets(new Vector
<String
>(targets
));
78 } catch (Throwable e
) {
80 throw new SlcException("Could not run Ant script " + buildFile
, e
);
82 project
.fireBuildFinished(exception
);
86 private File
extractBaseDir(String path
) {
87 if(this.baseDir
!=null)
90 String baseDir
= null;
91 if (path
.length() > 1) {
92 int indx
= path
.lastIndexOf('/', path
.length() - 1);
93 if (indx
== -1 || indx
== 0) {
96 baseDir
= path
.substring(0, indx
) + "/";
101 File file
= new File(baseDir
);
105 return new File(System
.getProperty("user.dir"));
109 public void setBuildFile(Resource buildFile
) {
110 this.buildFile
= buildFile
;
113 public void setTargets(List
<String
> targets
) {
114 this.targets
= targets
;
117 public void setProperties(Map
<Object
, Object
> properties
) {
118 this.properties
= properties
;
121 public void setBaseDir(File baseDir
) {
122 this.baseDir
= baseDir
;
125 protected static class LoggingListener
implements BuildListener
{
127 public void buildFinished(BuildEvent event
) {
128 if (log
.isDebugEnabled())
129 log
.debug("Ant build finished: " + event
);
132 public void buildStarted(BuildEvent event
) {
133 if (log
.isDebugEnabled())
134 log
.debug("Ant build started: " + event
);
137 public void messageLogged(BuildEvent event
) {
138 if (event
.getPriority() == Project
.MSG_DEBUG
) {
139 if (log
.isTraceEnabled())
140 log
.trace(event
.getMessage());
141 } else if (event
.getPriority() == Project
.MSG_VERBOSE
) {
142 if (log
.isDebugEnabled())
143 log
.debug(event
.getMessage());
144 } else if (event
.getPriority() == Project
.MSG_INFO
) {
145 log
.info(event
.getMessage());
147 } else if (event
.getPriority() == Project
.MSG_WARN
) {
148 log
.warn(event
.getMessage());
150 } else if (event
.getPriority() == Project
.MSG_ERR
) {
151 log
.error(event
.getMessage());
153 log
.error(event
.getMessage());
157 public void targetFinished(BuildEvent event
) {
158 if (log
.isTraceEnabled())
159 log
.debug("Target finished: " + event
.getTarget());
162 public void targetStarted(BuildEvent event
) {
163 if (log
.isTraceEnabled())
164 log
.debug("Target started: " + event
.getTarget());
167 public void taskFinished(BuildEvent event
) {
170 public void taskStarted(BuildEvent event
) {