生成Ant构建文件 [英] Generate Ant build file
问题描述
我有以下项目结构:
root/
comp/
env/
version/
build.xml
build.xml
build.xml
其中的根/ comp / env的/版本/ build.xml文件是:
<project name="comp-env-version" basedir=".">
<import file="../build.xml" optional="true" />
<echo>Comp Env Version tasks</echo>
<target name="run">
<echo>Comp Env Version run task</echo>
</target>
</project>
根/ comp / env的/ build.xml文件是:
<project name="comp-env" basedir=".">
<import file="../build.xml" optional="true" />
<echo>Comp Env tasks</echo>
<target name="run">
<echo>Comp Env run task</echo>
</target>
</project>
根/ COMP / build.xml文件是:
<project name="comp" basedir=".">
<echo>Comp tasks</echo>
</project>
每个构建文件导入父生成文件和每个孩子的继承和覆盖父任务/属性。
Each build file imports the parent build file and each child inherits and overrides parent tasks/properties.
我需要的是让生成的XML构建不运行任何
例如,如果我对根/ comp / env的/版/运行蚁族(或类似的东西),我想获得以下输出:
For example, if I run "ant" (or something like that) on root/comp/env/version/, I would like to get the following output:
<project name="comp-env-version" basedir=".">
<echo>Comp tasks</echo>
<echo>Comp Env tasks</echo>
<echo>Comp Env Version tasks</echo>
<target name="run">
<echo>Comp Env Version run task</echo>
</target>
</project>
有一个Ant插件来做到这一点?使用Maven?什么是我的选择,如果不呢?
Is there an Ant plugin to do this? With Maven? What are my options if not?
编辑:
我需要的东西,如MVN帮助:有效-POM为Ant
推荐答案
根据上的导入任务,它的工作原理非常像一个实体包含了两个附加功能:
Based on the description of the import task, it works very much like an entity includes with two additional features:
- 目标覆盖
- 特殊属性
有关查看有效构建的目的,我不认为特殊性能的处理是必需的(尽管它可能是通过遍历插入目标添加)。这样的处理来实现这一变
For the purposes of viewing the "effective build" I don't think the special properties processing is required (though it could be added by iterating the inserted targets). So the processing to achieve this becomes.
- 解析build.xml文件到DOM
- 对于每个顶级包括(仅提供顶级允许)标签,找到引用源文件。
- 解析引用的build.xml
- 插入从引用的build.xml不与那些在当前文件发生冲突的任何内容。
- 引用的build.xml文件(S),直到没有找到 重复步骤2
- 输出生成的DOM
您可以定义使这个处理可以在任务定义为从您的构建中运行一个自定义Ant任务。看到这个教程了解更多详情。
You can define a custom Ant task so that this processing can be defined in a task to be run from within your build. See this tutorial for more details.
下面是通过进口递归并插入从引用的文件中的DOM元素的基本实现。有一个在它几乎可以肯定一些错误,因为我把它扔在一起,但它应该主要是你以后做的:
Here's a basic implementation that recurses through imports and inserts the DOM elements from the referenced files. There's almost certainly a few bugs in it as I threw it together, but it should do largely what you're after:
/**
* Reads the build.xml and outputs the resolved build to stdout
*/
public static void main(String[] args) {
try {
Element root = new EffectiveBuild().parse(new File(args[0]));
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
outputter.output(root, System.out);
} catch (Exception e) {
// TODO handle errors
e.printStackTrace();
}
}
/**
* Get the DOM for the passed file and iterate all imports, replacing with
* non-duplicate referenced content
*/
private Element parse(File buildFile) throws JDOMException, IOException {
Element root = getRootElement(buildFile);
List<Element> imports = root.getChildren("import");
for (int i = 0; i < imports.size(); i++) {
Element element = imports.get(i);
List<Content> importContent = parseImport(element, root, buildFile);
int replaceIndex = root.indexOf(element);
root.addContent(replaceIndex, importContent);
root.removeContent(element);
}
root.removeChildren("import");
return root;
}
/**
* Get the imported file and merge it into the parent.
*/
private List<Content> parseImport(Element element, Element currentRoot,
File buildFile) throws JDOMException, IOException {
String importFileName = element.getAttributeValue("file");
File importFile = new File(buildFile.getParentFile(), importFileName)
.getAbsoluteFile();
if (importFileName != null) {
Element importRoot = getRootElement(importFile);
return getImportContent(element, currentRoot, importRoot,
importFile);
}
return Collections.emptyList();
}
/**
* Replace the passed element with the content of the importRoot
* (not the project tag)
*/
private List<Content> getImportContent(Element element,
Element currentRoot, Element importRoot, File buildFile)
throws JDOMException, IOException {
if (currentRoot != null) {
// copy all the reference import elements to the parent if needed
List<Content> childNodes = importRoot.cloneContent();
List<Content> importContent = new ArrayList<Content>();
for (Content content : childNodes) {
if (content instanceof Element
&& ((Element) content).getName().equals("import")) {
importContent.addAll(parseImport((Element) content,
currentRoot, buildFile));
}
if (!existsInParent(currentRoot, content)) {
importContent.add(content);
} else {
// TODO note the element was skipped
}
}
return importContent;
}
return Collections.emptyList();
}
/**
* Return true if the content already defined in the parent
*/
private boolean existsInParent(Element parent, Content content) {
if (content instanceof Text) {
if (((Text) content).getText().trim().length() == 0) {
// let the pretty printer deal with the whitespace
return false;
}
return true;
}
if (content instanceof Element) {
String id = ((Element) content).getAttributeValue("name");
String name = ((Element) content).getName();
List<Content> parentContent = parent.getChildren();
if (id != null) {
for (Content content2 : parentContent) {
if (content2 instanceof Element
&& ((Element) content2).getName().equals(name)) {
String parentId = ((Element) content2)
.getAttributeValue("name");
if (parentId != null && parentId.equals(id)) {
return true;
}
}
}
}
}
return false;
}
/**
* Parse the passed file.
*/
private Element getRootElement(File buildFile) throws JDOMException,
IOException {
SAXBuilder builder = new SAXBuilder();
builder.setValidation(false);
builder.setIgnoringElementContentWhitespace(true);
Document doc = builder.build(buildFile);
Element root = doc.getRootElement();
return root;
}
这篇关于生成Ant构建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!