Creating a config list of classpath jars in ant - java

I have a list of jars in an ant task like this..
<path id="lib.path.id">
<fileset dir="${lib.dir}">
<include name="jar/*.jar"/>
</fileset>
</path>
I want to unroll this into a config file like this..
wrapper.java.classpath.1=../lib/activation.jar
wrapper.java.classpath.2=../lib/bcel.jar
wrapper.java.classpath.3=../lib/c3p0-0.8.4.5.jar
wrapper.java.classpath.4=../lib/cglib-full-2.0.2.jar
....
How can I do this in ant?

As explained in my comment, if you are using Tanuki Service Wrapper for Java, you are not forced to list all your jar in the wrapper.conf, you can simply indicate a path that contains all your JAR files:
wrapper.java.classpath.1=/path/to/lib/*.jar
wrapper.java.classpath.2=/any/other/lib/directory/*.jar
wrapper.java.classpath.3=/a/path/to/one/library/my-library.jar
...

In Ant you can use the pathconvert task in order to convert the path collection to a String. Then you can use it in your config file. It won't be in the exact format you specified, but it will be in a proper classpath format, ready to use for a java command.
<pathconvert targetos="unix" property="wrapper.java.classpath" refid="lib.path.id"/>
To create a properties file use the propertyfile task:
<propertyfile file="my.properties">
<entry key="wrapper.java.classpath" value="${wrapper.java.classpath}"/>
</propertyfile>

Eran hinted the right direction. I am using the ant.library.dir as an example.
<project name="util">
<property name="lib.dir" value="${ant.library.dir}"/>
<target name="gen-property-file" description="">
<path id="lib.path.id">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
<pathconvert pathsep="${line.separator}wrapper.java.classpath.Number="
property="echo.path.compile"
refid="lib.path.id">
</pathconvert>
<echo file="my.properties">wrapper.java.classpath.Number=${echo.path.compile}</echo>
</target>
This snippet procudes an file my.properties:
wrapper.java.classpath.Number=D:\Programme\eclipse-rcp-helios-SR1-win32\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-antlr.jar
wrapper.java.classpath.Number=D:\Programme\eclipse-rcp-helios-SR1-win32\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-apache-bcel.jar
wrapper.java.classpath.Number=D:\Programme\eclipse-rcp-helios-SR1-win32\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-apache-bsf.jar
wrapper.java.classpath.Number=D:\Programme\eclipse-rcp-helios-SR1-win32\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-apache-log4j.jar
wrapper.java.classpath.Number=D:\Programme\eclipse-rcp-helios-SR1-win32\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-apache-oro.jar
wrapper.java.classpath.Number=D:\Programme\eclipse-rcp-helios-SR1-win32\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-apache-regexp.jar
wrapper.java.classpath.Number=D:\Programme\eclipse-rcp-helios-SR1-win32
...
You can replace the .Number and the Basepath manually or with a script.

Related

ant build missing jar from lib

I have a build.xml and I have a path for the classpath that I set to classpathref="compile.classpath" during compile:
<path id="compile.classpath">
<fileset dir="./lib">
<include name="*" />
</fileset>
</path>
The lib folder contains weblogic.jar but when i try to compile the project, i got many errors because of missing the weblogic.jar
If I modify my path to this:
<path id="compile.classpath">
<fileset dir="./lib">
<include name="*" />
</fileset>
<fileset dir="${env.WL_HOME}/wlserver/server/lib">
<include name="weblogic.jar" />
</fileset>
</path>
So I add the weblogic.jar from my local installed weblogic directory, there are no errors, and it's compiled.
I copied the weblogic.jar to my project lib folder from the local installed weblogic folder, so it must be the same weblogic.jar
What should I try? Thank you!
I would do something like this in your build.xml (probably just before you do the compilation will work).
<property name="echo.classpath" refid="compile.classpath"/>
<echo message="compileClasspath - ${echo.classpath}"/>
What you probably need to do is to be quite explicit about where your lib directory is, relative paths are tricky if you have multiple build.xml files, and nested directories and stuff.
What I have done before is to make sure that you explicitly define a property in the right place for your lib directory, and just use that rather than ./
<project basedir=".">
<target name="init">
<property name="local.lib.dir" value="${basedir}/lib">
</target>
<target name="compile" depends="init">
<path id="compile.classpath">
<fileset dir="${local.lib.dir}">
<include name="*" />
</fileset>
</path>
....
</target>
</project>

Exclude jar flom classpath in Ant

How can I exclude jars included in one classpath variable from another classpath variable in Ant? I have a myclasspath1 property that contains all jars I want to include and myclasspath2 with a list of other jar. I want to get classpath3 that includes all jars from classpath1 that are not in classpath2. Here is what I came up with for one jar:
<path id="my.classpath1">
<pathelement path="${myClasspath1}"/>
</path>
<!-- at the end I want to get all jars concatenated into a string -->
<pathconvert pathsep="," property="my.classpath3" refid="my.classpath1">
<mapper type="flatten"/>
<map from="excluded.jar" to="" />
</pathconvert>
How can exclude all jars included in myclasspath2 instead?
I would advise that your classpath management should be explicit as you can make them, for example:
<path id="my.classpath1">
<fileset dir="${lib.dir}">
<includes name="*.jar"/>
<excludes name="foo.jar"/>
</fileset>
</path>
<path id="my.classpath2">
<fileset dir="${lib.dir}">
<includes name="*.jar"/>
<excludes name="bar.jar"/>
</fileset>
</path>
<path id="my.classpath3">
<fileset dir="${lib.dir}">
<includes name="foo.jar"/>
<includes name="bar.jar"/>
</fileset>
</path>
Classpath management is a pain.... The best solution would be to use a dependency manager like apache ivy . There are also ANT tasks available for Maven. Both would download jars automatically from Maven Central and are capable of managing your classpaths for you.

How can I extract a file from a classpath?

I want to add a file to a classpath from a fileset. How can I do this with Ant?
For example, let's say the fileset with id "my.fileset" contains:
dir1/subdir1/file1.jar, dir2/subdir2/file2.jar, dir3/subdir3/file3.jar
I know the name of the jar: "file2.jar". Now, I want to find the full filename and add it to a classpath (I assume this will require to use a regex like ".*file2.*)
I'm not sure I understand the question. Are you trying to dynamically add the file to the classpath, or are you trying to set a classpath in ant? In ant, once you set a property (like a classpath), you cannot change it.
To create a classpath in a normal build.xml do something like this:
<path id="compile.classpath">
<pathelement location="${foo.jar}"/>
<fileset dir="./bin">
<include name="*.jar"/>
</fileset>
<pathelement location="./lib"/>
<fileset dir="./lib">
<include name="**/*.jar"/>
</fileset>
</path>
This will add directories, or every jar in a bin folder, or even every jar in the ./lib sub-tree.

How to include external class files during build in ant

I have a package(with some outdated Java files) which i need to build using ant tool. I have some updated classes for which source is not available. So, I need to add the updated class to the jar during build and skip the outdated Java files from the build. Please let me know how to achieve this.
For example - I dont have the updated source java file for com.org.auth.ABC.Java, but i have the updated class file which i got from the another source ie com.org.auth.ABC.class. During build i need to point to the updated class(com.org.auth.ABC.class) for this class alone and create a new jar.
Please find how i am currently pointing to the class files below.
<target name="xxx.jar" depends="xjc,compile">
<jar destfile="${dist.dir}/xxx.jar">
<fileset dir="${classes.dir}" includes="**/*.class"/>
<fileset dir="${jaxb-classes.dir}" includes="**/*.class"/>
<fileset dir="${jaxb-source.dir}" includes="**/bgm.ser,**/jaxb.properties"/>
</jar>
</target>
If you want to leave some package from compilation you can use excludes attribute of fileset ant tag.
for example:
<fileset dir="src/">
<exclude name="**/dir_name_to_exclude/**" />
</fileset>
In order to include the specified class in compilation you can put the containing folder in your class-path using ant.
<path id="project.class.path">
<pathelement location="lib/"/>
<pathelement path="${java.class.path}/"/>
<pathelement path="path to your class file's base folder"/>
</path>
<javac srcdir="${src.dir}"
destdir="${classes.dir}"
classpathref="project.class.path">
... put source files here..
</javac>
And if you want to include that class-file in your jar then add it to fileset using include tag:
<fileset dir="classfiles">
<include name="your class file name"/>
</fileset>
Hope this helps

ant target that iterates all files in the build path with a given extension

I have a simple syntax for script files of a given extension that translates to java class files that I want to create, each containing a given function that can be invoked by reflection.
I want to write a main function that I can invoke from the ant script that can see the ant build directory, traverse it for all the files that have my extension, and translate each one to a java file that I put not in the bin directory but in the src directory package that holds the file (a script) with my extension.
Is this possible?
Andy
Here is an example of how you could create a .java file in the src directory for every .ext file in the bin directory.
But for code generation, I expect that you would need to implement a custom Ant task.
<project default="test">
<target name="test">
<copy todir="src">
<fileset dir="bin">
<include name="**/*.ext"/>
</fileset>
<globmapper from="*.ext" to="*.java"/>
<filterchain>
<!--
You will do some custom filtering
to create your new Java src file.
This is just for illustration.
-->
<headfilter lines="1"/>
<tokenfilter>
<replaceregex pattern=".*" replace="your new content"/>
</tokenfilter>
</filterchain>
</copy>
</target>
<!-- use with care! -->
<target name="clean">
<delete dir="src">
<include name="**/*.java"/>
</delete>
</target>
</project>
Try the ant-contrib project. It has a task which takes a fileset. You could then do whatever you want with it:
<ac:for param="file">
<fileset ... />
<sequential>
<echo message="#{file}" />
</sequential>
</ac:for>
Caveat: I'm the project owner of this. It does not yet take ant 1.7/1.8 constructs (ie. resource collections), but that effort is underway.

Categories