gpt4 book ai didi

ant - 使用 Ant 1.8 扩展点支持父构建的惯用语

转载 作者:行者123 更新时间:2023-12-04 23:26:12 25 4
gpt4 key购买 nike

请记住,以下示例是从我们非常复杂的构建结构中大大简化的。

假设我有一个充分利用 Ant 1.8 扩展点的构建,这样我就有了一组通用的目标,定义如下:

<?xml version="1.0" encoding="utf-8"?>
<project name="common-targets">

<extension-point name="compile" />
<extension-point name="test" depends="compile" />
<extension-point name="package" depends="test" />
<extension-point name="deploy" depends="package" />

</project>

后面的每个阶段都依赖于前面的阶段,创建了一系列明确定义的步骤链,构建的其他部分可以做出贡献;例如,编译步骤可能定义为:
<?xml version="1.0" encoding="utf-8"?>
<project name="compile">

<import file="common-targets.xml" />

<target name="compile:compile" extensionOf="compile">
<echo message="Compiling..." />
</target>

</project>

而测试可能是:
<?xml version="1.0" encoding="utf-8"?>
<project name="test">

<import file="common-targets.xml" />

<target name="test:test" extensionOf="test">
<echo message="Testing..." />
</target>

</project>

等等。我可以以各种方式组合它们,为了便于讨论,假设它们位于一个名为 default-lifecycle.xml 的构建文件中:
<?xml version="1.0" encoding="utf-8"?>
<project name="default-lifecycle">

<import file="compile.xml" />
<import file="test.xml" />
<import file="package.xml" />
<import file="deploy.xml" />

</project>

一个名为 child1 的项目可以这样使用:
<?xml version="1.0" encoding="utf-8"?>
<project name="child1">
<import file="../scripts/default-lifecycle.xml" />
</project>

而名为 child2 的 child1 的对等(巧妙地)将是相同的:
<?xml version="1.0" encoding="utf-8"?>
<project name="child2">
<import file="../scripts/default-lifecycle.xml" />
</project>

因此,我已经实现了一个值得称赞的目标,即创建可以跨多个项目拆分的可重用构建。更好的是,每个构建都可以通过添加添加到定义明确的扩展点的目标来定制,从而在我们需要时使事情变得非常灵活。

但是,我无法找出创建分层父/子构建的最佳方法,因此我的目录结构如下:
.-+
|
+-build.xml
+-child1
|
+-build.xml
+-child2
|
+-build.xml
+-scripts

这样我就可以调用顶级构建中的安装目标,并让它在每个 child 中执行安装目标。

恕我直言,理想情况下,我会通过让父构建导入公共(public)目标并将目标绑定(bind)到每个公共(public)目标来做到这一点,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<project name="parent">

<import file="common-targets.xml" />

<target name="parent:compile" depends="parent:init-build-path" extensionOf="compile">
<subant target="compile" buildpathref="parent..build-path" />
</target>

<target name="parent:test" depends="parent:init-build-path" extensionOf="test">
<subant target="test" buildpathref="parent..build-path" />
</target>

<target name="parent:package" depends="parent:init-build-path" extensionOf="package">
<subant target="package" buildpathref="parent..build-path" />
</target>

<target name="parent:deploy" depends="parent:init-build-path" extensionOf="deploy">
<subant target="deploy" buildpathref="parent..build-path" />
</target>

<target name="parent:init-build-path">
<path id="parent..build-path">
<fileset dir="." includes="**/build.xml" excludes="build.xml" />
</path>
<echo message="Build order is ${toString:parent..build-path}" />
</target>

</project>

但是,这对我不起作用,因为我们最终会进行层次结构的广度优先下降而不是深度优先:如果我们确实部署在层次结构的顶层,我们首先在每个项目中进行编译,然后我们做测试,然后我们进行打包,然后进行部署。我需要它来执行在 child1 中部署的所有步骤,然后在 child2 中执行所有步骤,等等。

可以通过忘记在父构建中使用扩展点而直接定义每个目标,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<project name="parent">

<target name="compile" depends="parent:init-build-path">
<subant target="compile" buildpathref="parent..build-path" />
</target>

<target name="test" depends="parent:init-build-path">
<subant target="test" buildpathref="parent..build-path" />
</target>

<target name="package" depends="parent:init-build-path">
<subant target="package" buildpathref="parent..build-path" />
</target>

<target name="deploy" depends="parent:init-build-path">
<subant target="deploy" buildpathref="parent..build-path" />
</target>

<target name="parent:init-build-path">
<path id="parent..build-path">
<fileset dir="." includes="**/build.xml" excludes="build.xml" />
</path>
<echo message="Build order is ${toString:parent..build-path}" />
</target>

</project>

但是,现在我的父版本没有扩展点机制,可以在不覆盖目标的情况下轻松增强它们。

我是不是想太多了? Ant 社区是否提出了成语、模式、使用指南、说明或任何东西来帮助我解决这个难题?

最佳答案

However, this doesn't work for me because we end up doing a breadth first descent of the hierarchy instead of a depth first: if we do deploy on the top level of the hierarchy, we first do compile in each project, then we do test, then we do package, then we do deploy. I need it to do all the steps to deploy in child1, then all the steps in child2, etc.



真的吗 ?
如果您从根目录调用“ant parent:deploy”,在调用 child2 之前,它是否不会先对 child1 项目进行完整部署?

关于ant - 使用 Ant 1.8 扩展点支持父构建的惯用语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4996129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com