简体   繁体   English

如何使用依赖于常春藤导入的lib的Ant taskdef

[英]How can I use an Ant taskdef that depends on lib imported by ivy

I've created a taskdef in my build.xml that depends on a class that resides in a jar imported by Ivy. 我在build.xml中创建了一个taskdef,它依赖于一个由Ivy导入的jar中的类。

I'd like to understand if it's possible in any way, given the fact that my build breaks before starting because it can't find the class for the taskdef. 考虑到我的构建在启动之前就中断了,因为它找不到taskdef的类,因此我想了解是否有任何可能。

This seems a egg and chicken issue, as I can't get the class because I'd have to resolve my dependencies first (which needs the build to be working). 这似乎是一个鸡蛋和鸡肉的问题,因为我不能上课,因为我必须先解决我的依赖关系(这需要构建才能起作用)。

Yes, it is possible. 对的,这是可能的。 However, it means that you must resolve and retrieve the jars before that <taskdef> task is executed. 但是,这意味着必须在执行<taskdef>任务之前解析并检索jar。 There are two ways to do this: 有两种方法可以做到这一点:

  • Put the <ivy:resolve> and a <ivy:retrieve> task before the <typedef/> task. <ivy:resolve><ivy:retrieve>任务放在<typedef/>任务之前。 All three can be outside of a <target> . 这三个都可以在<target> However, in that case, if you run a clean , these three tasks will be executed first before the clean which means a simple clean will take longer, and you have to make sure you don't clean anything done during the <ivy:resolve> and the <ivy:retrieve> . 但是,在这种情况下,如果您运行clean ,那么这三个任务将在clean之前首先执行,这意味着简单的clean需要花费更长的时间,并且您必须确保在<ivy:resolve>期间不clean任何内容。 <ivy:resolve><ivy:retrieve>

  • Put all three in their own target, then make the target which is dependent upon that third party jar dependent upon the target that retrieves the Ivy jars and defines the task. 将所有三个对象放置在自己的目标中,然后使依赖于第三方jar的目标依赖于检索常春藤jar并定义任务的目标。 This can be difficult with something like the Ant-Contrib tasks which may be used by almost all of the tasks, but should be easier with things like some sort of packaging task that's used at the very end of your build process. 这对于诸如Ant-Contrib任务之类的事情可能很难实现,而几乎所有任务都可以使用,但是对于诸如在构建过程结束时使用的某种打包任务之类的事情,它应该变得更加容易。

The following is an example that uses ivy to retrieve the groovy task dependencies. 以下是使用常春藤检索常规任务依赖项的示例。 As stated by David the trick is to call taskdef after the ivy has resolved the dependencies and created a path reference: 正如David所说的,技巧是在常春藤解决依赖关系并创建路径引用之后调用taskdef:

The following is more normal example build 以下是更正常的示例构建

I create configurations to match the build classpaths I require. 我创建配置以匹配所需的构建类路径。 Pay special attention to the mappings for the "build" configuration. 要特别注意“构建”配置的映射。 This is how the jars associated with ANT tasks can be kept separate to the jars used for compilation. 这就是与ANT任务关联的jar可以与用于编译的jar分开保持的方式。

Bootstrapping ivy 引导常春藤

Reaction to your chicken and egg comment. 对您的鸡肉和鸡蛋的评论。

I use ivy to manage all build dependencies. 我使用常春藤来管理所有构建依赖项。 The following target ensures that ivy is installed on my build machine. 以下目标可确保在我的构建计算机上安装了ivy。

<available classname="org.apache.ivy.Main" property="ivy.installed"/>

<target name="install-ivy" description="Install ivy" unless="ivy.installed">
    <mkdir dir="${user.home}/.ant/lib"/>
    <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.3.0/ivy-2.3.0.jar"/>
    <fail message="Ivy has been installed. Run the build again"/>
</target>

Just documenting my solution in case it might help anyone. 仅记录我的解决方案,以防万一。 Doing it cleanly as in David's answer is probably the right way to go, but just in case you need the quick solution... 像大卫回答中那样干净地做可能是正确的方法,但是以防万一您需要快速的解决方案...

1) Move your taskdef inside the macro you want to call, instead of defining it outside. 1)将taskdef移到要调用的宏中,而不是在外部定义它。 This will avoid it being evaluated and the build breaking immediatelly. 这样可以避免对其进行评估,并且避免立即破坏构建。

2) Add your dependencies resolution as a dependency of the task containing the call to the macro. 2)将依赖项解析添加为包含对宏的调用的任务的依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM