【发布时间】:2014-02-21 15:44:15
【问题描述】:
我正在尝试创建一个系统,我可以在其中使用通用 build.xml 作为我们绝大多数构建的模板。我非常接近。基本上,应用程序级 build.xml 文件只需要定义一些东西(目标 jar、依赖项(作为列表)和一些其他状态项),然后导入模板 build.xml。
我遇到的问题是我正在尝试获取依赖项并以编程方式构建类路径。我很接近,我想。但是,一旦在编译步骤中引用了类路径,它就会尝试解析似乎在编译步骤发生之前已正确准备的引用。而且,它会导致 BUILD FAILED : Reference cpDependency4 not found。
注意:COTS 的东西存储在同一个目录中,因此使这部分工作变得微不足道。项目依赖可能都来自不同的子目录,也需要拉入子依赖项/目录。
以下是应用程序 build.xml 中准备的项目示例:
<property name="PROJECT_DEPENDENCIES" value="/path/to/other/dependency1,/path/to/other/dependency2,/path/to/other/dependency3,/path/to/other/dependency4" />
<property name="COTS_DEPENDENCIES" value="commons-io-*.jar,log4j-*.jar,spring-aop-*.RELEASE.jar,spring-beans-*.RELEASE.jar,spring-context-*.RELEASE.jar,spring-core-*.RELEASE.jar,spring-jms-*.RELEASE.jar,spring-oxm-*.RELEASE.jar,spring-web-*.RELEASE.jar"/>
<!-- ... -->
<import file="${DEVELOPMENT_BASE_DIR}/common/ant/antTemplate.xml" />
然后antTemplate文件包含:
<path id="cpDependency0" />
<var name="trackDependencyPath" value="cpDependency0" />
<var name="index" value="0"/>
<for list="${PROJECT_DEPENDENCIES}" param="dependency">
<sequential>
<var name="prior" value="${index}"/>
<math result="index" operand1="${index}" operation="+" operand2="1" datatype="int" />
<property name="var${index}" value="@{dependency}" />
<path id="cpDependency${index}">
<path refid="cpDependency${prior}"/>
<fileset dir="@{dependency}/jar/${env}" includes="*.jar" />
<fileset dir="@{dependency}/jar/${env}/dependencies" includes="*.jar" />
</path>
<var name="trackDependencyPath" value="cpDependency${index}" />
</sequential>
</for>
<path id="classpath.compile">
<fileset dir="${COTS_DIR}/main" includes="${COTS_DEPENDENCIES}" />
<path refid="${trackDependencyPath}" />
</path>
这似乎设置正确,但是......稍后,当它到达编译目标时:
<target name="compile" depends="init">
<echo message="Compiling ${ant.project.name}..." />
<javac source="1.7" target="1.7" debug="${debug}" srcdir="${SOURCE_JAVA_MAIN_DIR}" destdir="${BUILD_JAVA_MAIN_DIR}" classpathref="classpath.compile" />
<jar destfile="${PACKAGE_DIR}/${TARGET_JAR}" basedir="${BUILD_JAVA_MAIN_DIR}" excludes="*.jar" />
<copy todir="${PACKAGE_DIR}">
<path refid="classpath.compile" />
</copy>
</target>
我收到以下错误(在 javac 行):
BUILD FAILED
/path/to/common/ant/antTemplate.xml:188: Reference cpDependency4 not found.
【问题讨论】:
-
不直接回答您的问题,但我建议调查使用 ivy 来管理类路径。此外,ANT 库是管理通用构建逻辑的一种非常有用的方法。有关 ANT 库示例,请参阅:stackoverflow.com/questions/15643191/…
标签: ant reference classpath build.xml ant-contrib