【问题标题】:Ant (with ant-contrib) build - path / dependency issue - Reference Not FoundAnt(使用 ant-contrib)构建 - 路径/依赖问题 - 未找到参考
【发布时间】: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


【解决方案1】:

你的问题是:

<path id="classpath.compile">    
  <fileset dir="${COTS_DIR}/main" includes="${COTS_DEPENDENCIES}" />            
  <path refid="${trackDependencyPath}" />
</path>

指的不是 refid,而是一个属性(或 antcontrib 变量),在这种情况下
具有相同的值 => 'cpDependency4'

-- 评论后编辑--

即使使用字符串 'cpDependency4' 也无济于事,因为路径 ID 在 for 循环中从未正确设置,正如一些测试所揭示的那样:

<for list="${PROJECT_DEPENDENCIES}" param="dependency">
  <sequential>
    ...
    <var name="trackDependencyPath" value="cpDependency${index}" />
    <!-- put pathconvert here -->
  </sequential>      
</for>

试试看:

<pathconvert property="foo"><path refid="cpDependency4"/></pathconvert>


<pathconvert property="foo"><path refid="cpDependency${index}"/></pathconvert>

你会得到:

Reference cpDependency4 not found.

在这两种情况下。提高噪声级进行调试,您会看到:

Adding reference: cpDependency0
Setting ro project property: trackDependencyPath -> cpDependency0
Setting ro project property: index -> 0
Setting ro project property: prior -> 0
Setting ro project property: index -> 1
Setting project property: var1 -> /path/to/other/dependency1
Adding reference: cpDependency${index}
Property "env" has not been set
Property "env" has not been set
Setting ro project property: trackDependencyPath -> cpDependency1
Setting ro project property: prior -> 1
Setting ro project property: index -> 2
Setting project property: var2 -> /path/to/other/dependency2
Overriding previous definition of reference to cpDependency${index}
Adding reference: cpDependency${index}
Property "env" has not been set
Property "env" has not been set
Setting ro project property: trackDependencyPath -> cpDependency2
Setting ro project property: prior -> 2
Setting ro project property: index -> 3
Setting project property: var3 -> /path/to/other/dependency3
Overriding previous definition of reference to cpDependency${index}
Adding reference: cpDependency${index}
Property "env" has not been set
Property "env" has not been set
Setting ro project property: trackDependencyPath -> cpDependency3
Setting ro project property: prior -> 3
Setting ro project property: index -> 4
Setting project property: var4 -> /path/to/other/dependency4
Overriding previous definition of reference to cpDependency${index}
Adding reference: cpDependency${index} 

【讨论】:

  • 好的 - 这很有道理 - 但你知道一种方法来获得所需的行为吗?不过,让我感到困惑的是 refid 是一个字符串——我从变量中得到了它的显式名称。如果我在 refid 中明确输入名称“cpDependency4” - 它会给出相同的错误(并且 cpDependency4 是 ant 路径,与 ant-contrib 无关)。
  • Wild - 所以看起来 ${index} 没有在循环中解释。达格纳比特。而且,感谢您的帮助!不是真正的解决方案,而是一个很好的解释。
  • 是的 - 没有想出一个解决方案,但你的脚本似乎很复杂,我相信有一个更简单的解决方案。如果您需要完整的依赖管理,请查看另一个 apache 项目 - 与 ant 一起玩得很好 - 名为 Ivy => ant.apache.org/ivy/https://ant.apache.org/ivy
  • 是的,你对常春藤的看法可能是对的。这开始很简单,但已经离我有点远了。
  • 毕竟 Ant 不是一种编程语言 - 如果可以切换工具,您应该评估 Gradle => gradle.org
猜你喜欢
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-28
  • 2017-02-20
相关资源
最近更新 更多