Exclude jar flom classpath in Ant - java

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.

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>

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

build.xml javac is complaining about missing classes from javax.servlet.http during compliation

I've written a build.xml (Updated 8:49 PM) for a netbeans project. And I've found that the tomcat libraries such as serlvet-api.jar are in the directory C:\apache-tomcat-7.0.35\lib. But I'm not sure how I'm meant to connect the target="class_compile" using the fieldset dir to the tomcat directory (without changing the build.xml each time I wish to compile from another computer).
I've read the question error while including external JARs in ant script, with the solution being a missing classpathref attribute within the javac element (although my classpathref attribute seems to be correct).
<path id="build.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="class_compile" depends="prepare" description="Compile the whole project">
<javac destdir="${build.classes}"
debug="${debug}"
deprecation="on"
optimize="off"
srcdir="${src.dir}"
classpathref="build.classpath"
includes="*/**"
/>
<copy todir="${build.classes}">
<fileset dir="${src.dir}" includes="**/*.properties"/>
</copy>
</target>
<target name="prepare">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes}"/>
<path id="run.classpath" >
<pathelement location="${build.classes}" />
</path>
<mkdir dir="${build.lib}"/>
<mkdir dir="${qa.dir}"/>
</target>
Currently, once the class_compile target is executed multiple errors regarding missing class files are reported..
emma:
Created dir: C:\capstonegroup3\TTTserver\build\emma-instr
Created dir: C:\capstonegroup3\TTTserver\build\emma-reports
prepare:
Created dir: C:\capstonegroup3\TTTserver\build\classes
Created dir: C:\capstonegroup3\TTTserver\build\lib
Created dir: C:\capstonegroup3\TTTserver\build\qa-reports
class_compile:
C:\capstonegroup3\TTTserver\build.xml:152: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
Compiling 21 source files to C:\capstonegroup3\TTTserver\build\classes
C:\capstonegroup3\TTTserver\src\java\AuthServer\AuthenticationInterface.java:8: error: package javax.servlet.http does not exist
import javax.servlet.http.HttpServletRequest;
Is there any way to set a property to the tomcat apache/lib directory, from persay a .property file? I saw in my build_impl.xml (generated by netbeans, that has a property file included during the -init-private target).
<target depends="-pre-init" name="-init-private">
<property file="nbproject/private/private.properties"/>
</target>
But I'm unsure how to gain access to those properties for my build.xml. But basically I'm after a solution that generates a relative path to the apache-tomcat\lib directory, and successfully compiles the class files without missing packages.
You should declare the servlet-api.jar file to build.classpath as below
<path id="build.classpath">
<fileset dir="C:/apache-tomcat-7.0.35/lib">
<include name="servlet-api.jar" />
</fileset>
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
What I like to do is keep a local.properties file that describes my local environment in the parent folder of the project folder (e.g. under NetBeansProjects), e.g.:
NetBeansProjects
+- local.properties
+- MyProject
+- build.xml
+- <other stuff>
The build.xml is like:
tomcat.home=/C:/java/tomcat
gwt.dir=/C:/java/google/gwt-2.5.1
This file is not kept under version control and allows each developer to configure his/her own environment. It is read by Ant using something like:
<property file="../local.properties" />
And used e.g. as (this is only an example, adjust for proper usage):
<path id="project.classpath">
<pathelement path="war/WEB-INF/classes" />
<fileset dir="war/WEB-INF/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${tomcat.home}/lib"><!-- tomcat.home defined in local.properties -->
<include name="**/*.jar" />
</fileset>
</path>

Exclude some JARs from Web-Inf/Lib Folder

I'm starting with Ant. I created a build.xml to generate a WAR file of a Web Project and it worked OK.
Then, I made some change to exclude all *.jar from WEB-INF/lib Folder and also works OK.
Now I need to make the changes to exclude all JARs files, but leave some especial JARs in WEB-INF/lib Folder. This JARs are from other project created by me.
The idea es exclude all third parties JARs and only leave my own JARs inside WEB-INF/lib folder.
There is some way to do that?
All my Jars start with "fnet" so maybe I can use that to create some rule, but I don't know how to do that
This is my Build.xml:
<?xml version="1.0" ?>
<project name="warConLibs" default="build-war">
<target name="clean">
<delete file="c:/projweb.war"/>
<delete file="c:/projweb_sl.war"/>
</target>
<target name="build-war">
<war destfile="c:/projweb.war" webxml="./WebContent/WEB-INF/web.xml">
<fileset dir="./WebContent">
<include name="**/*.*"/>
</fileset>
<classes dir="./bin"/>
</war>
</target>
<target name="build-war-sin-libs">
<war destfile="c:/projweb_sl.war" webxml="./WebContent/WEB-INF/web.xml">
<fileset dir="./WebContent">
<include name="**/*.*"/>
<exclude name="**/*.jar"/>
</fileset>
<classes dir="./bin"/>
</war>
</target>
</project>
The correct way to exclude a jar file is given in the documentation. If anyone face same issue, they can refer to this link.
This example is taken from the documentation, here we are removing jdbc1.jar from lib
Assume the following structure in the project's base directory:
thirdparty/libs/jdbc1.jar
thirdparty/libs/jdbc2.jar
build/main/com/myco/myapp/Servlet.class
src/metadata/myapp.xml
src/html/myapp/index.html
src/jsp/myapp/front.jsp
src/graphics/images/gifs/small/logo.gif
src/graphics/images/gifs/large/logo.gif
then the war file myapp.war created with
<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
<fileset dir="src/html/myapp"/>
<fileset dir="src/jsp/myapp"/>
<lib dir="thirdparty/libs">
<exclude name="jdbc1.jar"/>
</lib>
<classes dir="build/main"/>
<zipfileset dir="src/graphics/images/gifs"
prefix="images"/>
</war>
will consist of
WEB-INF/web.xml
WEB-INF/lib/jdbc2.jar
WEB-INF/classes/com/myco/myapp/Servlet.class
META-INF/MANIFEST.MF
index.html
front.jsp
images/small/logo.gif
images/large/logo.gif
You may want to read again about the war Ant task: https://ant.apache.org/manual/Tasks/war.html
The correct syntax would be:
<war destfile="..." webxml="...">
<lib dir="WebContent/WEB-INF/lib">
<include name="fnet*.jar"/>
</lib>
<classes dir="bin"/>
</war>

Creating a config list of classpath jars in ant

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.

Categories