How to add to classpath all classes from set of directories in ant?

微笑、不失礼 提交于 2019-12-12 10:36:12

问题


How to add to classpath all classes from set of directories?

I have following property:

class.dirs=lib1dir,lib2dir,lib3dir

There are classes under these directories.
Is it possible to add all classes under these directories to classpath?

Something like:

<classpath>
   <dirset dir="${root.dir}" includes="${class.dirs}/**/*.class"/>
</classpath>

or

<classpath>
   <pathelement location="${class.dirs}" />
</classpath>

But this example does not work, of course.


回答1:


You can set up a path to include all .class files from your specific directories:

<path id="mypath"> 
  <fileset dir="${root.dir}">
    <include name="lib1dir/**/*.class lib2dir/**/*.class lib3dir/**/*.class"/>
  </fileset>
</path> 

However, if you want to use this path as a classpath, you only need to reference the root folders, otherwise you will get ClassNotFoundErrors as the package names translate into directories:

<path id="build.classpath"> 
  <dirset dir="${root.dir}">
    <include name="lib1dir lib2dir lib3dir"/>
  </dirset>
</path> 

Then reference the path by its id when using (e.g. for classpath):

<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" />


来源:https://stackoverflow.com/questions/10704324/how-to-add-to-classpath-all-classes-from-set-of-directories-in-ant

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