Ant2IDE - IDE project file generation from Ant build.xml

2008-11-19 10:07

Ant2IDE generates Eclipse (or other IDE-specific) project files with proper classpath and source folder settings from Ant build.xml automatically.

I wrote ant2eclipse long time ago with my crude shell script skill to generate Eclipse project files from an Ant build.xml file.

In Ant2IDE, I extended the idea so that it works in a more elegant way using the BuildListener interface. The usage is pretty simple:

  1. Add ant2ide.jar to the classpath:
    export CLASSPATH="/usr/local/libexec/ant2ide.jar:$CLASSPATH"
  2. Change the working directory to the project home:
    cd /home/trustin/workspace/myAntProject
  3. Clean the build
    ant clean
  4. Run ant with the -listener option and appropriate task(s):
    ant -listener net.gleamynode.ant2ide.EclipseProjectGenerator compiile-main compile-test

You might prefer to use the following simple shell script:

#!/bin/sh
# Path: /usr/local/bin/ant2eclipse
export CLASSPATH="$CLASSPATH:/usr/local/libexec/ant2ide/ant2ide.jar"
ant -listener net.gleamynode.ant2ide.EclipseProjectGenerator "$@"

How it works

How does Ant2IDE generate the Eclipse project files? It listens to the build events fired by Ant and records the source and destination directory of the javac tasks into its internal data structure. Once build is completed successfully, Ant2IDE processes the recorded information to generate IDE-specific project files. It’s very simple but it works much better than analyzing build.xml directly or creating a new Java project from an Eclipse workbench.

Caveats

Of course, Ant2IDE is not a silver bullet – there’s some limitation in this approach.

First, you can’t get the correct build information unless you provide a proper Ant target. For example, clean target will never help AntIDE generate the project files. You should specify the target that compiles all source code, such as build-all or all, test-all.

Second, it doesn’t add all resource directories automatically. You have to add them manually for most cases. There’s no way to determine exactly whether a copy task copies resource files for now. One exception is when your project has a directory layout which is similar to that of Maven 2 project; Ant2IDE looks for a resources directory when the name of the source code directory is java, and add the resources directory to the source path automatically.

Third, Eclipse JDT is the only IDE that Ant2IDE supports at this moment. It’s designed to support other IDEs such as NetBeans and IntelliJ, but I don’t use them these days. Any contribution would be great.

Changelog

  • Nov 19, 2008 – Added auto-detection of resources directory
  • Nov 14, 2008 – Initial implementation
---

Comment

10 Comments

Thanks for sharing.
It would be useful, and moreover the use of BuildListener get some ideas to my mind for a kick starting project…

Stefano Maestri · 2008-10-14 18:51 · # · Reply

@Stefano Maestri: You’re welcome. Good luck for the new project! :)

Trustin Lee · 2008-10-14 19:10 · # · Reply

Really a nice idea. What about not only extracting information from <javac> but also from <jar><war><ear>. Especially if <jar> contains a <manifest> for specifying a main-class there could be some valuable information.

Jan Matèrne · 2008-10-16 19:21 · # · Reply

Have just tried ant2ide to assist with a migrating a client codebase from IntelliJ to Eclipse…

EclipseProjectGenerator doesn’t close the project description tag – line 67 needs to be:
buf.append(”</projectDescription>”);

Robin Bramley · 2008-10-18 18:39 · # · Reply

@Robin Bramley: Oops. Thanks for the bug report. I’ve just uploaded the updated JAR.

Trustin Lee · 2008-10-18 20:13 · # · Reply

@Jan Matèrne What information would you extract from those tasks?

Trustin Lee · 2008-10-18 20:14 · # · Reply

You can do the opposite, get Ant to use the same classpath as indicated in your Eclipse .classpath file by using Ant4Eclipse (http://ant4eclipse.sourceforge.net/)

Galder Zamarreno · 2008-10-30 19:46 · # · Reply

@Galder Zamarreno: The point of this idea is to avoid modifying the build.xml

Trustin Lee · 2008-10-30 21:43 · # · Reply

Very nice.
Good job.

Martin · 2008-11-09 04:18 · # · Reply

@Martin: Thanks!

Trustin Lee · 2008-11-19 10:11 · # · Reply

 
  • Preview 버튼 누르고 reCAPTCHA 입력 후 Submit 버튼까지 눌러야 실제로 게시됩니다.
  • Make sure to answer the reCAPTCHA and click the Submit button to get your comment posted. It's not enough to click the Preview button only! -- See why.
---