4
votes

We have very custom ant build files for our projects, which basically do this:

  • Some pre-compilation work (e.g. code generation)
  • Compiling Java code
  • Some post-compilation work (e.g. copying files to the classes directory for inclusion in the jar file; building jar files; copying files to some other place so that Tomcat picks them up)

Currently, our ant integration consists of telling Eclipse to run the "do it all" ant target (which does its own compilation).

This seems iffy because ant is compiling and not Eclipse, and the Eclipse compiler produces better error messages. It's also unclear whether the ant build or the default Java build should run first. And we need to tell the ant target to run basically when anything at all in the project has changed. This leads to ant running the whole machinery on every save, basically. This is slow. (If we haven't edited the input file for the code generator, then we can skip running the code generator, and ant takes a long time even to figure out it doesn't have to do anything.)

Are the suggestions for strategies to follow here?

  • Do we need to split up the ant builder into multiple ant builders, each with their own set of watched resources, some of them running before the Java builder, others running after the Java builder?
  • Should we run ant first, then the Java builder, or vice versa? What should be the refresh settings?
  • Do we want to tell Eclipse to rebuild class files touched by others, or not?

Thanks for any suggestions.

2

2 Answers

7
votes

The best way to delegate the generation job to Eclipse is through the Ant builder. Look here on how to setup a builder.

In your build.xml file create 2 targets: generate-code and cleanup-generated-code.

Then hook them up this way in builder's Targets tab:

Ant Builder Targets configuration

This will allow your project to respond nicely to build events.

The next step is to make Eclipse to compile your generated sources automatically.

For this, in the root of your project create a directory called generated-src. This directory should be empty and you should put it under management of you source control system. Because there will be some generated code, put .xxxignore file into this directory.

Once generated-src directory is in-place, make it your project's source directory ( Source tab in Java Build Path )

Source directory

Last, but not least, you want to kick Eclipse's java compiler, once you've generated your sources. To do that you need to modify the Refresh tab of you Ant builder.

Here is the setting that does it:

Refresh Tab

When you click Specify Resources button, make sure that your whole project directory is selected, like here:

Selected Resources

This way, when anything in your project changes it kicks off the ant builder, which may, or may not, regenerate sources, which would lead to java recompilation.

One last screenshot. Make sure that both build.xml and base directory are relative to your ${workspace_loc} in Builder's Main tab, like here ( basically only use Browse Workspace button ):

Builder's Main Tab

Once this is done, hookup generate-code and cleanup-generated-code to your regular targets in your build.xml that you would call for command-line builds. This way Eclipse and your command line build are doing exactly the same steps for code generation. Moreover your Eclipse build will be fully sensitive to the real sources of your project.

0
votes

You might find that you don't need to run the Ant build for your local development work. You should let Eclipse take care of compiling the code and deploying to a local Tomcat instance. Save the Ant build for when you are deploying to a shared dev environment.

Of course, you will need to invoke something to run the code generation. But I'm guessing you don't need to do that very often. You could create an Eclipse launch configuration to run the specific Ant task to generate the code and add it to you favourites menu. Invoke it manually when you need to.