如何在Ant中获取具有依赖项*的构建目标*列表?

时间:2013-10-17 21:28:38

标签: ant

我发现了这个问题:

How can I get a list of build targets in Ant?

我想知道的是:有没有办法获得目标列表,他们的depends-on值?我们有一个大的build.xml文件,它当前编写的方式是否存在描述并不能真正告诉我目标是主目标还是“其他”目标。

运行ant 1.8.1,这是初步的尽职调查,因为我准备升级到Gradle,所以我需要弄清楚哪些目标确实是“高级别”目标,哪些目标是“支持”目标。 / p>

注意我在锁定的环境中工作,因此无法下载第三方软件或ant扩展程序。

附加说明如果蚂蚁无法达到此级别的详细信息,那么这也是一个有效的答案

2 个答案:

答案 0 :(得分:5)

在Ant 1.8.2及更高版本中,使用-d标志打印调试信息:

ant -p -d <your main build file>

你会得到这样的细节:

 javadoc
   depends on: resolve
 javadoc.distribute
 latest-ivy
 package
   depends on: -maybe-package-by-bom, -maybe-package-by-spec, -maybe-package-for-dc

-d标志还将打印ant -p未打印的“其他”目标(没有描述的目标)及其依赖项。

答案 1 :(得分:0)

如果你想要一个递归树列表,你可以将这个XQuery脚本与Saxon一起使用:

(:~
 : XQuery to display the dependencies of an Ant target.
 :
 : There are two modes of operation:
 :   1) Display all targets and immediate dependencies, specified by $project-file
 :   2) Show a tree of a single targets dependencies, this happens when $target-name is set as well.
 :
 : External parameters:
 :  $project-file The initial Ant file to start parsing from (imports will be expanded)
 :  $target-name If specified we examine only a single target and produce a tree of all dependencies (recursively)
 :  $show-file Whether the file path of the dependency should be shown
 : 
 : Example Usage: java -cp Saxon-HE-9.7.0-18.jar net.sf.saxon.Query -q:ant-show-deps.xqy \!indent=yes project-file=file:/Users/are/exist-git/build.xml target-name=installer show-file=true
 :
 : If you don't want to specify the $target-name you can pass ?target-name=\(\) to Saxon on the command line.
 :
 : @author Adam Retter
 :)
xquery version "1.0";

declare variable $project-file external;
declare variable $target-name as xs:string? external;
declare variable $show-file as xs:boolean external;

declare function local:expand-import-targets($file as xs:string) as element(target)* {
    local:expand-import-targets($file, ())
};

declare function local:expand-import-targets($file as xs:string, $visited as xs:string*) as element(target)* {
    let $path := local:resolve($file, $visited[1])
    return
        if(not($visited = $path))then
            let $imported-project := doc($path)/project
            return
            (
                for $target in $imported-project/target
                return
              <target name="{$target/@name}" file="{$path}">
                  {
                  for $dependency in tokenize(replace($target/@depends, '\s+', ''), ',')
                  return
                      <dependency name="{$dependency}"/>
                  }
              </target>
                ,
                for $import in $imported-project/import
                return
                    local:expand-import-targets($import/@file, ($path, $visited))
            )
        else()
};

declare function local:resolve($file as xs:string, $prev-file as xs:string?) {
    if(not($prev-file))then
        $file
    else if(starts-with($file, "/") or starts-with($file, "file:/"))then
        $file
    else
        resolve-uri($file, $prev-file)
};

declare function local:target-tree($target-name as xs:string, $targets as element(target)*) as element(target)? {
    let $target := $targets[@name eq $target-name]
    return
        element target {
            $target/@name,
            if($show-file)then
                $target/@file
            else(),
            for $dependency in $target/dependency
            return
                local:expand-dependency($dependency/@name, $targets)
        }
};

declare function local:expand-dependency($dependency-name as xs:string, $targets as element(target)*) {
    for $expanded in $targets[@name eq $dependency-name]
    return
        element dependency {
            $expanded/@name,
            if($show-file)then
                $expanded/@file
            else(),
            for $sub-dependency in $expanded/dependency
            return
                local:expand-dependency($sub-dependency/@name, $targets)
        }
};

let $targets := local:expand-import-targets($project-file)
return
    if($target-name)then
        local:target-tree($target-name, $targets)
    else
        <targets>
        {
            for $target in $targets
            order by $target/@name
            return $target
        }
        </targets>