Common jar task in ant build script - ant

I have a bunch of ant projects that build plug-ins that all conform to the same API. The plug-ins incorporate the third-party libraries that they depend on so that they are self-contained. As much of the behaviour in the individual build scripts was similar, I decided to pull out the common parts to a common build script.
The original build scripts looked something like this:
ProjectA/build.xml:
<project name="ProjectA" basedir=".">
...
<target name="jar">
<jar destfile="${project.target}" manifest="manifest.mf">
<fileset dir="${project.build.bin.dir}" />
<zipfileset src="externlib1.jar" />
<zipfileset src="externlib2.jar" />
</jar>
</target>
...
</project>
ProjectB/build.xml:
<project name="ProjectB" basedir=".">
...
<target name="jar">
<jar destfile="${project.target}" manifest="manifest.mf">
<fileset dir="${project.build.bin.dir}" />
<zipfileset src="externlib2.jar" />
<zipfileset src="externlib3.jar" />
</jar>
</target>
...
</project>
Here is what my build scripts look like after refactoring:
Common.xml:
<project name="Common" basedir=".">
...
<target name="jar">
<jar destfile="${project.target}" manifest="common-manifest.mf">
<fileset dir="${project.build.bin.dir}" />
<zipfileset refid="extern.libs" />
</jar>
</target>
...
</project>
ProjectA/build.xml:
<project name="ProjectA" basedir=".">
...
<zipfileset id="extern.libs">
<file file="externlib1.jar" />
<file file="externlib2.jar" />
</zipfileset>
...
<import file="../common.xml" />
</project>
ProjectB/build.xml:
<project name="ProjectB" basedir=".">
...
<zipfileset id="extern.libs">
<file file="externlib2.jar" />
<file file="externlib3.jar" />
</zipfileset>
...
<import file="../common.xml" />
</project>
However, the refactored build does not work--I believe that the problem is that I cannot declare a zipfileset that has multiple files.
I cannot figure out a way that I can declare a fileset such that the behaviour with the common jar task is the same as the behaviour where the jar tasks are declared in each project's build script. Has anyone solved this problem before? Is there a different way I can accomplish the same thing?

It's a bit fiddly, but the zipgroupfileset of the jar task may help.
Something like this for ProjectA should work. I've guessed the directory, adjust as appropriate.
<fileset dir="${project.build.lib.dir}" id="extern.libs">
<include name="externlib1.jar" />
<include name="externlib2.jar" />
</fileset>
Then in the common.xml file, I've renamed your common-manifest.mf.
<target name="jar">
<jar destfile="${project.target}" duplicate="preserve" manifest="common-manifest.mf">
<zipgroupfileset refid="extern.libs"/>
</jar>
</target>

Related

how to excludes and include task in ant script

I want to copy only specific jar file using ant script. It is very easy task but i am not able to do this. I have written the below ant script for this task. I want to excludes all jar file except aopalliance-.jar file. But when i run this script it excludes all the jar file. can someone correct this script .
<?xml version="1.0" encoding="UTF-8"?>
<project default="mvcexample" name="MVCExample" basedir=".">
<property name="web.dir" value="WebContent" />
<property name="webinf.dir" value="WebContent/WEB-INF" />
<property name="lib.dir" value="WebContent/WEB-INF/lib" />
<property name="lib2.dir" value="WebContent/WEB-INF/lib2" />
<target name="copyjar">
<copy todir="${lib2.dir}">
<fileset dir="${lib.dir}" excludes="**/*.jar">
<include name="${lib.dir}/aopalliance-.jar"></include>
</fileset>
</copy>
</target>
</project>
Here's what you need to do:
<target name="copyjar">
<copy todir="${lib2.dir}">
<fileset dir="${lib.dir}">
<!-- <include name="${lib.dir}/aopalliance-.jar"/> -->
<include name="aopalliance-.jar"/> <!-- Don't put the dir name! -->
</fileset>
</copy>
</target>
When you specify a directory, you merely specify the name pattern under that directory. What you were asking for was the jar ${lib.dir}/${lib.dir}/aopalliance-.jar. By the way, is there suppose to be a version number in there somewhere? Like aopalliance-3.4.jar? If so, you need <include name="aopalliance-*.jar"/>.
Also notice that if I used <include .../> instead of <include...></include>.

Ant With Internal Dependencies

I have a a jar right now that uses external dependencies. I'm trying to create a jar that packages all the external dependencies inside, and will just give me one jar. I saw this question asked multiple times, but I still can't figure it out. I'm using Ant, and copied some of the examples I saw on here. I'm using zipgroupfileset to reference the external(now internal) jars. As soon as I added the zipgroupfileset I got a runtime error that said my Runner class could not be found.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. -->
<project basedir="." default="build" name="ExcelDemo">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="../../../../Program Files (x86)/eclipse"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.6"/>
<property name="source" value="1.6"/>
<property name="external-lib-dir" value="lib\poi-3.9" />
<property name="external-lib-dir2" value="lib\poi-3.9\lib" />
<property name="external-lib-dir3" value="lib\poi-3.9\ooxml-lib" />
<path id="ExcelDemo.classpath">
<pathelement location="bin"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src" excludes="**/*.launch, **/*.java"/>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="ExcelDemo.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects">
<ant antfile="${ExcelSensitize.location}/build.xml" inheritAll="false" target="clean"/>
<ant antfile="${ExcelSensitize.location}/build.xml" inheritAll="false" target="build">
<propertyset>
<propertyref name="build.compiler"/>
</propertyset>
</ant>
</target>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
<target name="RunnerClass">
<java classname="runner.RunnerClass" failonerror="true" fork="yes">
<classpath refid="ExcelDemo.classpath"/>
</java>
</target>
<target name="jar" description="Create a jar for this project">
<manifestclasspath property="lib.list" jarfile="Test.jar">
<classpath refid="ExcelDemo.classpath" />
</manifestclasspath>
<jar jarfile="Test.jar" includes="*.class" basedir="bin">
<zipgroupfileset dir="${external-lib-dir}" includes="*.jar"/>
<zipgroupfileset dir="${external-lib-dir2}" includes="*.jar"/>
<zipgroupfileset dir="${external-lib-dir3}" includes="*.jar"/>
<manifest>
<attribute name="Class-Path" value="${lib.list}" />
<attribute name="Main-Class" value="runner.RunnerClass" />
</manifest>
</jar>
</target>
</project>
To make things simpler:
Create a separate sources jar for compilation. Then, have a separate compiled jar without the sources.
Don't include the third party jars. Instead, use Ivy with Ant. Ant will automatically download the required jars. In fact, I've see sources that just include the ivy.jar, so Ivy will automatically be configured when you unjar the sources. You type in ant, and everything just builds.
As an alternative, you can look at Maven which is how many projects are now packaged. In fact, if your jar is an open source project, you can probably host it on the OSS Maven repository. This way, no one even needs to manually download your compiled jar. If they want it, they configure their Maven project to do it for them.
i think the problem is that you use basedir="bin" in the your jar task. then path of your zipgroupfileset convert to bin/${external-lib-dir}

ANT Generated jar: is it a namespace issue?

I have a Eclipse-Java-Project with an ANT-build-file. This build file exports a jar of the project without compiling it. So I only export the sources.
<target name="jar">
<mkdir dir="/jar"/>
<jar destfile="/jar/my_test_jarfile.jar" basedir="/src" />
</target>
I use this generated jar in another eclipse java project and set the path to the jar in the build-path-settings of the project. The problem is that eclipse says it cannot resolve the namespace of the imported classes of the jar.
If I export the jar manually by right clicking on the project and then "Export" and putting the jar to the build path of the other project, everything works fine and there are no errors. So the question is now, what am I doing wrong?
So here is my solution. It seems that you have to compile the source first and then pack it into a jar. I don't give a guarantee that this jar is exactly the same like the one you get from eclipse when you do the right click thing and export etc.
But it works for me, there are no namespace errors any longer. so here is a minimum version of my ant targets:
<project default="run" basedir=".">
<property name="src.dir" value="src" />
<property name="classes.dir" value="bin" />
<property name="build.dir" value="build" />
<path id="libs">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<pathelement path="${basedir}\${classes.dir}"/>
</path>
<target name="run">
<antcall target="compile"/>
<antcall target="jar"/>
</target>
<target name="compile">
<javac debug="true" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="libs" encoding="UTF-8" />
</target>
<target name="jar">
<jar destfile="${build.dir}/my_jar_file.jar" basedir="${classes.dir}">
</target>
</project>

Javadoc errors when building Ant project

I am trying to write a build.xml file for my project. When I run build.xml as an Ant project, I get the following error:
D:\workspace\LogAlerter\src\com\j32bit\alerter\launcher\LogAlerter.java:9:
error: package org.apache.log4j does not exist
[javadoc] import org.apache.log4j.Logger;
I have imported log4j in LogAlerter.Java. Here is my build.xml file:
<?xml version="1.0"?>
<project name="LogAlerter" default="main" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="src" />
<property name="build.dir" location="build" />
<property name="dist.dir" location="dist" />
<property name="docs.dir" location="docs" />
<property name="libs.dir" location="lib" />
<!--
Create a classpath container which can be later used in the ant task
-->
<path id="build.classpath">
<fileset dir="${libs.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${docs.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Creates the build, docs and dist directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${docs.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir" >
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" includeantruntime="false">
</javac>
</target>
<!-- Creates Javadoc -->
<target name="docs" depends="compile">
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<!-- Define which files / directory should get included, we include all -->
<packageset dir="${src.dir}" defaultexcludes="yes">
<include name="**" />
</packageset>
</javadoc>
</target>
<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
<jar destfile="${dist.dir}\LogAlerter.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="LogAlerter.Main" />
</manifest>
</jar>
</target>
<target name="main" depends="compile, jar, docs">
<description>Main target</description>
</target>
</project>
Try adding a classpath ref to your javadoc task:
<javadoc packagenames="src"
sourcepath="${src.dir}"
destdir="${docs.dir}"
classpathref="build.classpath">
What the warning is telling you is that you've not provided the full classpath to the javadoc task. Try adding a similar classpath ref to that in your compile task and see where that leads.
Importing is fine but make sure it is available at run time for the JavaDoc tool. log4j.jar should be present in your build.classpath.
Make use of the classpathref inside the docs target like so:
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}" classpathref="build.classpath">

How to parameterize a path in ANT?

I have the following defined in a file called build-dependencies.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="build-dependencies">
...
<path id="common-jars">
<fileset file="artifacts/project-1/jar/some*.jar" />
<fileset file="artifacts/project-2/jar/someother*.jar" />
</path>
...
</project>
I include it at the top of my build.xml file. Now I need to make the artifacts folder a parameter so it can be changed during execution of different targets.
Having this...
<?xml version="1.0" encoding="UTF-8"?>
<project name="build-dependencies">
...
<path id="common-jars">
<fileset file="${artifacts}/project-1/jar/some*.jar" />
<fileset file="${artifacts}/project-2/jar/someother*.jar" />
</path>
...
</project>
...and defining an "artifacts" property (and changing it) in the target does not work because it seems that the property substitution happens when the path is defined in build-dependencies.xml
How can I solve this? One way I was thinking was to have a parameterized macro and call that before the path is actually used, but that seems not elegant. Something like this:
<macrodef name="create-common-jars">
<attribute name="artifacts"/>
<sequential>
<path id="common-jars">
<fileset file="#{artifacts}/project-1/jar/some*.jar" />
<fileset file="#{artifacts}/project-2/jar/someother*.jar" />
</path>
</sequential>
</macrodef>
EDIT: Ivy and command line parameters are not an option.
You don't want a parameterized path. You want a PatternSet. You can define the patternset at the top-level and then just refer to it in individual targets when you need it. For your example:
<?xml version="1.0" encoding="UTF-8"?>
<project name="build-dependencies">
...
<patternset id="common-jars">
<include name="project-1/jar/some*.jar" />
<include name="project-2/jar/someother*.jar" />
</patternset>
...
<path id="instrumented-jars">
<fileset dir="instrumented">
<patternset refid="common-jars" />
</fileset>
</path>
...
<path id="standard-jars">
<fileset dir="not-instrumented">
<patternset refid="common-jars" />
</fileset>
</path>
...
</project>
I'd recommend using ivy to manage your classpath dependencies. Ivy has a neat concept called configurations that allows you to group collections of artifacts based on their usage.
Here's an adaption from one of my own build files:
<target name="retrieve" description="3rd party dependencies">
<ivy:resolve/>
<ivy:cachepath pathid="build.path" conf="build"/>
<ivy:cachepath pathid="runtime.path" conf="runtime"/>
</target>
The configurations are managed in the ivy.xml file (Would replace your build-dependencies.xml file)
<ivy-module version="2.0">
<info organisation="com.myspotontheweb" module="HelloWorld"/>
<configurations>
<conf name="build" description="jars needed for build" />
<conf name="runtime" extends="build" description="jars needed at runtime" />
</configurations>
<dependencies>
<dependency org="org1" name="project1" rev="1.0" conf="build->default"/>
<dependency org="org2" name="project2" rev="1.0" conf="build->default"/>
<dependency org="org3" name="project3" rev="1.0" conf="runtime->default"/>
<dependency org="org4" name="project4" rev="1.0" conf="runtime->default"/>
</dependencies>
</ivy-module>
The jar artifacts associated with each project would be downloaded and cached automatically from the on-line maven repositories or you can create your own local repository to hold collections of locally owned artifacts.
Lets call your file build.xml. So you execute it by running ant command. In the first case the artifacts names is hardcoded in the property defined on the third line below
<?xml version="1.0" encoding="UTF-8"?>
<project name="build-dependencies">
<property name="artifacts" value="first-value" />
...
<path id="common-jars">
<fileset file="artifacts/project-1/jar/some*.jar" />
<fileset file="artifacts/project-2/jar/someother*.jar" />
</path>
...
</project>
Now when you want to change it and use another value for that artifacts property, we run the script thus
ant -Dartifacts=new-value
This will override the hardcoded artifacts value in build.xml
If working in terms of ant targets you can do something similar, in the target on first line define the property, and if you want to overwrite the default value then pass the property as a parameter when that target is called.
Your comment reminded me of something else. Have your developers create a artifacts-dir-name.xml file. It will have only one line:
<?xml version="1.0" encoding="UTF-8"?>
<project name="artifacts-file">
<property name="artifacts" value="new-value" />
</project>
Now in your build.xml file, before the line where artifacts property is defined, import that file thus:
<import file="artifacts-dir-name.xml" optional="true" />
Now in Eclipse if this file exists, then the property is read from it and artifacts is set to "new-value", else the property is read from build.xml and is set to "first-value". All the developers need to do is to ensure artifacts-dir-name.xml file exists in that directory. This can run within Eclipse too.
is using environment variables an option (if they are set when eclipse is launched they will be picked up)? If so, have each one set ARTIFACTS and this should work:
<?xml version="1.0" encoding="UTF-8"?>
<project name="build-dependencies">
<property environment="env"/>
<path id="common-jars">
<fileset file="${env.ARTIFACTS}/project-1/jar/some*.jar" />
<fileset file="${env.ARTIFACTS}/project-2/jar/someother*.jar" />
</path>
</project>
OK, I think there is no other obvious way for me to do what I am trying to do, except use a macro that takes a parameter and creates the path with the appropriate artifacts folder.
To give a bit of context, why I was trying to what I wanted is to have "instrumented" and "not-instrumented" artifacts in separate folders. And in my "targets" I could just vary the artifacts mode. So what I do now is I have a macro: <initialise-build-settings artifacts-mode="instrumented" /> that sets up all the paths and other variables.
Thanks for your answers guys.
You can do this with different dependencies:
setpath.xml:
<project name="setpath">
<target name="setCommonJars">
<path id="common-jars">
<fileset file="${param1}/some*.jar" />
<fileset file="${param1}/someother*.jar" />
</path>
</target>
</project>
build.xml:
<project name="Test path" basedir=".">
<import file="./setpath.xml" />
<target name="buildT1" depends="setT1,setCommonJars">
<property name="jar-str" refid="common-jars" />
<echo message="buildT1: ${jar-str}" />
</target>
<target name="buildT2" depends="setT2,setCommonJars">
<property name="jar-str" refid="common-jars" />
<echo message="buildT2: ${jar-str}" />
</target>
<target name="setT1">
<property name="param1" value="t1" />
</target>
<target name="setT2">
<property name="param1" value="t2" />
</target>
</project>
If you call target buildT1 then the t1 directory will be used, if you call buildT2 then the t2 directory will be used.

Resources