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  
24  import java.util.Map;
25  import java.util.Iterator;
26  
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  
30  import org.apache.tools.ant.Project;
31  import org.apache.tools.ant.Task;
32  import org.apache.tools.ant.BuildException;
33  import org.apache.tools.ant.types.FileSet;
34  import org.apache.tools.ant.types.Environment;
35  import org.apache.tools.ant.taskdefs.Java;
36  import org.apache.tools.ant.taskdefs.Mkdir;
37  import org.apache.tools.ant.taskdefs.Property;
38  
39  import org.apache.geronimo.genesis.MojoSupport;
40  
41  /**
42   * Support for Ant-based Mojos.
43   *
44   * @deprecated Use the {@link AntHelper} component.
45   *
46   * @version $Rev: 469680 $ $Date: 2006-10-31 14:23:54 -0800 (Tue, 31 Oct 2006) $
47   */
48  public abstract class AntMojoSupport
49      extends MojoSupport
50  {
51      protected Project ant;
52  
53      protected void init() throws MojoExecutionException, MojoFailureException {
54          super.init();
55  
56          ant = new Project();
57          ant.setBaseDir(getProject().getBasedir());
58          
59          initAntLogger(ant);
60  
61          ant.init();
62  
63          // Inherit properties from Maven
64          inheritProperties();
65      }
66      
67      protected void initAntLogger(final Project ant) {
68          MavenAntLoggerAdapter antLogger = new MavenAntLoggerAdapter(log);
69          antLogger.setEmacsMode(true);
70          antLogger.setOutputPrintStream(System.out);
71          antLogger.setErrorPrintStream(System.err);
72          
73          if (log.isDebugEnabled()) {
74              antLogger.setMessageOutputLevel(Project.MSG_VERBOSE);
75          }
76          else {
77              antLogger.setMessageOutputLevel(Project.MSG_INFO);
78          }
79          
80          ant.addBuildListener(antLogger);
81      }
82      
83      protected void setProperty(final String name, Object value) {
84          assert name != null;
85          assert value != null;
86  
87          String valueAsString = String.valueOf(value);
88  
89          if (log.isDebugEnabled()) {
90              log.debug("Setting property: " + name + "=" + valueAsString);
91          }
92  
93          Property prop = (Property)createTask("property");
94          prop.setName(name);
95          prop.setValue(valueAsString);
96          prop.execute();
97      }
98  
99      protected void setSystemProperty(final Java java, final String name, final String value) {
100         assert java != null;
101         assert name != null;
102         assert value != null;
103 
104         Environment.Variable var = new Environment.Variable();
105         var.setKey(name);
106         var.setValue(value);
107         java.addSysproperty(var);
108     }
109 
110     protected void setSystemProperty(final Java java, final String name, final File value) {
111         assert java != null;
112         assert name != null;
113         assert value != null;
114 
115         Environment.Variable var = new Environment.Variable();
116         var.setKey(name);
117         var.setFile(value);
118         java.addSysproperty(var);
119     }
120     
121     protected 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     protected FileSet createFileSet() {
136         FileSet set = new FileSet();
137         set.setProject(ant);
138         return set;
139     }
140 
141     protected Task createTask(final String name) throws BuildException {
142         assert name != null;
143 
144         return ant.createTask(name);
145     }
146 
147     protected void mkdir(final File dir) {
148         assert dir != null;
149 
150         Mkdir mkdir = (Mkdir)createTask("mkdir");
151         mkdir.setDir(dir);
152         mkdir.execute();
153     }
154 }