Any way to generate ant build.xml file automatically from Eclipse?
Asked Answered
O

7

77

From Eclipse, I found I can easily export an Ant build file for my project. It provides references to 3rd party libraries and some base targets. I'm using it from my global build file. The only thing that bothers me about this, is that if something is modified in the project structure (like adding a new 3rd party library), we have to think (yes that can be hard sometimes!) about regenerating that build.xml file. I'm wondering if anyone here knows a way to have it updated automatically. By "automatically" I mean that it would not be necessary to explicitly ask Eclipse to regenerate it every time it's needed. I don't know what could be the trigger though...

Any thoughts or knowledge on this?

Thanks!

MJ

Oblique answered 15/11, 2010 at 15:33 Comment(4)
Have you tried looking at adding a custom builder step?Touter
I'm not sure exactly how I could go about this. I've looked at project builders. I think there might be something there... I could probably add a builder which would generate the build.xml file once the main "Java Builder" is done. Is this what you call a builder step? I could try this if I had any clue about programmatically calling the export ant buildfile action on the project. I doubt it is even possible.Oblique
Hello M. Joanis, what solution did you finally find? I'm trying to achieve the very same thing, so it would be nice if you could share your findings.Interdiction
Hello DMM, I didn't have, yet, to update that file. So, I didn't bother much longer with finding a way to update it automatically. Given the file structure simplicity, I'd probably write a script generating it (keeping the IDE and the building process decoupled at the same time). I have lots of targets auto generated, but most of them don't matter out of the IDE, so I'm down to 3-4 main apps. Depends on your specific needs...Oblique
H
69

Right-click on an Eclipse project then "Export" then "General" then "Ant build files". I don't think it is possible to customise the output format though.

Hereinbefore answered 15/11, 2010 at 16:32 Comment(4)
Sorry, I added a note on "automatically". Like I said, I know I can easily generate and export such a file. What bothers me is having to tell Eclipse to regenerate it every time it needs to be done. I would be interested in a way to automatically trigger that "export ant buildfile" action on my projects. I see no problem (so far) with the output format.Oblique
Then I don't think it is possible in standard Eclipse. Maybe some plugin permits it though...Hereinbefore
This seems the generate the build.xml for the web projects but not for the ear. Can I auto generate a build.xml for the ear file?Pointsman
At the moment this feature is present in Eclipse Java plugin. You need the Web Tools Platform plugin and its J2EE perspective.Hereinbefore
S
5

I have been trying to do the same myself. What I found was that the "Export Ant Buildfile" gets kicked off in the org.eclipse.ant.internal.ui.datatransfer.AntBuildfileExportPage.java file. This resides in the org.eclipse.ant.ui plugin.

To view the source, use the Plug-in Development perspective and open the Plug-ins view. Then right-click on the org.eclipse.ant.ui plugin and select import as > source project.

My plan is to create a Java program to programmatically kick off the ant buildfile generation and call this in an Ant file every time I build by adding the ant file to the builders of my projects (Right-click preferences on a projet, under the builders tab).

Serpens answered 22/9, 2011 at 15:15 Comment(2)
Looks like a very promising path. You seem to have a good understanding of the Eclipse plugin architecture. Would be nice from you to keep us informed with any progress you make. Thanks!Oblique
Update: Unfortunately, my work didn't give me enough time to implement it. Instead, it became a team ritual that every time we added a new dependency the team would re-export the build files. It's not ideal, but it is self-enforcing because if someone tried to execute a build on the server without exporting the build files it usually broke.Serpens
L
5

I've had the same problem, our work environment is based on Eclipse Java projects, and we needed to build automatically an ANT file so that we could use a continuous integration server (Jenkins, in our case).

We rolled out our own Eclipse Java to Ant tool, which is now available on GitHub:

ant-build-for-java

To use it, call:

java -jar ant-build-for-java.jar <folder with repositories> [<.userlibraries file>]

The first argument is the folder with the repositories. It will search the folder recursively for any .project file. The tool will create a build.xml in the given folder.

Optionally, the second argument can be an exported .userlibraries file, from Eclipse, needed when any of the projects use Eclipse user libraries. The tool was tested only with user libraries using relative paths, it's how we use them in our repo. This implies that JARs and other archives needed by projects are inside an Eclipse project, and referenced from there.

The tool only supports dependencies from other Eclipse projects and from Eclipse user libraries.

Lithosphere answered 29/4, 2015 at 2:15 Comment(0)
P
2

Take a look at the .classpath file in your project, which probably contains most of the information that you want. The easiest option may be to roll your own "build.xml export", i.e. process .classpath into a new build.xml during the build itself, and then call it with an ant subtask.

Parsing a little XML sounds much easier to me than to hook into Eclipse JDT.

Pl answered 19/11, 2010 at 20:59 Comment(2)
I absolutely agree that parsing an XML file is much more appealing than writing an Eclipse plugin or something like that. Your suggestion is interesting, but isn't the .classpath file "a little bit" concise? Mine provides 5 paths and that's it. What's great with the exported Ant build file is that not only it provides a target for building the whole thing, but it also provides correct targets allowing to run JUnit tests. I'll keep that in mind.Oblique
Good point. I would expect that Eclipse also takes the "project nature" into account when determining what targets to generate in the build files. You could check into how Eclipse does generate the build.xml, as it uses ant itself internally. But that takes you back to having to do a "science project" to understand these Eclipse internals. Maybe the Builder steps are really the right option.Pl
F
2

I'm the one who donated the Ant export filter to Eclipse. I added the auto export feature, but only to my personal plug-in eclipse2ant, which I still maintain to coordinate bug fixes.

Unfortunately I have no time to merge it to the official Eclipse builds.

Featherhead answered 20/2, 2014 at 20:38 Comment(0)
M
2

If all you need is the classpath entries, I do something like the following to use the eclipse build path.

<xmlproperty file=".classpath" collapseAttributes="true" delimiter=";" />

Then set that value in the path

<path id="eclipse.classpath">
    <pathelement path="${classpath.classpathentry.path}"/>
</path>


<target name="compile" depends="init">

    <javac srcdir="${src}" destdir="${build}" updatedProperty="compiled">
        <classpath refid="eclipse.classpath"/>
    </javac>
</target>
Millenarianism answered 9/4, 2014 at 16:29 Comment(0)
C
0
  • Select File > Export from main menu (or right click on the project name and select Export > Export…).
  • In the Export dialog, select General > Ant Buildfiles as follows: enter image description here

  • Click Next. In the Generate Ant Buildfilesscreen:

    • Check the project in list.
    • Uncheck the option "Create target to compile project using Eclipse compiler" - because we want to create a build file which is independent of Eclipse.
    • Leave the Name for Ant buildfile as default: build.xml

enter image description here

  • Click Finish, Eclipse will generate the build.xml file under project’s directory as follows:
    enter image description here
  • Double click on the build.xml file to open its content in Ant editor: enter image description here

source

Cwm answered 19/12, 2017 at 1:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.