1
2
3
4
5
6
7
8
9
10
11
12
13 package org.sonatype.install4j.maven;
14
15 import org.apache.maven.plugin.Mojo;
16 import org.apache.maven.plugin.logging.Log;
17 import org.apache.maven.project.MavenProject;
18 import org.apache.tools.ant.BuildException;
19 import org.apache.tools.ant.Project;
20 import org.apache.tools.ant.ProjectComponent;
21 import org.apache.tools.ant.Task;
22 import org.apache.tools.ant.taskdefs.Chmod;
23 import org.apache.tools.ant.taskdefs.Mkdir;
24 import org.apache.tools.ant.taskdefs.Property;
25 import org.apache.tools.ant.types.Path;
26
27 import java.io.File;
28 import java.util.Iterator;
29 import java.util.Map;
30
31
32
33
34
35
36 public class AntHelper
37 {
38 private final Mojo owner;
39
40 private final Log log;
41
42 private final MavenProject project;
43
44 private final Project ant;
45
46 public AntHelper(final Mojo owner, final MavenProject project) {
47 assert owner != null;
48 assert project != null;
49
50 this.owner = owner;
51 this.log = owner.getLog();
52 this.project = project;
53
54 ant = new Project();
55 ant.setBaseDir(project.getBasedir());
56 initAntLogger(ant);
57 ant.init();
58
59
60 inheritProperties();
61 }
62
63 private void initAntLogger(final Project ant) {
64 MavenAntLoggerAdapter antLogger = new MavenAntLoggerAdapter(log);
65 antLogger.setEmacsMode(true);
66 antLogger.setOutputPrintStream(System.out);
67 antLogger.setErrorPrintStream(System.err);
68
69 if (log.isDebugEnabled()) {
70 antLogger.setMessageOutputLevel(Project.MSG_VERBOSE);
71 }
72 else {
73 antLogger.setMessageOutputLevel(Project.MSG_INFO);
74 }
75
76 ant.addBuildListener(antLogger);
77 }
78
79 private void inheritProperties() {
80
81 Map props = project.getProperties();
82 Iterator iter = props.keySet().iterator();
83 while (iter.hasNext()) {
84 String name = (String) iter.next();
85 String value = String.valueOf(props.get(name));
86 setProperty(name, value);
87 }
88
89
90 setProperty("project.basedir", project.getBasedir());
91 }
92
93 public <T extends ProjectComponent> T createTask(final Class<T> type) {
94 assert type != null;
95
96 T task = null;
97 try {
98 task = type.newInstance();
99 }
100 catch (Exception e) {
101 throw new RuntimeException(e);
102 }
103 task.setProject(ant);
104 return task;
105 }
106
107 public Task createTask(final String name) throws BuildException {
108 assert name != null;
109 return ant.createTask(name);
110 }
111
112 public Path createPath(final File location) {
113 Path path = new Path(ant);
114 path.setLocation(location);
115 return path;
116 }
117
118 public void setProperty(final String name, final Object value) {
119 assert name != null;
120 assert value != null;
121
122 String valueAsString = String.valueOf(value);
123
124 if (log.isDebugEnabled()) {
125 log.debug(String.format("Setting property: %s=%s", name, valueAsString));
126 }
127
128 Property prop = createTask(Property.class);
129 prop.setName(name);
130 prop.setValue(valueAsString);
131 prop.execute();
132 }
133
134 public String getProperty(final String name) {
135 return ant.getProperty(name);
136 }
137
138 public void mkdir(final File dir) {
139 assert dir != null;
140
141 Mkdir mkdir = createTask(Mkdir.class);
142 mkdir.setDir(dir);
143 mkdir.execute();
144 }
145
146 public void chmod(final File dir, final String includes, final String perm) {
147 Chmod chmod = createTask(Chmod.class);
148 chmod.setDir(dir);
149 chmod.setIncludes(includes);
150 chmod.setPerm(perm);
151 chmod.execute();
152 }
153
154 public void chmod(final File file, final String perm) {
155 Chmod chmod = createTask(Chmod.class);
156 chmod.setFile(file);
157 chmod.setPerm(perm);
158 chmod.execute();
159 }
160 }