Strategy for using ant built from Eclipse

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

  • Some pre-compilations (e.g. code generation)
  • Compiling Java Code
  • Some work after compilation (for example, copying files to a class directory for inclusion in a jar file, creating jar files, copying files to another location so that Tomcat picks them up)

At present, our ant integration is to tell Eclipse when the ant-one (do-it-yourself) compilation goal is triggered.

It looks like ant is compiling, not Eclipse, and the Eclipse compiler generates better error messages. It is also unclear whether to run the ant or Java construct by default. And we need to say that the ant target is triggered mainly when something at all in the project has changed. This leads to the fact that ant controls the whole machine at every save, basically. It is slow. (If we did not edit the input file for the code generator, then we can skip starting the code generator, and ant takes a lot of time, even to understand that it should not do anything.)

Are strategies proposed for strategies?

  • Is it necessary to split the ant constructor into several ant constructors, each of which has its own set of observable resources, some of which work before the Java constructor, and others work after the Java constructor?
  • Should we run ant first and then the Java constructor or vice versa? What should be the update settings?
  • Do we want to tell Eclipse to rebuild class files affected by others or not?

Thanks for any suggestions.

+4
source share
2 answers

The best way to delegate an Eclipse generation job is through the Ant constructor. Take a look here on how to set up a builder.

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

Then connect them this way in the builder Targets tab:

Ant Builder Targets configuration

This will allow your project to respond well to events.

The next step is to get Eclipse to automatically generate your generated sources.

To do this, create a directory named generated-src in the root of your project. This directory must be empty and you must place it under the control of your version control system. Because there will be some generated code, put the .xxxignore file in this directory.

Once the generated-src directory is in place, make it the project’s source directory (Source tab in the Java build path)

Source directory

Last but not least, you want to run the Eclipse java compiler once you have created your sources. To do this, you need to change the Refresh tab for you Ant builder.

Here is the setup that does this:

Refresh tab

When you click the Specify Resources button, make sure that the entire project directory is selected, for example:

Selected resources

Thus, when something in your project changes, it launches the Ant constructor, which may or may not restore the sources, which will lead to recompilation of Java.

Last screenshot. Make sure that both build.xml and the base directory refer to your $ {workspace_loc} in the Builder Main tab, for example here (basically use the Browse Workspace button):

Builder's Main Tab

Once this is done, connect generate-code and cleanup-generated-code to your usual goals in build.xml , which you invoke to build the command line. In this way, Eclipse and your command line assembly follow exactly the same steps to generate the code. In addition, your Eclipse build will be completely sensitive to the real sources of your project.

+7
source

You may find that you do not need to run the Ant construct for your local development. You must let Eclipse take care of compiling the code and deploying it to a local Tomcat instance. Save the Ant build for deployment in a common development environment.

Of course, you will need to call something to start the code generation. But I assume that you do not need to do this very often. You can create an Eclipse launch configuration to run a specific Ant task to generate code and add it to your favorite menus. Call it manually when you need to.

0
source

Source: https://habr.com/ru/post/1337470/


All Articles