3
votes

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.

3
Alternative question: Given my Java class files are stored in "src/" and a set of dependent .jar files are defined in my User Libraries, could someone please post an example build.xml script which compiles the src files to "./dist/Project.jar", copies the .jar files defined in the libraries to a "./dist/lib/" folder, and generates the necessary manifest file in the jar?kmccoy

3 Answers

4
votes

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>
0
votes

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.

0
votes

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.