View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.geronimo.genesis.ant;
21  
22  import java.io.File;
23  import java.util.Map;
24  import java.util.Iterator;
25  
26  import org.apache.tools.ant.Project;
27  import org.apache.tools.ant.Task;
28  import org.apache.tools.ant.BuildException;
29  import org.apache.tools.ant.types.Environment;
30  import org.apache.tools.ant.types.FileSet;
31  import org.apache.tools.ant.taskdefs.Property;
32  import org.apache.tools.ant.taskdefs.Java;
33  import org.apache.tools.ant.taskdefs.Mkdir;
34  import org.apache.tools.ant.taskdefs.Echo;
35  import org.apache.maven.project.MavenProject;
36  
37  import org.apache.geronimo.genesis.ComponentSupport;
38  
39  /**
40   * Support for Ant-based Mojos.
41   *
42   * @version $Rev: 470159 $ $Date: 2006-11-01 17:19:16 -0800 (Wed, 01 Nov 2006) $
43   */
44  public class AntHelper
45      extends ComponentSupport
46  {
47      private MavenProject project;
48  
49      private Project ant;
50  
51      public Project getAnt() {
52          if (ant == null) {
53              ant = new Project();
54              ant.setBaseDir(getProject().getBasedir());
55  
56              initAntLogger(ant);
57  
58              ant.init();
59          }
60  
61          return ant;
62      }
63  
64      public void setProject(final MavenProject project) {
65          assert project != null;
66  
67          this.project = project;
68      }
69  
70      private MavenProject getProject() {
71          if (project == null) {
72              throw new IllegalStateException("Not initialized; missing MavenProject");
73          }
74          return project;
75      }
76  
77      public FileSet createFileSet() {
78          FileSet set = new FileSet();
79          set.setProject(getAnt());
80          return set;
81      }
82  
83      public Task createTask(final String name) throws BuildException {
84          assert name != null;
85  
86          return getAnt().createTask(name);
87      }
88  
89      protected void initAntLogger(final Project ant) {
90          CommonsLoggingAntLoggerAdapter antLogger = new CommonsLoggingAntLoggerAdapter(log);
91          antLogger.setEmacsMode(true);
92          antLogger.setOutputPrintStream(System.out);
93          antLogger.setErrorPrintStream(System.err);
94  
95          if (log.isDebugEnabled()) {
96              antLogger.setMessageOutputLevel(Project.MSG_VERBOSE);
97          }
98          else {
99              antLogger.setMessageOutputLevel(Project.MSG_INFO);
100         }
101 
102         ant.addBuildListener(antLogger);
103     }
104 
105     protected void setProperty(final String name, Object value) {
106         assert name != null;
107         assert value != null;
108 
109         String valueAsString = String.valueOf(value);
110 
111         if (log.isDebugEnabled()) {
112             log.debug("Setting property: " + name + "=" + valueAsString);
113         }
114 
115         Property prop = (Property)createTask("property");
116         prop.setName(name);
117         prop.setValue(valueAsString);
118         prop.execute();
119     }
120 
121     public void inheritProperties() {
122         // Propagate properties
123         Map props = getProject().getProperties();
124         Iterator iter = props.keySet().iterator();
125         while (iter.hasNext()) {
126             String name = (String)iter.next();
127             String value = String.valueOf(props.get(name));
128             setProperty(name, value);
129         }
130 
131         // Hardcode a few
132         setProperty("pom.basedir", getProject().getBasedir());
133     }
134     
135     public void setSystemProperty(final Java java, final String name, final String value) {
136         assert java != null;
137         assert name != null;
138         assert value != null;
139 
140         Environment.Variable var = new Environment.Variable();
141         var.setKey(name);
142         var.setValue(value);
143         java.addSysproperty(var);
144     }
145 
146     public void setSystemProperty(final Java java, final String name, final File value) {
147         assert java != null;
148         assert name != null;
149         assert value != null;
150 
151         Environment.Variable var = new Environment.Variable();
152         var.setKey(name);
153         var.setFile(value);
154         java.addSysproperty(var);
155     }
156 
157     public void mkdir(final File dir) {
158         assert dir != null;
159 
160         Mkdir mkdir = (Mkdir)createTask("mkdir");
161         mkdir.setDir(dir);
162         mkdir.execute();
163     }
164 
165     public void echo(final String msg) {
166         assert msg != null;
167         
168         Echo echo = (Echo)createTask("echo");
169         echo.setMessage(msg);
170         echo.execute();
171     }
172 }