views:

327

answers:

3

I have a rather length project I'm working on, which requires an older JDK to compile correctly, various JAR includes, and the like. I'm assembling the entire project using a batch script, and I'd like to make the whole process fully automated with the script. So I was wondering if I could view the shell commands Eclipse does to make a particular piece of code turn into a JAR (from compiling with the appropriate JDK I specified to making the JAR), so I don't need to manually go in and waste a ton of time making sure I do things right in the shell myself. Thanks!

+3  A: 

Eclipse doesn't execute shell commands for compilation. It uses its own builtin compiler.

It is possible to let Eclipse generate ANT Script for you. This script can compile your classes using Eclipse builtin compiler. It will also configure classpath for compilation. It might be good first step. To create script, select your project and choose Export > Ant Buildfiles in context menu.

Peter Štibraný
Interesting. With an ANT script though, is there a possibility of doing the entire build without being dependent upon Eclipse being installed though? Thanks!
No, Ant script generated by Eclipse needs Eclipse. You'll probably need to write your own script to avoid eclipse dependency.
Peter Štibraný
Ah. Well, I still might be able to make use of them, given the likely complications I'd need to go through from making a script from scratch. One last question, can eclipse execute an Ant script from the shell? Thanks so much!
Yes, Eclipse can run Ant scripts directly, and even help a little with debugging. Look for Run > External Tools > Ext. Tools Configuration. In the dialog choose Ant Build and configure your script.
Peter Štibraný
Eclipse can run ant scripts from the shell using a command like: eclipse -application org.eclipse.ant.core.antRunner -f build.xmlWhen doing this Eclipse will automatically set up the ant class path to contain all the custom tasks contributed by eclipse plug-ins.
Andrew Niefer
+2  A: 

As far as I know, Eclipse don't issue a shell command to compile or make a JAR file. Instead, it uses the corresponding Java API.

What I use regularly to do that kind of job is ant (http://ant.apache.org).

Here is a really simple build.xml file that takes the java sources in "src" directory, compile them using libraries in the "lib" directory and put the resulting classes in a file "project.jar".

<?xml version="1.0" encoding="UTF-8"?>
<project name="project">
    <target name="package" description="builds the jar file from the compiled classes">
     <mkdir dir="build" />
     <javac srcdir="src" destdir="build">
      <classpath>
       <fileset dir="lib" includes="**/*.jar" />
      </classpath>
     </javac>
     <jar destfile="project.jar">
      <fileset dir="build" includes="**/*.classes" />
     </jar>
    </target>
</project>
rolaf
Eclipse has its own compiler. +1 for sample script though ;-)
Peter Štibraný
Awesome, thanks for the sample! This should work well with what Peter was also saying!
The ant <javac> task will use different compilers depending on the build.compiler property. Setting build.compiler=org.eclipse.jdt.core.JDTCompilerAdapter will use the JDT compiler (assuming it is on the Ant classpath, which it will be by default if you are running ant inside eclipse).
Andrew Niefer
A: 

A really good option is to use Ant4Eclipse (http://ant4eclipse.sourceforge.net/)

This is a set of ant tasks that interrogate the eclipse project metadata and can be used to determine proper project build order, source and output directories and so forth.

You can also map ant targets to eclipse builders (for example, if you have an ANTLR builder setup in eclipse, you can define an ant target that compiles ANTLR code and map it to the name of the ANTLR builder to be triggered)

At my last job I wrote a generic build script using ant4eclipse. I'm going to try to create a new generic build script at some point but haven't had the time.

Scott Stanchfield