我将几个不同的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.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生成的标准脚本。
答案 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命令来读取我的所有变量。
这是一个很长的解决方法,但我终于得到了我想要的一切。
如果将来有人对所有代码感兴趣,我很乐意编辑我的答案并将其放在这里。当看起来不太可能有人提到这个答案时,我不想完成额外的工作。