2012-05-21 58 views
2

我需要使用ImportTask將當前build.xml中的所有xml文件包含在目錄(我不知道名稱和文件數)中。導入目錄中的所有構建文件 - phing

這是我build.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<project name="New Project" basedir="." default="myimport"> 
    <target name="myimport" description="dummy to import other build files"> 
     <if> 
      <isset property="file" /> 
      <then> 
       <echo msg="Importing ${file}" /> 
       <import file="${file}" /> 
      </then> 
     </if> 
    </target> 

    <if> 
     <not> 
      <isset property="dummy.property" /> 
     </not> 
     <then> 
      <echo msg="Now include all files in ./dev/build directory" /> 
      <property name="dummy.property" value="true" /> 
      <foreach param="msg" absparam="file" target="myimport"> 
       <fileset dir="./dev/build/"> 
        <include name="*.xml"/> 
       </fileset> 
      </foreach> 
     </then> 
    </if> 
</project> 

,並在目標目錄中的示例文件:

<?xml version="1.0" encoding="utf-8"?> 
<project name="test" basedir="." default="dummy"> 
    <target name="dummy" description="Dummy task"> 
     <echo msg="Dummy task, just for test" /> 
    </target> 

    <echo msg="Imported!" /> 
</project> 

當我運行phing -l結果是:

Buildfile: /home/f0rud/workspace/s/build.xml 
    [echo] Now include all files in ./dev/build directory 
    [foreach] Calling Buildfile '/home/f0rud/workspace/s/build.xml' with target 'myimport' 

New Project > myimport: 

    [echo] Importing ./dev/build/test.xml 
    [echo] Imported! 
Default target: 
---------------------------------------------------------------------------- 
myimport dummy to import other build files 

Main targets: 
---------------------------------------------------------------------------- 
myimport dummy to import other build files 

但有沒有虛擬(或test.dummy)目標,爲什麼?

注:有一個有趣的錯誤,如果我刪除if部分,我得到Maximum function nesting level of '100' reached, aborting! error但那不是我的問題(如果將解決這個問題。)

回答

1

的問題是在全球範圍內的進口工作。

當我在目標中調用它時,它在全局上下文中不可用。

所以我寫了一個簡單的Phing任務加載文件集的所有文件,像這樣:

class ImportDirTask extends Task { 

    /** Array of filesets */ 
    private $filesets = array(); 

    /** 
    * Nested creator, adds a set of files (nested fileset attribute). 
    */ 
    function createFileSet() { 
     $num = array_push($this->filesets, new FileSet()); 
     return $this->filesets[$num-1]; 
    } 

    /** 
    * Parse a Phing build file and copy the properties, tasks, data types and 
    * targets it defines into the current project. 
    * 
    * @return void 
    */ 
    public function main() { 
    // filesets 
    foreach ($this->filesets as $fs) { 
     $ds  = $fs->getDirectoryScanner($this->project); 
     $srcFiles = $ds->getIncludedFiles(); 
     $srcDirs = $ds->getIncludedDirectories(); 
     foreach ($srcFiles as $f) 
     { 
      $task = new ImportTask(); 
      $task->setProject($this->project); 
      $task->init(); 
      $task->setFile($this->file = $fs->getDir($this->project) . FileSystem::getFileSystem()->getSeparator() . $f); 
      $task->main(); 
     } 

    } 
    } //end main 

} //end ImportDirTask 

這只是工作。