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    import java.util.Map;
024    import java.util.Iterator;
025    
026    import org.apache.tools.ant.Project;
027    import org.apache.tools.ant.Task;
028    import org.apache.tools.ant.BuildException;
029    import org.apache.tools.ant.types.Environment;
030    import org.apache.tools.ant.types.FileSet;
031    import org.apache.tools.ant.taskdefs.Property;
032    import org.apache.tools.ant.taskdefs.Java;
033    import org.apache.tools.ant.taskdefs.Mkdir;
034    import org.apache.tools.ant.taskdefs.Echo;
035    import org.apache.maven.project.MavenProject;
036    
037    import org.apache.geronimo.genesis.ComponentSupport;
038    
039    /**
040     * Support for Ant-based Mojos.
041     *
042     * @version $Rev: 470159 $ $Date: 2006-11-01 17:19:16 -0800 (Wed, 01 Nov 2006) $
043     */
044    public class AntHelper
045        extends ComponentSupport
046    {
047        private MavenProject project;
048    
049        private Project ant;
050    
051        public Project getAnt() {
052            if (ant == null) {
053                ant = new Project();
054                ant.setBaseDir(getProject().getBasedir());
055    
056                initAntLogger(ant);
057    
058                ant.init();
059            }
060    
061            return ant;
062        }
063    
064        public void setProject(final MavenProject project) {
065            assert project != null;
066    
067            this.project = project;
068        }
069    
070        private MavenProject getProject() {
071            if (project == null) {
072                throw new IllegalStateException("Not initialized; missing MavenProject");
073            }
074            return project;
075        }
076    
077        public FileSet createFileSet() {
078            FileSet set = new FileSet();
079            set.setProject(getAnt());
080            return set;
081        }
082    
083        public Task createTask(final String name) throws BuildException {
084            assert name != null;
085    
086            return getAnt().createTask(name);
087        }
088    
089        protected void initAntLogger(final Project ant) {
090            CommonsLoggingAntLoggerAdapter antLogger = new CommonsLoggingAntLoggerAdapter(log);
091            antLogger.setEmacsMode(true);
092            antLogger.setOutputPrintStream(System.out);
093            antLogger.setErrorPrintStream(System.err);
094    
095            if (log.isDebugEnabled()) {
096                antLogger.setMessageOutputLevel(Project.MSG_VERBOSE);
097            }
098            else {
099                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    }