Home » apache-ant-1.7.1-src » org.apache.tools » ant » taskdefs » [javadoc | source]
    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    *
   17    */
   18   
   19   package org.apache.tools.ant.taskdefs;
   20   
   21   import org.apache.tools.ant.BuildException;
   22   import org.apache.tools.ant.ProjectHelper;
   23   import org.apache.tools.ant.Project;
   24   import org.apache.tools.ant.Task;
   25   import org.apache.tools.ant.util.FileUtils;
   26   
   27   import java.io.File;
   28   import java.util.Vector;
   29   
   30   /**
   31    * Task to import another build file into the current project.
   32    * <p>
   33    * It must be 'top level'. On execution it will read another Ant file
   34    * into the same Project.
   35    * </p>
   36    * <p>
   37    * <b>Important</b>: we have not finalized how relative file references
   38    * will be resolved in deep/complex build hierarchies - such as what happens
   39    * when an imported file imports another file. Use absolute references for
   40    * enhanced build file stability, especially in the imported files.
   41    * </p>
   42    * <p>Examples:</p>
   43    * <pre>
   44    * &lt;import file="../common-targets.xml"/&gt;
   45    * </pre>
   46    * <p>Import targets from a file in a parent directory.</p>
   47    * <pre>
   48    * &lt;import file="${deploy-platform}.xml"/&gt;
   49    * </pre>
   50    * <p>Import the project defined by the property <code>deploy-platform</code>.</p>
   51    *
   52    * @since Ant1.6
   53    * @ant.task category="control"
   54    */
   55   public class ImportTask extends Task {
   56       private String file;
   57       private boolean optional;
   58       private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
   59   
   60       /**
   61        * sets the optional attribute
   62        *
   63        * @param optional if true ignore files that are not present,
   64        *                 default is false
   65        */
   66       public void setOptional(boolean optional) {
   67           this.optional = optional;
   68       }
   69   
   70       /**
   71        * the name of the file to import. How relative paths are resolved is still
   72        * in flux: use absolute paths for safety.
   73        * @param file the name of the file
   74        */
   75       public void setFile(String file) {
   76           // I don't think we can use File - different rules
   77           // for relative paths.
   78           this.file = file;
   79       }
   80   
   81       /**
   82        *  This relies on the task order model.
   83        *
   84        */
   85       public void execute() {
   86           if (file == null) {
   87               throw new BuildException("import requires file attribute");
   88           }
   89           if (getOwningTarget() == null
   90               || !"".equals(getOwningTarget().getName())) {
   91               throw new BuildException("import only allowed as a top-level task");
   92           }
   93   
   94           ProjectHelper helper =
   95                   (ProjectHelper) getProject().
   96                       getReference(ProjectHelper.PROJECTHELPER_REFERENCE);
   97   
   98           if (helper == null) {
   99               // this happens if the projecthelper was not registered with the project.
  100               throw new BuildException("import requires support in ProjectHelper");
  101           }
  102   
  103           Vector importStack = helper.getImportStack();
  104   
  105           if (importStack.size() == 0) {
  106               // this happens if ant is used with a project
  107               // helper that doesn't set the import.
  108               throw new BuildException("import requires support in ProjectHelper");
  109           }
  110   
  111           if (getLocation() == null || getLocation().getFileName() == null) {
  112               throw new BuildException("Unable to get location of import task");
  113           }
  114   
  115           File buildFile = new File(getLocation().getFileName()).getAbsoluteFile();
  116   
  117           // Paths are relative to the build file they're imported from,
  118           // *not* the current directory (same as entity includes).
  119   
  120           File buildFileParent = new File(buildFile.getParent());
  121           File importedFile = FILE_UTILS.resolveFile(buildFileParent, file);
  122   
  123           getProject().log("Importing file " + importedFile + " from "
  124                            + buildFile.getAbsolutePath(), Project.MSG_VERBOSE);
  125   
  126           if (!importedFile.exists()) {
  127               String message =
  128                   "Cannot find " + file + " imported from "
  129                   + buildFile.getAbsolutePath();
  130               if (optional) {
  131                   getProject().log(message, Project.MSG_VERBOSE);
  132                   return;
  133               } else {
  134                   throw new BuildException(message);
  135               }
  136           }
  137   
  138           if (importStack.contains(importedFile)) {
  139               getProject().log(
  140                   "Skipped already imported file:\n   "
  141                   + importedFile + "\n", Project.MSG_VERBOSE);
  142               return;
  143           }
  144   
  145           try {
  146               helper.parse(getProject(), importedFile);
  147           } catch (BuildException ex) {
  148               throw ProjectHelper.addLocationToBuildException(
  149                   ex, getLocation());
  150           }
  151       }
  152   
  153   }

Save This Page
Home » apache-ant-1.7.1-src » org.apache.tools » ant » taskdefs » [javadoc | source]