Classpath for taskdef?

拈花ヽ惹草 提交于 2019-12-05 16:36:02

问题


I am defining a new task in Ant. I exported it as a jar and added to my buildfile:

<taskdef classname="X" classpath="Y.jar"/>

The problem is that this fails at runtime. It tells me it didn't find the class. By adding the jar to the classpath, it is corrected.

My question is: Is there a way that I can refer to my jar from the Ant buildfile, without changing the classpath?


回答1:


If you know the path of your jar, inside ant script you can define the classpath for your own task.

<taskdef name="myTaskName" classname="com.myorg.myclass">
  <classpath>
    <pathelement location="pathToMyJar.jar"/>
  </classpath>
</taskdef>



回答2:


Yes. I'm assuming that you looked at the doc for taskdef, which just shows the task name and implementing class. However, taskdef subclasses typedef, and if you look at the doc for the latter you'll see that there's also a classpath attribute.

HOWEVER, using this attribute means that your ant scripts are tied to a particular environment; they aren't very portable. A far better approach is to pass the classpath into Ant, using the -lib invocation option.



来源:https://stackoverflow.com/questions/4511129/classpath-for-taskdef

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!