How to set Eclipse build path and class path from Ant build file?

There is a lot of discussion about Ant and Eclipse, but the answers have not answered me before.

Here's the deal: I'm trying to create a Java program that compiles successfully using Ant from the command line. (To make things even more confusing, the program I'm trying to compile is Ant itself.)

What I really want to do is bring this project to Eclipse and compile it into Eclipse so that type bindings and variable bindings (nomenclature from Eclipse JDT) are resolved correctly. I need this because I need to run static code analysis that is built on top of the Eclipse JDT. I usually bring the Java project to Eclipse so that Eclipse builds it and resolves all the bindings - just import the source directories into the Java project and then tell it to use the src / main / directory as the "source directory". "

Unfortunately, running with Ant leads to a build failure with numerous compilation errors. It seems to me that the Ant build file correctly sets the path to the path and the build path (possibly excluding certain source files), and Eclipse does not have this information.

Is there a way to take the path to the path and build the path of the information embedded in the Ant build file and provide this Eclipse information for placement in the .project and .classpath files? I tried creating a new project from an existing build file (option in the File menu), but that does not help. The project still has the same compilation errors.

Thanks Nels

+7
java eclipse build ant eclipse-jdt
source share
5 answers

I never found a really clean way to do this, but one “hacky” way to do this is to manipulate the .classpath file that eclipse uses (this contains the build path).

So .classpath will have stuff in it like this:

<classpathentry kind="lib" path="C:/jboss-4.2.3.GA/client/jboss-system-client.jar"/> 

So you could, for example, write some kind of batch version of a script, etc. that would read your ant file dependencies and put them in the eclipse.classpath file (in the correct format, of course).

But personally, I’m never deceived by such things. What I am doing is just putting all the jars that I need in the same directory, and then in my ant file I have a path similar to this:

 <path id="all_libs"> <fileset dir="test_reflib"> <include name="**/*.jar"/> </fileset> </path> 

test_reflib needs to be defined only where this folder containing all banks is located.

Then on the eclipse side you can simply add “Add banks” and go to the same folder and just select all banks. What's even colder is that when you drop new jars into this folder, just click on the root level in the eclipse project and do "Update", then edit the build path and click add jar again and it will show you the jar that you have not yet been added to the build path (i.e. the new jar that you just put in the folder).

This obviously does not work too well if you share the jars in a central place, but works well for small projects, where you can simply copy all the jars to a central folder for the project.

+6
source share

I use ivy to manage my ANT classes, I highly recommend learning how it works.

There is an eclipse plugin that will manage the eclipse class paths from the same ivy.xml file that ANT uses to determine its dependencies.

+6
source share

I wrote an Ant task that creates an Eclipse.userlibraries file. You can import the created file to create a custom library in Eclipse. And then use this user library as part of your build path.

To use the task, add this to your Ant build file:

 <target name="createEclipseUserLibraries" description="Creates classpath and bootclasspatch that can be imported into Eclipse"> <taskdef name="createEclipseUserLibraries" classname="com.forumsys.tools.CreateEclipseUserLibraries" classpathref="yourclasspathref"/> <createEclipseUserLibraries classpathref="classpathref" bootclasspathref="bootclasspathref"/> </target> 

Ant Task. Ant.jar is required for execution and compilation:

 import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Reference; /** * A custom tag to create a file the eclipse can import to setup a user libraries. * * Created: Mar 29, 2014 9:44:09 AM * * @author <a href="mailto:jslopez@gmail.com">Javier S. López</a> * @version 1.0 */ public class CreateEclipseUserLibraries extends Task { public static final String UTF8_ENCODING = "UTF-8"; public static final String DEFAULT_BOOT_CLASSPATH_LIBRARY_NAME = "SYSTEM_LIBRARY"; public static final String DEFAULT_CLASSPATH_LIBRARY_NAME = "LIBRARY"; public static final String DEFAULT_DESTINATION = "Eclipse.userlibraries"; private static final String INDENT = " "; private Path _classpath; private Path _bootClasspath; private String _bootClasspathLibraryName = DEFAULT_BOOT_CLASSPATH_LIBRARY_NAME; private String _classpathLibraryName = DEFAULT_CLASSPATH_LIBRARY_NAME; private String _destination = DEFAULT_DESTINATION; public void setClasspath(final Path classpath) { if (_classpath == null) { _classpath = classpath; } else { _classpath.append(classpath); } } public void setClasspathRef(final Reference reference) { if (_classpath == null) { final Project antProject = getProject(); _classpath = new Path(antProject); } _classpath.setRefid(reference); } public void setBootClasspath(final Path bootClasspath) { if (_bootClasspath == null) { _bootClasspath = bootClasspath; } else { _bootClasspath.append(bootClasspath); } } public void setBootClasspathRef(final Reference reference) { if (_bootClasspath == null) { final Project antProject = getProject(); _bootClasspath = new Path(antProject); } _bootClasspath.setRefid(reference); } public void setClasspathLibraryName(final String name) { if (!isEmpty(name)) { _classpathLibraryName = name; } } public void setBootClasspathLibraryName(final String name) { if (!isEmpty(name)) { _bootClasspathLibraryName = name; } } public void setDestination(final String argDestination) { if (!isEmpty(argDestination)) { _destination = argDestination; } } @Override public void execute() throws BuildException { if (_classpath == null) { throw new BuildException("classpath or classpathref attribute must be set"); } if (_bootClasspath == null) { throw new BuildException("bootclasspath or bootclasspathref attribute must be set"); } try { createUserLibrariesFile(); } catch (final IOException e) { throw new BuildException(e.getMessage(), e); } } /** * @throws IOException * */ private void createUserLibrariesFile() throws IOException { final StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("<?final xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"); stringBuilder.append("\n"); stringBuilder.append("<eclipse-userlibraries version=\"2\">").append("\n"); createBootClasspathLibrary(stringBuilder); createClasspathLibrary(stringBuilder); stringBuilder.append("</eclipse-userlibraries>"); final Project antProject = getProject(); final File baseDir = antProject.getBaseDir(); final File file = new File(baseDir, _destination); if (file.exists()) { file.delete(); } final boolean append = false; BufferedOutputStream bos = null; try { final FileOutputStream fos = new FileOutputStream(file, append); bos = new BufferedOutputStream(fos); bos.write(stringBuilder.toString().getBytes(UTF8_ENCODING)); bos.flush(); } finally { if (bos != null) { bos.close(); } } } /** * @param stringBuilder * */ private void createBootClasspathLibrary(final StringBuilder stringBuilder) { createLibrary(stringBuilder, _bootClasspathLibraryName, true, _bootClasspath); } /** * @param stringBuilder */ private void createClasspathLibrary(final StringBuilder stringBuilder) { createLibrary(stringBuilder, _classpathLibraryName, false, _classpath); } /** * @param stringBuilder * @param bootClasspathLibraryName * @param b * @param bootClasspath */ private void createLibrary(final StringBuilder stringBuilder, final String libraryName, final boolean isSystemLibrary, final Path path) { stringBuilder.append(INDENT).append("<library name=\"").append(libraryName); stringBuilder.append("\" systemlibrary=\"").append(Boolean.toString(isSystemLibrary)).append("\">\n"); final String[] paths = path.list(); final Project antProject = getProject(); final File baseDir = antProject.getBaseDir(); final String baseDirName = baseDir.getName(); for (final String strPath : paths) { final int index = strPath.indexOf(baseDirName); //Only include the relative path if (index != -1) { stringBuilder.append(INDENT).append(INDENT); stringBuilder.append("<archive path=\"").append( strPath.substring(index - 1)).append("\"/>\n"); } } stringBuilder.append(INDENT).append("</library>\n"); } public static final boolean isEmpty(final String str) { return (str == null) || (str.length() == 0); } } 
+2
source share

In the original ant distribution, first run "ant -f fetch.xml" (or similar) to load many of the required dependencies. Add them to your Eclipse project and see if it helps.

+1
source share

We created Eclipse.classpath and .project files from Ant for a large project with central banks (100+) (not counting src jars and javadocs). Like build.xml, associated with the obvious addition of src and javadoc attributes here .

+1
source share

All Articles