子项目未在 NetBeans build.xml 文件中看到 Ant 变量集

Subprojects not seeing Ant variables set in NetBeans build.xml file

我将几个不同的 NetBeans 项目链接在一起。我有一个 MainProject 链接在 SubProject 中作为编译时库。

我正在尝试修改 MainProject 的 Ant build.xml 脚本以设置新的 Ant 变量。我可以在 MainProject 中正确设置和查看此变量,但 SubProject 的 Ant build.xml 脚本看不到此变量。

我的 build.xml 脚本在 MainProject

<?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>

我的 build.xml 脚本在 SubProject

<?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>

现在当我 运行 clean and buildMainProject 上时,我得到以下输出:

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 查看由 MainProject 设置的 ${test.test.test} 变量的状态?

编辑 - 我正在为我的项目使用 NetBeans 自动生成的 ant 构建脚本。 Netbeans 创建一个 build-impl.xml 和一个 build.xml。当您调用 ant 进程时,它会调用 build-impl.xml 中的目标。然后 build-impl.xml 将调用我在 build.xml 中的自定义代码。例如,NetBeans IDE 中的 运行ning 一个 clean and build 将调用 build-impl.xml 中的适当目标,而 build-impl.xml 将依次调用我在 build.xml。我可以 post build-impl.xml 脚本,但它们只是 Netbeans IDE.

生成的标准脚本

我在调查 .build-impl.xml 脚本后找到了答案。

我发现 .build-impl.xml for MainProject 使用以下 ant 代码调用 SubProject 的构建:

<antcall target="-maybe-call-dep">
    <param name="call.built.properties" value="${built-jar.properties}"/>
    <param location="${project.SubProject}" name="call.subproject"/>
    <param location="${project.SubProject}/build.xml" name="call.script"/>
    <param name="call.target" value="jar"/>
    <param name="transfer.built-jar.properties" value="${built-jar.properties}"/>
    <param name="transfer.not.archive.disabled" value="true"/>
</antcall>

现在有了这些知识,我可以看到 SubProject 只能访问 MainProject 中的一组有限字段。其中一个字段 ${built-jar.properties} 是 ant 属性文件的路径。我在 ${built-jar.properties} 文件的位置旁边创建了一个新的属性文件。在新的属性文件中,我放置了我想要存储的所有 ant 变量。现在,在 SubProject 中,我创建了一个指向新文件的变量。这涉及在 ${built-jar.properties} 文件上使用 dirname 命令来获取目录。然后我调用了一个 loadproperties ant 命令来读取我所有的变量。

这是一个相当长的解决方法,但我终于得到了我想要的一切。

如果以后有人对所有代码感兴趣,我很乐意编辑我的答案并将其放在这里。当似乎不太可能有人会参考这个答案时,我不想做额外的工作。