1 package org
.argeo
.slc
.ant
;
7 import java
.util
.Properties
;
9 import org
.apache
.commons
.logging
.Log
;
10 import org
.apache
.commons
.logging
.LogFactory
;
11 import org
.apache
.tools
.ant
.Project
;
12 import org
.apache
.tools
.ant
.ProjectHelper
;
13 import org
.apache
.tools
.ant
.listener
.CommonsLoggingListener
;
14 import org
.argeo
.slc
.core
.structure
.StructurePath
;
15 import org
.argeo
.slc
.core
.structure
.StructureRegistry
;
17 /** Utilities to manipulate the structure registry in SLC Ant. */
18 public class AntRegistryUtil
{
19 private static Log log
= LogFactory
.getLog(AntRegistryUtil
.class);
21 /** Reads a structure registry from an Ant file without executing it. */
22 public static StructureRegistry
readRegistry(File antFile
) {
23 if (log
.isDebugEnabled())
24 log
.debug("Reads registry for Ant file " + antFile
);
25 Project p
= new Project();
26 p
.setUserProperty("ant.file", antFile
.getAbsolutePath());
27 p
.setBaseDir(antFile
.getParentFile());
29 ProjectHelper helper
= new SlcProjectHelper();
30 p
.addReference(ProjectHelper
.PROJECTHELPER_REFERENCE
, helper
);
31 helper
.parse(p
, antFile
);
33 StructureRegistry registry
= (StructureRegistry
) p
34 .getReference(SlcProjectHelper
.REF_STRUCTURE_REGISTRY
);
35 registry
.setMode(StructureRegistry
.READ
);
37 p
.executeTarget(p
.getDefaultTarget());
41 /** Executes only the active paths of the Ant file. */
42 public static Project
runActive(File antFile
,
43 List
<StructurePath
> activePaths
) {
44 if (log
.isDebugEnabled())
45 log
.debug("Runs the " + activePaths
.size()
46 + " provided active paths of Ant file " + antFile
);
47 Project p
= new Project();
48 p
.setUserProperty("ant.file", antFile
.getAbsolutePath());
49 p
.setBaseDir(antFile
.getParentFile());
51 ProjectHelper helper
= new SlcProjectHelper();
52 p
.addReference(ProjectHelper
.PROJECTHELPER_REFERENCE
, helper
);
53 helper
.parse(p
, antFile
);
55 StructureRegistry registry
= (StructureRegistry
) p
56 .getReference(SlcProjectHelper
.REF_STRUCTURE_REGISTRY
);
57 registry
.setMode(StructureRegistry
.ACTIVE
);
58 registry
.setActivePaths(activePaths
);
64 /** Executes all paths of the provided target of the Ant file. */
65 public static Project
runAll(File antFile
, String target
) {
66 if (log
.isDebugEnabled())
67 log
.debug("Runs all paths of Ant file " + antFile
);
68 Project p
= new Project();
69 p
.setUserProperty("ant.file", antFile
.getAbsolutePath());
70 p
.setBaseDir(antFile
.getParentFile());
72 ProjectHelper helper
= new SlcProjectHelper();
73 p
.addReference(ProjectHelper
.PROJECTHELPER_REFERENCE
, helper
);
74 helper
.parse(p
, antFile
);
76 runProject(p
, target
);
80 /** Executes all paths of the provided target of the Ant URL. */
81 public static Project
runAll(URL url
, String target
, Properties properties
) {
82 if (log
.isDebugEnabled())
83 log
.debug("Runs all paths of Ant URL " + url
);
84 Project p
= new Project();
85 p
.setUserProperty("ant.file", url
.toString());
86 // p.setBaseDir(url.toString());
87 p
.addBuildListener(new CommonsLoggingListener());
89 ProjectHelper helper
= new SlcProjectHelper();
90 p
.addReference(ProjectHelper
.PROJECTHELPER_REFERENCE
, helper
);
93 if (properties
!= null) {
94 for (Map
.Entry
<Object
, Object
> entry
: properties
.entrySet()) {
95 p
.setUserProperty(entry
.getKey().toString(), entry
.getValue()
100 runProject(p
, target
);
104 /** Executes all paths of the default target of the Ant file. */
105 public static Project
runAll(File antFile
) {
106 return runAll(antFile
, null);
109 protected static void runProject(Project p
, String target
) {
110 p
.fireBuildStarted();
111 Throwable exception
= null;
113 p
.executeTarget(target
!= null ? target
: p
.getDefaultTarget());
114 } catch (Throwable e
) {
116 log
.error("Exception when running Ant: ",e
);
118 p
.fireBuildFinished(exception
);