📄 ant.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.taskdefs;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintStream;import java.lang.reflect.Method;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import java.util.Vector;import java.util.Set;import java.util.HashSet;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.BuildListener;import org.apache.tools.ant.DefaultLogger;import org.apache.tools.ant.Project;import org.apache.tools.ant.ProjectComponent;import org.apache.tools.ant.ProjectHelper;import org.apache.tools.ant.Target;import org.apache.tools.ant.Task;import org.apache.tools.ant.MagicNames;import org.apache.tools.ant.Main;import org.apache.tools.ant.types.PropertySet;import org.apache.tools.ant.util.FileUtils;/** * Build a sub-project. * * <pre> * <target name="foo" depends="init"> * <ant antfile="build.xml" target="bar" > * <property name="property1" value="aaaaa" /> * <property name="foo" value="baz" /> * </ant></span> * </target></span> * * <target name="bar" depends="init"> * <echo message="prop is ${property1} ${foo}" /> * </target> * </pre> * * * @since Ant 1.1 * * @ant.task category="control" */public class Ant extends Task { private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); /** the basedir where is executed the build file */ private File dir = null; /** * the build.xml file (can be absolute) in this case dir will be * ignored */ private String antFile = null; /** the output */ private String output = null; /** should we inherit properties from the parent ? */ private boolean inheritAll = true; /** should we inherit references from the parent ? */ private boolean inheritRefs = false; /** the properties to pass to the new project */ private Vector properties = new Vector(); /** the references to pass to the new project */ private Vector references = new Vector(); /** the temporary project created to run the build file */ private Project newProject; /** The stream to which output is to be written. */ private PrintStream out = null; /** the sets of properties to pass to the new project */ private Vector propertySets = new Vector(); /** the targets to call on the new project */ private Vector targets = new Vector(); /** whether the target attribute was specified **/ private boolean targetAttributeSet = false; /** * simple constructor */ public Ant() { //default } /** * create a task bound to its creator * @param owner owning task */ public Ant(Task owner) { bindToOwner(owner); } /** * If true, pass all properties to the new Ant project. * Defaults to true. * @param value if true pass all properties to the new Ant project. */ public void setInheritAll(boolean value) { inheritAll = value; } /** * If true, pass all references to the new Ant project. * Defaults to false. * @param value if true, pass all references to the new Ant project */ public void setInheritRefs(boolean value) { inheritRefs = value; } /** * Creates a Project instance for the project to call. */ public void init() { newProject = getProject().createSubProject(); newProject.setJavaVersionProperty(); } /** * Called in execute or createProperty (via getNewProject()) * if newProject is null. * * <p>This can happen if the same instance of this task is run * twice as newProject is set to null at the end of execute (to * save memory and help the GC).</p> * <p>calls init() again</p> * */ private void reinit() { init(); } /** * Attaches the build listeners of the current project to the new * project, configures a possible logfile, transfers task and * data-type definitions, transfers properties (either all or just * the ones specified as user properties to the current project, * depending on inheritall), transfers the input handler. */ private void initializeProject() { newProject.setInputHandler(getProject().getInputHandler()); Iterator iter = getBuildListeners(); while (iter.hasNext()) { newProject.addBuildListener((BuildListener) iter.next()); } if (output != null) { File outfile = null; if (dir != null) { outfile = FILE_UTILS.resolveFile(dir, output); } else { outfile = getProject().resolveFile(output); } try { out = new PrintStream(new FileOutputStream(outfile)); DefaultLogger logger = new DefaultLogger(); logger.setMessageOutputLevel(Project.MSG_INFO); logger.setOutputPrintStream(out); logger.setErrorPrintStream(out); newProject.addBuildListener(logger); } catch (IOException ex) { log("Ant: Can't set output to " + output); } } // set user-defined properties getProject().copyUserProperties(newProject); if (!inheritAll) { // set Ant's built-in properties separately, // because they are not being inherited. newProject.initProperties(); } else { // set all properties from calling project addAlmostAll(getProject().getProperties()); } Enumeration e = propertySets.elements(); while (e.hasMoreElements()) { PropertySet ps = (PropertySet) e.nextElement(); addAlmostAll(ps.getProperties()); } } /** * Handles output. * Send it the the new project if is present, otherwise * call the super class. * @param outputToHandle The string output to output. * @see Task#handleOutput(String) * @since Ant 1.5 */ public void handleOutput(String outputToHandle) { if (newProject != null) { newProject.demuxOutput(outputToHandle, false); } else { super.handleOutput(outputToHandle); } } /** * Handles input. * Deleate to the created project, if present, otherwise * call the super class. * @param buffer the buffer into which data is to be read. * @param offset the offset into the buffer at which data is stored. * @param length the amount of data to read. * * @return the number of bytes read. * * @exception IOException if the data cannot be read. * @see Task#handleInput(byte[], int, int) * @since Ant 1.6 */ public int handleInput(byte[] buffer, int offset, int length) throws IOException { if (newProject != null) { return newProject.demuxInput(buffer, offset, length); } return super.handleInput(buffer, offset, length); } /** * Handles output. * Send it the the new project if is present, otherwise * call the super class. * @param toFlush The string to output. * @see Task#handleFlush(String) * @since Ant 1.5.2 */ public void handleFlush(String toFlush) { if (newProject != null) { newProject.demuxFlush(toFlush, false); } else { super.handleFlush(toFlush); } } /** * Handle error output. * Send it the the new project if is present, otherwise * call the super class. * @param errorOutputToHandle The string to output. * * @see Task#handleErrorOutput(String) * @since Ant 1.5 */ public void handleErrorOutput(String errorOutputToHandle) { if (newProject != null) { newProject.demuxOutput(errorOutputToHandle, true); } else { super.handleErrorOutput(errorOutputToHandle); } } /** * Handle error output. * Send it the the new project if is present, otherwise * call the super class. * @param errorOutputToFlush The string to output. * @see Task#handleErrorFlush(String) * @since Ant 1.5.2 */ public void handleErrorFlush(String errorOutputToFlush) { if (newProject != null) { newProject.demuxFlush(errorOutputToFlush, true); } else { super.handleErrorFlush(errorOutputToFlush); } } /** * Do the execution. * @throws BuildException if a target tries to call itself; * probably also if a BuildException is thrown by the new project. */ public void execute() throws BuildException { File savedDir = dir; String savedAntFile = antFile; Vector locals = new Vector(targets); try { getNewProject(); if (dir == null && inheritAll) { dir = getProject().getBaseDir(); } initializeProject(); if (dir != null) { newProject.setBaseDir(dir); if (savedDir != null) { // has been set explicitly newProject.setInheritedProperty(MagicNames.PROJECT_BASEDIR, dir.getAbsolutePath()); } } else { dir = getProject().getBaseDir(); } overrideProperties(); if (antFile == null) { antFile = Main.DEFAULT_BUILD_FILENAME; } File file = FILE_UTILS.resolveFile(dir, antFile); antFile = file.getAbsolutePath(); log("calling target(s) " + ((locals.size() > 0) ? locals.toString() : "[default]") + " in build file " + antFile, Project.MSG_VERBOSE); newProject.setUserProperty(MagicNames.ANT_FILE , antFile); String thisAntFile = getProject().getProperty(MagicNames.ANT_FILE); // Are we trying to call the target in which we are defined (or // the build file if this is a top level task)? if (thisAntFile != null && file.equals(getProject().resolveFile(thisAntFile)) && getOwningTarget() != null) { if (getOwningTarget().getName().equals("")) { if (getTaskName().equals("antcall")) { throw new BuildException("antcall must not be used at" + " the top level."); } throw new BuildException(getTaskName() + " task at the" + " top level must not invoke" + " its own build file."); } } try { ProjectHelper.configureProject(newProject, file); } catch (BuildException ex) { throw ProjectHelper.addLocationToBuildException( ex, getLocation()); } if (locals.size() == 0) { String defaultTarget = newProject.getDefaultTarget(); if (defaultTarget != null) { locals.add(defaultTarget); } } if (newProject.getProperty(MagicNames.ANT_FILE) .equals(getProject().getProperty(MagicNames.ANT_FILE)) && getOwningTarget() != null) { String owningTargetName = getOwningTarget().getName(); if (locals.contains(owningTargetName)) { throw new BuildException(getTaskName() + " task calling " + "its own parent target."); } boolean circular = false; for (Iterator it = locals.iterator(); !circular && it.hasNext();) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -