子项目未在NetBeans build.xml文件中设置Ant变量

时间:2017-06-21 17:20:19

标签: netbeans ant

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

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

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 build上运行MainProject时,我得到以下输出:

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.xmlbuild.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生成的标准脚本。

1 个答案:

答案 0 :(得分:0)

在调查.build-impl.xml脚本之后,我能够找到答案。

我发现.build-impl.xml的{​​{1}}使用以下ant代码调用MainProject的构建:

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是ant属性文件的路径。我创建了一个新的属性文件,位于${built-jar.properties}文件的位置旁边。在新的属性文件中,我放置了我想要存储的所有ant变量。现在,在${built-jar.properties}中,我创建了一个指向新文件的变量。这涉及在SubProject文件上使用dirname命令来获取目录。然后我调用${built-jar.properties} ant命令来读取我的所有变量。

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

如果将来有人对所有代码感兴趣,我很乐意编辑我的答案并将其放在这里。当看起来不太可能有人提到这个答案时,我不想完成额外的工作。