001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.geronimo.genesis.ant;
021    
022    import java.io.File;
023    
024    import java.util.Map;
025    import java.util.Iterator;
026    
027    import org.apache.maven.plugin.MojoExecutionException;
028    import org.apache.maven.plugin.MojoFailureException;
029    
030    import org.apache.tools.ant.Project;
031    import org.apache.tools.ant.Task;
032    import org.apache.tools.ant.BuildException;
033    import org.apache.tools.ant.types.FileSet;
034    import org.apache.tools.ant.types.Environment;
035    import org.apache.tools.ant.taskdefs.Java;
036    import org.apache.tools.ant.taskdefs.Mkdir;
037    import org.apache.tools.ant.taskdefs.Property;
038    
039    import org.apache.geronimo.genesis.MojoSupport;
040    
041    /**
042     * Support for Ant-based Mojos.
043     *
044     * @deprecated Use the {@link AntHelper} component.
045     *
046     * @version $Rev: 469680 $ $Date: 2006-10-31 14:23:54 -0800 (Tue, 31 Oct 2006) $
047     */
048    public abstract class AntMojoSupport
049        extends MojoSupport
050    {
051        protected Project ant;
052    
053        protected void init() throws MojoExecutionException, MojoFailureException {
054            super.init();
055    
056            ant = new Project();
057            ant.setBaseDir(getProject().getBasedir());
058            
059            initAntLogger(ant);
060    
061            ant.init();
062    
063            // Inherit properties from Maven
064            inheritProperties();
065        }
066        
067        protected void initAntLogger(final Project ant) {
068            MavenAntLoggerAdapter antLogger = new MavenAntLoggerAdapter(log);
069            antLogger.setEmacsMode(true);
070            antLogger.setOutputPrintStream(System.out);
071            antLogger.setErrorPrintStream(System.err);
072            
073            if (log.isDebugEnabled()) {
074                antLogger.setMessageOutputLevel(Project.MSG_VERBOSE);
075            }
076            else {
077                antLogger.setMessageOutputLevel(Project.MSG_INFO);
078            }
079            
080            ant.addBuildListener(antLogger);
081        }
082        
083        protected void setProperty(final String name, Object value) {
084            assert name != null;
085            assert value != null;
086    
087            String valueAsString = String.valueOf(value);
088    
089            if (log.isDebugEnabled()) {
090                log.debug("Setting property: " + name + "=" + valueAsString);
091            }
092    
093            Property prop = (Property)createTask("property");
094            prop.setName(name);
095            prop.setValue(valueAsString);
096            prop.execute();
097        }
098    
099        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    }