BND Ant task - wrap non-OSGi jars - ant

I'm trying to use Ant bndwrap task to wrap non-OSGi jars in a directory. My current Ant configuration for this is:
<target name="wrap-jars" description="Wrap non-OSGi jars">
<taskdef resource="aQute/bnd/ant/taskdef.properties" classpath="${biz.aQute:bnd:jar}"/>
<bndwrap output="${dist.dir}/app-modules">
<fileset dir="${dist.dir}/app-modules" includes="*.jar" />
</bndwrap>
<move overwrite="true" todir="${dist.dir}/app-modules" >
<fileset dir="${dist.dir}/app-modules" includes="*.bar" />
<mapper type="glob" from="*.bar" to="*.jar" />
</move>
</target>
This works fine, but the problem is that it also wraps existing OSGi jar, which causes problems. For instance, I noticed it changes Bundle-SymbolicName header to some default value. It might be changing something else, which I don't want. I only want it to operate on jars that have no OSGi info at all.
Is there some way to tell BND to ignore existing OSGi headers in manifest, or complete jars that are already OSGi-fied?

I would store non-OSGi jars in a separate folder and modify the fileset to process only that folder.

I've noticed that recent bnd versions (for example, 2.1.0) now honour the Bundle-SymbolicName when rewrapping OSGi jars.

just change your fileset to exclude that jar

Related

ivy jar located in my dep lib

how can I tell ant to find Ivy's jar in my own lib? ant just kept looking at it's home folder even when I've explicitly told it to find the jar somewhere else.
I would recommend removing the ivy jar from the ANT home directory. (For some very odd reason it's not normally packaged with ANT).
Instead I recommend including a special task to ensure ivy is installed.
<available classname="org.apache.ivy.Main" property="ivy.installed"/>
<target name="install-ivy" description="Install ivy" unless="ivy.installed">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0/ivy-2.4.0.jar"/>
<fail message="Ivy has been installed. Run the build again"/>
</target>
Analysis
The ANT manual outlines the order in which jars a loaded by ANT at startup.
-lib jars in the order specified by the -lib elements on the command line
jars from ${user.home}/.ant/lib (unless -nouserlib is set)
jars from ANT_HOME/lib
This will always happen and unfortunately it won't matter what you do inside your build file.....
Jars in the ANT_HOME/lib
In my opinion, putting jars in the ANT_HOME effectively creates a bespoke installation of ANT. It makes your projects less portable across machines, and the customizations are frequently forgotten and undocumented.
So if you have control over the build server I would recommend removing any ANT tasks your find here.
Jars in the ${user.home}/.ant/lib
Placing jars here is less objectionable for the following reasons
Directory owned by the user running the build
Can be ignored at run-time by by using the commandline option -nouserlib
The only jar I put here is ivy... All other jars exist in the ivy cache (including ANT tasks)
You can place Ivy binaries in some folder inside you project folder. For example, in my case, it's etc/build/. I put where ivy.jar and jsch.jar.
After that provide the correct namespace in project defenfition and load Ivy.
<project name="somename" basedir="." xmlns:ivy="antlib:org.apache.ivy.ant">
<target name="ivy-load">
<path id="ivy.lib.path">
<pathelement location="${basedir}/etc/build/ivy.jar"/>
<pathelement location="${basedir}/etc/build/jsch.jar"/>
</path>
<taskdef resource="org/apache/ivy/ant/antlib.xml" uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>
</target>
<target name="ivy-init" depends="ivy-load">
<ivy:settings file="${basedir}/etc/ivysettings/ivysettings.xml"/>
<ivy:resolve conf="${ivy.conf}"/>
</target>
...
</project>

Download jars from nexus using ant build tool as done automatically in Maven

I have a build.xml(ant based) which requires some jar from nexus to get copied in existing lib folder. i.e when it builds it should copy the jar from nexus with some version defined & then copy in lib & do compilation.
like happen in maven we define the artifact & its version . If changed will automatically download it from maven repo.
how can i do this in ant based builds?
experts pls advice.
I have taken the example listed in this thread one step further and created a macrodef to clean things up a bit for re-use. See below for downloading two artifacts from nexus (one snapshot, one release).
<project>
<target name="get-all">
<mkdir dir="lib" />
<nexus-get
groupId="foo.bar"
artifactId="some-artifact"
version="1.0.28"
repo="releases"
extension="jar"
dest="lib"
/>
<nexus-get
groupId="foo.bar"
artifactId="another-artifact"
version="1.0.0-SNAPSHOT"
repo="snapshots"
extension="jar"
dest="lib"
/>
</target>
<macrodef name="nexus-get">
<attribute name="groupId"/>
<attribute name="artifactId"/>
<attribute name="version"/>
<attribute name="repo"/>
<attribute name="extension"/>
<attribute name="dest"/>
<sequential>
<get src="http://my-nexus:9999/nexus/service/local/artifact/maven/redirect?r=#{repo}&g=#{groupId}&a=#{artifactId}&v=#{version}&e=#{extension}" dest="#{dest}/#{artifactId}.#{extension}" usetimestamp="true" />
</sequential>
</macrodef>
You would probably be interested in Ivy. It is a sub-project of Ant for dependency management. It is perfect for your situation because it can read Maven repositories and provides Ant tasks for downloading the published artifacts, constructing class paths from them, etc. It supports your use case of getting the most recent version of a dependency if you configure it to ask for the "latest.release" revision of the module.
Although there are surely specific ways to combine ant and maven the simplest thing (if you know the nexus URL and your artifact parameters to construct the download URL) would be just to use the ant Get task.
<project name="MyProject" default="resolveDependencies" basedir=".">
<target name="resolveDependencies">
<mkdir dir="lib" />
<get src="http://search.maven.org/remotecontent?filepath=log4j/log4j/1.2.9/log4j-1.2.9.jar" dest="lib/log4j-1.2.9.jar" usetimestamp="true" />
</target>
</project>
Perhaps use the Maven Ant Tasks.
As shown on http://maven.apache.org/ant-tasks/examples/dependencies.html
Can list dependencies in ant, and also do things like copy them
I think the section Using FileSets and the Version Mapper covers your need
You can use is filesetId, which will give you a fileset reference that can be used to copy files into a particular location. For example, to populate WEB-INF/lib with your dependencies you could use the following:
<artifact:dependencies filesetId="dependency.fileset" useScope="runtime">
<dependency groupId="junit" artifactId="junit" version="3.8.2" scope="test"/>
</artifact:dependencies>
<copy todir="${webapp.output}/WEB-INF/lib">
<fileset refid="dependency.fileset" />
<!-- This mapper strips off all leading directory information -->
<mapper type="flatten" />
</copy>

How can I specify the path of a JAR in an ant buildfile?

I am executing lot of scp and sshexec and other remote commands from an ant build script. These commands don't work if jsch.jar isn't in the ant lib directory. To make it work, I copied the JAR into the ant lib directory, but this is not a good solution, as anyone else wanting to run the script would have to do the same thing. To run the ant target from Teamcity, we will have to explicitly set the path of the lib file.
Is there a way I can specify the path of the JAR in the ant build XML itself?
Thanks all for your answers. I am managed to get it work with classloader task. This is what I did.
<project basedir="." >
<property environment="env"/>
<taskdef resource="net/jtools/classloadertask/antlib.xml">
<classpath>
<fileset dir="${basedir}/lib" includes="ant-classloader*.jar"/>
</classpath>
</taskdef>
<!--Add JSCH jar to the classpath-->
<classloader loader="system">
<classpath>
<fileset dir="${basedir}/lib" includes="jsch*.jar"/>
</classpath>
</classloader>
<target name="Test">
<scp todir="user1:pass1#server1:/tmp" trust="true" >
<fileset dir="dir1">
<include name="test.txt" />
</fileset>
</scp>
</target>
</project>
As you can see here, I didn't have to give any dependant target for my "Test" target, it just works. It uses classloader, which appends jsch.jar to the system classloader.
One possible work around would be to use the -lib command line option to tell ant where to look for additional jars. Perhaps you could create a wrapper script that calls ant with this option set.
Another way would be to move the ant-jsch.jar file (this is the jar that comes with ant that defines the tasks, not the jsch.jar file you need to download separately) out of your ant lib directory, and create a taskdef for your ssh task separate to the built in one, then set the classpath for this task to the jsch.jar and the ant-jsch.jar:
<taskdef name="sshexec"
classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec">
<classpath>
<pathelement location="jsch-0.1.44.jar"/>
<pathelement location="ant-jsch.jar" />
</classpath>
</taskdef>
I'm not sure this will help you though, since it also involves making changes to the lib directory.
As far as I'm aware, it's not currently possible to specify the extra jars required for the built in tasks in the build file itself in general. There are some special cases, like junit for instance.
To ensure your build is more cross platform I'd suggest using dependency management. The ivy plug-in can automatically install the version of your build's plugin at build-time.
This approach means the last jar you'll ever need to install into your ANT lib is ivy-2.2.0.jar :-)
First declare your project's dependencies in the file ivy.xml
<ivy-module version="2.0">
<info organisation="com.myspotontheweb" module="demo"/>
<configurations>
<conf name="anttask" description="Jars implementing ANT tasks"/>
</configurations>
<dependencies>
<dependency org="com.jcraft" name="jsch" rev="0.1.42" conf="anttask->default"/>
</dependencies>
</ivy-module>
Within your build.xml run ivy and use it to populate a custom classpath based on the ivy configuration:
<target name='init' description='Resolve project dependencies and set classpaths'>
<ivy:resolve/>
<ivy:cachepath pathid="anttask.path" conf="anttask"/>
</target>
Finally, elsewhere in your build declare your ANT tasks using the class path now automatically populated by ivy.
<target name='dosomething' depends="init">
<taskdef name="sshexec"
classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec"
classpathref="anttask.path"/>
..
..
</target>
This approach works for all ANT plug-ins, most of which are available in the central Maven repository. The second benefit is that it's easy to upgrade the plug-in versions across all builds.

How do I convert an Ant Path into a FileSet?

I'm writing an Ant script to package a project into a WAR file. The software consists of several projects with their own source directories, libraries, etc.
The WAR task has a nested element lib which I'm currently working on. I currently have a reference of the required libs as a Path (containing several FileSets, which I use in a classpath reference. The lib, however, wants the input to be a FileSet, and it refuses a Path.
I tried converting my Path into a FileSet, but then I didn't get it to work as a classpath elsewhere.
Is there a way to convert a Path into a FileSet? I would hate to copy-paste the directories.
<path id="compile.libs">
<fileset dir="${common.path}/lib" includes="*.jar"/>
<fileset dir="${data.path}/lib" includes="*.jar"/>
<fileset dir="${gui.path}/lib" includes="*.jar"/>
<fileset dir="${gui.path}/WebContent/WEB-INF/lib" includes="*.jar"/>
</path>
...when used with <war ..><../> <lib refid="compile.libs"/> </war> leads to:
BUILD FAILED
build.xml:173: compile.libs doesn't denote a zipfileset or a fileset
Assuming the paths are absolute, you can first convert the Path to a comma-delimited list using <pathconvert>, and then convert the list back into a Fileset:
<!-- create path -->
<path id="foo.path">
<pathelement location="/foo/bar/baz.txt"/>
<pathelement location="/qux/quux/quuux.txt"/>
</path>
<!-- convert foo.path to foo.list -->
<pathconvert
refid="foo.path"
property="foo.list"
pathsep=","
dirsep="/"
>
<!--
<fileset> will want relative paths, so we need to strip
the leading /. result: "foo/bar/baz.txt,qux/quux/quuux.txt"
-->
<map from="/" to=""/>
</pathconvert>
<!-- convert foo.list to fileset -->
<fileset id="foo.fileset" dir="/" includes="${foo.list}"/>
(Note the above assumes Unix; you may need to fiddle a bit with separators and whatnot if you're on Windows or you want to make it platform-independent.)
You may have several choices.
You may provide more than one
<lib> nested element to <war>
task. Maybe this would be enough.
You may preassemble all of your
lib files in one temporary
directory and then just reference that
directory as a fileset.
There is an ant-contrib
PathToFileSet task, but it
requires a central root directory,
and this may not be a case with your
compile.libs layout.
Since Ant 1.8.0 you can use a mappedresources. Source: Ant script: Prevent duplication of JAR in javac-classpath war-lib
I think I would try option 1.
I solved this by staging the libs like this :
<copy todir="stage/libs" flatten="true">
<path refid="classpath" />
</copy>
and then using a in the WAR task.simple.
The jars in the classpath used to compile are not the same that needs to be packaged inside the war. For example: I'm sure you need servlet-api.jar to compile your project but you don't need it inside the war because the container provides it. And some jars aren't needed at compile time but at runtime.
I know I'm not answering your question, just want you to think what you are doing.

How can I best share Ant targets between projects?

Is there a well-established way to share Ant targets between projects? I have a solution currently, but it's a bit inelegant. Here's what I'm doing so far.
I've got a file called ivy-tasks.xml hosted on a server on our network. This file contains, among other targets, boilerplate tasks for managing project dependencies with Ivy. For example:
<project name="ant-ivy-tasks" default="init-ivy"
xmlns:ivy="antlib:org.apache.ivy.ant">
...
<target name="ivy-download" unless="skip.ivy.download">
<mkdir dir="${ivy.jar.dir}"/>
<echo message="Installing ivy..."/>
<get src="http://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"
dest="${ivy.jar.file}" usetimestamp="true"/>
</target>
<target name="ivy-init" depends="ivy-download"
description="-> Defines ivy tasks and loads global settings">
<path id="ivy.lib.path">
<fileset dir="${ivy.jar.dir}" includes="*.jar"/>
</path>
<taskdef resource="org/apache/ivy/ant/antlib.xml"
uri="antlib:org.apache.ivy.ant"
classpathref="ivy.lib.path"/>
<ivy:settings url="http://myserver/ivy/settings/ivysettings-user.xml"/>
</target>
...
</project>
The reason this file is hosted is because I don't want to:
Check the file into every project that needs it - this will result in duplication, making maintaining the targets harder.
Have my build.xml depend on checking out a project from source control - this will make the build have more XML at the top-level just to access the file.
What I do with this file in my projects' build.xmls is along the lines of:
<property name="download.dir" location="download"/>
<mkdir dir="${download.dir}"/>
<echo message="Downloading import files to ${download.dir}"/>
<get src="http://myserver/ivy/ivy-tasks.xml" dest="${download.dir}/ivy-tasks.xml" usetimestamp="true"/>
<import file="${download.dir}/ivy-tasks.xml"/>
The "dirty" part about this is that I have to do the above steps outside of a target, because the import task must be at the top-level. Plus, I still have to include this XML in all of the build.xml files that need it (i.e. there's still some amount of duplication).
On top of that, there might be additional situations where I might have common (non-Ivy) tasks that I'd like imported. If I were to provide these tasks using Ivy's dependency management I'd still have problems, since by the time I'd have resolved the dependencies I would have to be inside of a target in my build.xml, and unable to import (due to the constraint mentioned above).
Is there a better solution for what I'm trying to accomplish?
If you are using ANT 1.8+, then you could just import the build.xml directly from the hosted location.
http://ant.apache.org/manual/Tasks/import.html
Since Ant 1.8.0 the task can also
import resources from URLs or
classpath resources (which are URLs,
really). If you need to know whether
the current build file's source has
been a file or an URL you can consult
the property ant.file.type.projectname
(using the same example as above
ant.file.type.builddocs) which either
have the value "file" or "url".
<!-- importing.xml -->
<project name="importing" basedir="." default="...">
<import file="http://myserver/ivy/ivy-tasks.xml"/>
</project>
If you use Antlibs you can package them all inside a JAR file. Then simply copy this file into the ${ANT_HOME}/lib directory to use them.
After some additional searching, a possible solution would be to use SVN externals to check out specific required files that may be needed by the build.xml.
However, this would only work for users who are using Subversion as source control. It would still be nice to have a SCM-agnostic solution for users who aren't using Subversion, or another SCM that supports similar functionality.
What we've done is to create a project called 'bootstrap' which contains the various xml-files needed for the other projects at our office.
So to set up your development environment you run build.xml in bootstrap which copies the xml-files (like your ivy-stuff, and other targets) to a known location, and then your build files include these like this:
<import file="${ant.bootstrap.dir}/ant-commons.xml" />
<import file="${ant.bootstrap.dir}/ant-commons-ear.xml" />
Our bootstrap build.xml contains this:
<target name="install">
<fail unless="ant.bootstrap.dir" message="ant.bootstrap.dir ${missing.property.message}"/>
<copy todir = "${ant.bootstrap.dir}">
<fileset dir = "src/xml"/>
</copy>
</target>

Resources