001// Copyright 2005-2006 Ferdinand Prantl <prantl@users.sourceforge.net>
002// Copyright 2001-2004 The Apache Software Foundation
003// All rights reserved.
004//
005// Licensed under the Apache License, Version 2.0 (the "License");
006// you may not use this file except in compliance with the License.
007// You may obtain a copy of the License at
008//
009// http://www.apache.org/licenses/LICENSE-2.0
010//
011// Unless required by applicable law or agreed to in writing, software
012// distributed under the License is distributed on an "AS IS" BASIS,
013// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014// See the License for the specific language governing permissions and
015// limitations under the License.
016//
017// See http://ant-eclipse.sourceforge.net for the most recent version
018// and more information.
019
020package prantl.ant.eclipse;
021
022import java.io.File;
023
024import org.apache.tools.ant.BuildException;
025import org.apache.tools.ant.Task;
026
027/**
028 * Generates project configuration files for Eclipse. Writes files .project and .classpath
029 * for the Eclipse IDE using the settings from the Ant script. The configuration for the
030 * task is stored in an object of the class EclipseElement and the actual output is
031 * delegated into an implementation of the interface EclipseOutput.
032 * 
033 * @see EclipseElement
034 * @since Ant-Eclipse 1.0
035 * @author Ferdinand Prantl &lt;prantl@users.sourceforge.net&gt;
036 */
037public class EclipseTask extends Task {
038
039    private EclipseElement eclipse;
040
041    private EclipseOutput output;
042
043    /**
044     * Creates a new instance of the task. Default constructor, to be called by ant in the
045     * productive environment.
046     * 
047     * @since Ant-Eclipse 1.0
048     */
049    public EclipseTask() {
050        eclipse = new EclipseElement();
051        output = null;
052    }
053
054    /**
055     * Creates a new instance of the task. Testing constructor, to be called in unit tests
056     * only.
057     * 
058     * @param object
059     *        An object that the output is delegated into.
060     * @throws NullPointerException
061     *         If the parameter <tt>object</tt> is null.
062     * @since Ant-Eclipse 1.0
063     */
064    protected EclipseTask(EclipseOutput object) {
065        eclipse = object.getEclipse();
066        output = object;
067    }
068
069    /**
070     * Returns the internal object containing the configuration. The method is supposed to
071     * be used in unit tests only.
072     * 
073     * @return The internal object containing the configuration.
074     * @since Ant-Eclipse 1.0
075     */
076    EclipseElement getEclipse() {
077        return eclipse;
078    }
079
080    /**
081     * Returns the internal object performing the output. The method is supposed to be
082     * used in unit tests only.
083     * 
084     * @return The internal object performing the output.
085     * @since Ant-Eclipse 1.0
086     */
087    EclipseOutput getOutput() {
088        return output;
089    }
090
091    /**
092     * If true, stop the build process if the generation of the project files fails.
093     * 
094     * @param value
095     *        <tt>True</tt> if it should halt, otherwise <tt>false</tt>.
096     * @since Ant-Eclipse 1.0
097     */
098    public void setFailOnError(boolean value) {
099        eclipse.setFailOnError(value);
100    }
101
102    /**
103     * Property to set to "true" if there is an error in the generation of the project
104     * files.
105     * 
106     * @param name
107     *        A name of the property to set in case of an error.
108     * @since Ant-Eclipse 1.0
109     */
110    public void setErrorProperty(String name) {
111        eclipse.setErrorProperty(name);
112    }
113
114    /**
115     * High-level project creation mode controlling defaults for the generated files and
116     * changing behavior according to the specified project type, which usually responds
117     * to the chosen development toolkit.
118     * 
119     * @param value
120     *        A project creation mode.
121     * @since Ant-Eclipse 1.0
122     */
123    public void setMode(EclipseElement.Mode value) {
124        eclipse.setMode(value);
125    }
126
127    /**
128     * Sets if the generated files are to be written always or only if the Ant build
129     * script has been changed. The latter is default.
130     * 
131     * @param flag
132     *        <tt>True</tt> if the files should always be overwritten, otherwise
133     *        <tt>false</tt>.
134     * @since Ant-Eclipse 1.0
135     */
136    public void setUpdateAlways(boolean flag) {
137        eclipse.setUpdateAlways(flag);
138    }
139
140    /**
141     * Sets the destination directory to place generated files into.
142     * 
143     * @param dir
144     *        A directory to place output files into.
145     * @since Ant-Eclipse 1.0
146     */
147    public void setDestDir(File dir) {
148        eclipse.setDestDir(dir);
149    }
150
151    /**
152     * Adds a definition of the settings element. Files in the directory
153     * <tt>.settings</tt> will be generated according to settings in this object. Only
154     * one settings element is allowed.
155     * 
156     * @return A definition of the settings element.
157     * @throws BuildException
158     *         If another settings element has been defined.
159     * @since Ant-Eclipse 1.0
160     */
161    public SettingsElement createSettings() {
162        eclipse.setSettings(new SettingsElement());
163        return eclipse.getSettings();
164    }
165
166    /**
167     * Adds a definition of the project element. The file .project will be generated
168     * according to settings in this object. Only one project element is allowed.
169     * 
170     * @return A definition of the project element.
171     * @throws BuildException
172     *         If another project element has been defined.
173     * @since Ant-Eclipse 1.0
174     */
175    public ProjectElement createProject() {
176        eclipse.setProject(new ProjectElement());
177        return eclipse.getProject();
178    }
179
180    /**
181     * Adds a definition of the classpath element. The file .classpath will be generated
182     * according to settings in this object. Only one classpath element is allowed.
183     * 
184     * @return A definition of the classpath element.
185     * @throws BuildException
186     *         If another classpath element has been defined.
187     * @since Ant-Eclipse 1.0
188     */
189    public ClassPathElement createClassPath() {
190        eclipse.setClassPath(new ClassPathElement());
191        return eclipse.getClassPath();
192    }
193
194    /**
195     * Generates the output files. Eventually existing files will be overwritten only if
196     * the timestamp of the ant project file is newer as the timestamp of a particular
197     * file.
198     * 
199     * @throws BuildException
200     *         In case of misconfiguration or errors.
201     * @since Ant-Eclipse 1.0
202     */
203    public void execute() throws BuildException {
204        if (output == null)
205            output = new FileEclipseOutput(this);
206        new SettingsGenerator(this).generate();
207        new ProjectGenerator(this).generate();
208        new ClassPathGenerator(this).generate();
209    }
210
211}