【发布时间】:2017-06-24 03:56:12
【问题描述】:
我有几个不同的 NetBeans 项目链接在一起。我有一个MainProject,它作为编译时库链接到SubProject。
我正在尝试修改 MainProject 的 Ant build.xml 脚本以设置新的 Ant 变量。我可以在MainProject 中正确设置和查看此变量,但SubProject 的Ant build.xml 脚本看不到此变量。
MainProject 中的我的 build.xml 脚本
<?xml version="1.0" encoding="UTF-8"?>
<project name="MainProject" default="default" basedir=".">
<description>Builds, tests, and runs the project MainProject.</description>
<import file="nbproject/build-impl.xml"/>
<target name="-pre-init">
<echo>MainProject Pre Init</echo>
<echo>${ant.version}</echo>
<property name="test.test.test" value="isset" />
<echo>test.test.test=${test.test.test}</echo>
</target>
</project>
SubProject 中的我的 build.xml 脚本
<?xml version="1.0" encoding="UTF-8"?>
<project name="SubProject" default="default" basedir=".">
<description>Builds, tests, and runs the project SubProject.</description>
<import file="nbproject/build-impl.xml"/>
<target name="-pre-init">
<echo>SubProject Pre Init</echo>
<echo>test.test.test=${test.test.test}</echo>
</target>
</project>
现在,当我在 MainProject 上运行 clean and build 时,我得到以下输出:
ant -f C:\\sandbox\\MainProject -Dnb.internal.action.name=rebuild clean jar
MainProject Pre Init
Apache Ant(TM) version 1.9.7 compiled on April 9 2016
test.test.test=isset
init:
deps-clean:
Updating property file: C:\sandbox\MainProject\build\built-clean.properties
SubProject Pre Init
test.test.test=${test.test.test}
SubProject.init:
SubProject.deps-clean:
Updating property file: C:\sandbox\MainProject\build\built-clean.properties
Deleting directory C:\sandbox\SubProject\build
SubProject.clean:
Deleting directory C:\sandbox\MainProject\build
clean:
MainProject Pre Init
Apache Ant(TM) version 1.9.7 compiled on April 9 2016
test.test.test=isset
init:
deps-jar:
Created dir: C:\sandbox\MainProject\build
Updating property file: C:\sandbox\MainProject\build\built-jar.properties
SubProject Pre Init
test.test.test=${test.test.test}
SubProject.init:
SubProject.deps-jar:
Created dir: C:\sandbox\SubProject\build
Updating property file: C:\sandbox\MainProject\build\built-jar.properties
Created dir: C:\sandbox\SubProject\build\classes
Created dir: C:\sandbox\SubProject\build\empty
Created dir: C:\sandbox\SubProject\build\generated-sources\ap-source-output
Compiling 1 source file to C:\sandbox\SubProject\build\classes
SubProject.compile:
Created dir: C:\sandbox\SubProject\dist
Copying 1 file to C:\sandbox\SubProject\build
Nothing to copy.
Building jar: C:\sandbox\SubProject\dist\SubProject.jar
To run this application from the command line without Ant, try:
java -jar "C:\sandbox\SubProject\dist\SubProject.jar"
SubProject.jar:
Created dir: C:\sandbox\MainProject\build\classes
Created dir: C:\sandbox\MainProject\build\empty
Created dir: C:\sandbox\MainProject\build\generated-sources\ap-source-output
Compiling 1 source file to C:\sandbox\MainProject\build\classes
compile:
Created dir: C:\sandbox\MainProject\dist
Copying 1 file to C:\sandbox\MainProject\build
Copy libraries to C:\sandbox\MainProject\dist\lib.
Building jar: C:\sandbox\MainProject\dist\MainProject.jar
To run this application from the command line without Ant, try:
java -jar "C:\sandbox\MainProject\dist\MainProject.jar"
jar:
BUILD SUCCESSFUL (total time: 4 seconds)
如何让SubProject 看到${test.test.test} 变量的状态,因为它是由MainProject 设置的?
编辑 - 我正在为我的项目使用 NetBeans 自动生成的 ant 构建脚本。 Netbeans 创建一个build-impl.xml 和一个build.xml。当您调用ant 进程时,它会调用build-impl.xml 中的目标。然后build-impl.xml 将在build.xml 中调用我的自定义代码。例如,在 NetBeans IDE 中运行clean and build 将调用build-impl.xml 中的适当目标,build-impl.xml 将依次调用build.xml 中的我的自定义代码。我可以发布 build-impl.xml 脚本,但它们只是 Netbeans IDE 生成的标准脚本。
【问题讨论】:
-
我在 Main 和 sub 中也看到了目标
-pre-init。有什么原因吗? -
子项目的目标是怎么调用的?可以发布可重现的构建脚本吗?还要检查你是否可以使用 subant - ant.apache.org/manual/Tasks/subant.html
-
饶,谢谢您的回复。为了回答您的问题,我使用的是 NetBeans IDE。 NetBeans 创建一个
build-impl.xml和一个build.xml。我用我的附加代码发布了我的build.xml。我可以发布build-impl.xml,但它们是由 IDE 自动生成的。当您运行构建时,它会调用build-impl.xml中的目标,这将拉入build.xml中的自定义代码。-pre-init目标由build-impl.xml提供,它将在运行build-impl.xml中的init目标之前运行您的自定义代码。我在build-impl.xml文件中没有看到 subant。