简体   繁体   中英

Eclipse: Ant script to export User Library/Libraries for project

I am new to Java programming. I initially started with NetBeans but have moved to Eclipse given the advice from a friend.

In NetBeans, a pre-written ant build script for the project would generate a Project.jar file and place all required libraries/jars in a lib/ folder.

However, in Eclipse it appears that I need to write my own ant script. I have written a few lines to generate the jar file:

<target name="compile"> <mkdir dir="${build.dir}"/> <javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="jars" debug="on"/> </target>

How do I write a command to copy all of the jars in my User Library to a ${build.dir}/lib/ folder?

Thanks.

Use the copy task

like so, with the appropriate include or exclude pattern

  <copy todir="${build.dir}/lib/">
    <fileset dir="src_dir">
      <include name="**/*.jar"/>
    </fileset>
  </copy>



 <copy todir="${build.dir}/lib/">
    <fileset dir="src_dir" excludes="**/*.java"/>
  </copy>

I recommend to use ant4eclipse library for ant based eclipse projects. When you use it, you can access eclipse workspace/project settings, and can iterate tought eclipse project class path in ant.

See the example code bellow:

<path id="ant.classpath">
    <fileset dir="${lib.dir}/ant4eclipse">
        <include name="*.jar" />
</fileset>

<taskdef resource="net/sf/antcontrib/antlib.xml" />
<taskdef resource="net/sf/ant4eclipse/antlib.xml" />

<targetPlatform

<target name="copy_jars">
  <getEclipseClasspath workspace="${basedir}/.."
                       projectname="TestProject"
                       targetPlatformLocation="c:/eclipse"
                       property="classpath"
                       relative="false"
                       runtime="true"
                       pathseparator="#" />

  <!-- iterate over all classpath entries -->
  <foreach list="${classpath}" delimiter="#"
    target="copy_jar_file" param="classpath.entry" />
</target>

<target name="copy_jar_file">
  <!-- check if current is a .jar-file ... -->
  <if>
    <isfileselected file="${classpath.entry}">
      <filename name="**/*.jar" />
    </isfileselected>
    <then>
      <!-- copy the jar file to a destination directory -->
      <copy file="${classpath.entry}" tofile="${dest.dir}"/>
    </then>
  </if>
</target>

If you would like to use user libraries, you can define it by userlibraries command.

If you are new to Java take the chance to have a look at maven . It is a build tool like ant with a lot of predefined 'goals' and a fully developed dependency (to other libraries) handling. You will find a eclipse plugin which will be very useful.

Maven projects have a special directory layout which is kind of best practise and helpful for beginners. If you work on a maven project you can just use the command

mvn dependency:copy-dependencies

as a console command (or eclipse run configuration) to copy your project dependencies (libraries) to the <project>\\target\\dependency directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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