How to enable the bans specified in the Java Build Path section of the Eclipse project in a jar file generated with Ant using the build.xml file

basically, my requirement is that if someone adds or removes a link to the jar in the "Java build path" section, then this jar should be enabled (or not enabled if deleted) when creating a project and the jar the project is created using the build file .xml ant. I was expecting a reference to a property or variable in an XML file. sort of:

<!-- copy the JARs that you need to "target" directory --> <copy todir="target"> <fileset dir="${buildPath}" includes="*.jar" /> </copy> 

where $ {buildPath} points to the "Java build path". This is just an example and does not work.

Please note that I do not want to put each jar separately in the build.xml file, but use something like include = "*. Jar", so if someone changes the "Java Build Path", I do not need to change the build file .xml

+4
source share
3 answers

The Java build path in eclipse is actually supported by the .classpath file in your eclipse project. Therefore, if you look in this file, you will see all the paths used by eclipse. You can analyze them with ant. For example, I have a simple project that has a Java build path that points to standard JRE libraries and a link to a jar from another project (dataset tools). This is my .classpath :

 <?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> <classpathentry kind="lib" path="/dataset-tools/target/dataset-tools.jar"/> <classpathentry kind="output" path="bin"/> </classpath> 

So, as you can see, if you add a new jar to your Java build path, it appears in the .classpath file:

  <classpathentry kind="lib" path="/dataset-tools/target/dataset-tools.jar"/> 

You can use ant to parse this file looking for these classpathentry elements with the form lib . This is the same answer presented here .

+1
source

Information about the Eclipse build path is stored in a .classpath file, and you can possibly parse simple bits from this (entries that point to individual JAR files either in the project workspace or externally with absolute paths), but you will only get this so far - there are other types of entries in the classpath, such as those based on "classpath variables", links to other projects in the workspace, or to other types of containers provided by the Eclipse plugins. For example, I have GGTS plugins (for Groovy and Grails), and my Grails .classpath project .classpath not have explicit JARs, it just says

 <classpathentry kind="con" path="org.grails.ide.eclipse.core.CLASSPATH_CONTAINER"/> 

There is an Ant4Eclipse project that claims to help with this, but the most recent version (“milestone 4”) is from mid-2010, and I don’t know how well it works with later eclipses.

An improved approach could be to declaratively manage your dependencies using Ivy and have both an Ant construct and an Eclipse project (using IvyDE ), from which their libraries are taken. When you want to add or remove a dependency, you simply edit the ivy.xml file, and both Ant and Eclipse will pick up the changes in the next build.

+2
source

Use the xmlproperty task to read .classpath in property values.

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

Then set this 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> 
+2
source

All Articles