gpt4 book ai didi

ant - ant能不能生成一个包含所有import的统一脚本,类似于maven的effective pom?

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

我有一个 ant 脚本,它导入其他脚本,这些脚本导入额外的脚本,导致定义可以发生的地方的网络。

ant 能否像 maven 的 help:effective-pom 一样拉入所有定义并输出一个文件?

我想将其作为输出文件添加到我的构建中,以使调试更容易一些。

最佳答案

您可以创建另一个目标来创建那种文件。例如,它看起来像这样:

main build.xml 做一些工作,也可以生成有效的构建:

<project name="testxml" default="build">

<import file="build1.xml" />
<import file="build2.xml" />

<target name="build">
<echo>build project</echo>
</target>

<target name="effective.build">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpath="C:\Program Files (x86)\groovy-2.1.4\embeddable\groovy-all-2.1.4.jar" />
<groovy>
def regex = ~/import file="(.*)"/
def buildXmlContent = new File('build.xml').text
new File('build.xml').eachLine {
line -> regex.matcher(line).find() {
def file = new File(it[1])
if (file.exists()) {
def replacement = file.getText().replaceAll(/&lt;project.*/, '').replaceAll(/&lt;\/project.*/, '')
buildXmlContent = buildXmlContent.replaceFirst(/&lt;import.*/, replacement)
}
}
}
new File('build.effective.xml') &lt;&lt; buildXmlContent
</groovy>
</target>

</project>

为测试创建了两个 build.xml:

<project name="testxml2" default="build">
<target name="clean">
<echo>clean project</echo>
</target>
</project>

<project name="testxml1" default="build">
<target name="test">
<echo>test project</echo>
</target>
</project>

如果你要运行ant effective.build你会得到新的build.effective.xml包含此内容的文件:

<project name="testxml" default="build">


<target name="test">
<echo>test project</echo>
</target>


<target name="clean">
<echo>clean project</echo>
</target>


<target name="build">
<echo>build project</echo>
</target>

<target name="effective.build">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpath="C:\Program Files (x86)\groovy-2.1.4\embeddable\groovy-all-2.1.4.jar" />
<groovy>
import java.util.regex.Pattern

def regex = ~/import file="(.*)"/
def buildXmlContent = new File('build.xml').text
new File('build.xml').eachLine {
line -> regex.matcher(line).find() {
def file = new File(it[1])
if (file.exists()) {
def replacement = file.getText().replaceAll(/&lt;project.*/, '').replaceAll(/&lt;\/project.*/, '')
buildXmlContent = buildXmlContent.replaceFirst(/&lt;import.*/, replacement)
}
}
}
new File('build.effective.xml') &lt;&lt; buildXmlContent
</groovy>
</target>

</project>

为此,我选择了 groovy(因为我实际上是在学习它),但您可以用另一种语言创建它。您还可以将此 groovy 代码编译为 ant 任务并将其用作新任务,例如 <effectiveant outfile="build.effective.xml"> .我的示例并不完美,但它展示了其中一种解决方案。

关于ant - ant能不能生成一个包含所有import的统一脚本,类似于maven的effective pom?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13256407/

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