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
.BuildException
;
12 import org
.apache
.tools
.ant
.Project
;
13 import org
.apache
.tools
.ant
.ProjectHelper
;
15 import org
.argeo
.slc
.core
.structure
.StructurePath
;
16 import org
.argeo
.slc
.core
.structure
.StructureRegistry
;
18 /** Utilities to manipulate the structure registry in SLC Ant. */
19 public class AntRegistryUtil
{
20 private static Log log
= LogFactory
.getLog(AntRegistryUtil
.class);
22 /** Reads a structure registry from an Ant file without executing it. */
23 public static StructureRegistry
readRegistry(File antFile
) {
24 if (log
.isDebugEnabled())
25 log
.debug("Reads registry for Ant file " + antFile
);
26 Project p
= new Project();
27 p
.setUserProperty("ant.file", antFile
.getAbsolutePath());
28 p
.setBaseDir(antFile
.getParentFile());
30 ProjectHelper helper
= new SlcProjectHelper();
31 p
.addReference(ProjectHelper
.PROJECTHELPER_REFERENCE
, helper
);
32 helper
.parse(p
, antFile
);
34 StructureRegistry registry
= (StructureRegistry
) p
35 .getReference(SlcProjectHelper
.REF_STRUCTURE_REGISTRY
);
36 registry
.setMode(StructureRegistry
.READ
);
38 p
.executeTarget(p
.getDefaultTarget());
42 /** Executes only the active paths of the Ant file. */
43 public static Project
runActive(File antFile
,
44 List
<StructurePath
> activePaths
) {
45 if (log
.isDebugEnabled())
46 log
.debug("Runs the " + activePaths
.size()
47 + " provided active paths of Ant file " + antFile
);
48 Project p
= new Project();
49 p
.setUserProperty("ant.file", antFile
.getAbsolutePath());
50 p
.setBaseDir(antFile
.getParentFile());
52 ProjectHelper helper
= new SlcProjectHelper();
53 p
.addReference(ProjectHelper
.PROJECTHELPER_REFERENCE
, helper
);
54 helper
.parse(p
, antFile
);
56 StructureRegistry registry
= (StructureRegistry
) p
57 .getReference(SlcProjectHelper
.REF_STRUCTURE_REGISTRY
);
58 registry
.setMode(StructureRegistry
.ACTIVE
);
59 registry
.setActivePaths(activePaths
);
65 /** Executes all paths of the provided target of the Ant file. */
66 public static Project
runAll(File antFile
, String target
) {
67 if (log
.isDebugEnabled())
68 log
.debug("Runs all paths of Ant file " + antFile
);
69 Project p
= new Project();
70 p
.setUserProperty("ant.file", antFile
.getAbsolutePath());
71 p
.setBaseDir(antFile
.getParentFile());
73 ProjectHelper helper
= new SlcProjectHelper();
74 p
.addReference(ProjectHelper
.PROJECTHELPER_REFERENCE
, helper
);
75 helper
.parse(p
, antFile
);
77 runProject(p
, target
);
81 /** Executes all paths of the provided target of the Ant URL. */
82 public static Project
runAll(URL url
, String target
, Properties properties
) {
83 if (log
.isDebugEnabled())
84 log
.debug("Runs all paths of Ant URL " + url
);
85 Project p
= new Project();
86 p
.setUserProperty("ant.file", url
.toString());
87 // p.setBaseDir(url.toString());
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
) {
117 p
.fireBuildFinished(exception
);