I have a build script and as part of that script it copies a jar file to a directory, for ease lets call it the utils jar. the utils jar is built by another build script sitting in another directory. What im trying to do have my build script run the utils build script so that I can ensure the utils jar is up to date.
So I know I need to import the utils build file.
<import file="../utils/build/build.xml" />
Which doesn't work because the import task, unlike almost every other ant taks, doesn't run from basedir, it runs from the pwd. So to get around that I have this little ditty, which does successfully import the build file
<property name="baseDirUpOne" location=".." />
<import file="${baseDirUpOne}/utils/build/build.xml" />
So now that ive solved my import problem I need to call the task, well that should be easy right:
<antcall target="utils.package" />
note that in the above, utils is the project name of ../utils/build/build.xml
the problem I'm now running into is that ant call doesn't execute in ../utils/build so what I need, and cant find, is a runat property or something similar, essentially:
<antcall target="utils.package" runat="../utils/build" />
The reason I need this is that in my utils build file the step to select which code to copy to the jar is based on relative paths so as to avoid hardcoding paths in my ant file. Any ideas?
I've got something similar set up: I have a main Ant build.xml which calls a separate build.xml that takes care of building my tests. This is how I do it:
<target name="build-tests">
<subant target="build">
<fileset dir="${test.home}" includes="build.xml"/>
</subant>
</target>
The trick is to use subant instead of antcall. You don't have to import the other build file.
Try using the "ant" task instead of the "antcall" task, which runs the imported build directly instead of importing it into the current build file. It has a "dir" parameter:
the directory to use as a basedir
for the new Ant project. Defaults to
the current project's basedir, unless
inheritall has been set to false, in
which case it doesn't have a default
value. This will override the basedir
setting of the called project.
So you could do:
<ant antfile="${baseDirUpOne}/utils/build/build.xml" dir="../utils/build" />
or something like that.
You can pass params down to antcall using nested in the antcall block. So, you can pass the properties down that way (probably even basedir since properties are immutable).
Related
Problem:
We have a Websphere EAR project. Under it are two web modules. These two web modules make use of the same shared session context. This in enabled by setting in it the ibm-application-ext.xml file of the ear project.
I know how to create an ant build script to create an .ear file. The thing is, I do not know how to include ibm-application-ext.xml in the ant script to have it included in the .ear file . How do I do that?
If the application.xml file is included through the ff. syntax below, how to I include ibm-application-ext.xml.
<ear destfile="somearfile.ear" appxml="conf/application.xml">
If it's stored in conf/application.xml, then something like this:
<ear destfile="somearfile.ear" appxml="conf/application.xml">
<zipfileset dir="conf" includes="ibm-application-ext.xml" prefix="META-INF"/>
</ear>
...or just:
<jar destfile="somearfile.ear">
<zipfileset dir="conf" includes="*.xml" prefix="META-INF"/>
</jar>
In one of our build script, we have following simple copy task added ->
<copy todir="${targetdir}"
file="${sourcedir}/modules/glassfish.jaxb.xjc_1.0.0.0_2-1-12.jar"/>
This copy task started hanging when the glassfish jar name got changed (version upgrade which are not in our control) at the source location. I was expecting it to error out causing the build failure in that case. Actually at first I was not able to figure out at what particular step build was hanging. Then when I added "-debug" to the ant command and I realized its successfully completing a step prior to this copy task and still there was no trace of copy command that is hung. When I updated the new jar name, it worked fine and build was successful which proved that the copy task is hanging because of filename got changed. To make it easy to debug next time, I added an echo statement like below just prior to that copy task ->
<echo message="Copying glassfish jar to ${targetdir}.."/>
But I am still confused as to why it didn't give error with build failure? I am using Apache Ant version 1.7.1. Could this be a bug? How else I can avoid this situation in future with just the copy task (without using * in the jar name)? TIA
That worked for me. Well, didn't work for me. I got the error message. I am using Ant 1.8 and Ant 1.9.2. I didn't try it with Ant 1.7, but I doubt it's a bug.
Try to use the -v parameter in Ant:
$ ant -v target
And be prepared for a longwinded output. This will give you information what's going on with Ant, and may explain why it's freezing. There's a few things you could do: Use a fileset to specify the file.
<copy todir="${targetdir}">
<fileset dir="${sourcedir}/modules">
<include name="glassfish*.jar"/> <!-- Will catch any glassfish.jar -->
</fileset>
</copy>
Of course, if the file doesn't exist, you won't get an error or even a warning. However, a <fail/> before will detect the issue:
<fail message="missing file ${sourcedir}/modules/glassfish.jaxb.xjc_1.0.0.0_2-1-12.jar">
<condition>
<not>
<available
file="${sourcedir}/modules/glassfish.jaxb.xjc_1.0.0.0_2-1-12.jar"
type="file"/>
</not>
</condition>
</fail>
To force the build to quit, an alternative way
<available file="${sourcedir}/modules/glassfish.jaxb.xjc_1.0.0.0_2-1-12.jar"
property="glassfish.jaxb.xjc.jar.present"/>
<fail message="you message" unless="glassfish.jaxb.xjc.jar.present"/>
just a few lines less :)
If you want to dig into it, try this:
write a simple build file, which contains only one target with copy, and put it to the same place of your main build file.
<target name="test-copy">
<!-- here use an old (wrong) file name -->
<copy todir="${targetdir}"
file="${sourcedir}/modules/glassfish.jaxb.xjc_1.0.0.0_2-1-12.jar"/>
</target>
run it, check if it fails or hangs.
If this simple build file works, it's very possible that something else in your main build file is causing the bug.
I have an Ant task that FTPs all files in a specified directory, and it uses a fileset:
<fileset dir="${publicDirectory}">
<include name="media/**/*" />
</fileset>
I have a file that contains all the files that I would like to include:
media/some/dir/1.txt
media/some/other/2.txt
...
How can I have the fileset read the file and only include whatever I've listed there?
I've tried quite a few things, but nothing seems to be able to get around a basic issue: The <ftp> task works only on filesets and not other types of resources. I've tried various filterchains, but to no avail.
The best I could come up with was using the Ant-Contrib <for> or <foreach> task to loop through the file and then use an <exec> task to execute the command line version of ftp.
I'm in the process of modifying an Ant script (currently in use from within MyEclipse) to work from the command line. I'm doing this so anyone can check out the project and build it without MyEclipse. The problem I'm running into is that MyEclipse includes the dependencies behind the scenes. It does this by looking at the workspace's Ant configuration and compiling the classpath based on the selected libraries in the preferences dialog. Long story short, I need to take those dependencies and make the script smart enough to include them on its own, without the help of MyEclipse.
The tasks that are giving me a headache are the sshexec and scp tasks. They are optional ant tasks that require a version of jsch to run. I removed jsch from MyEclipse's Ant classpath and added it to a lib folder in the project itself (lib/dev). MyEclipse immediately complained that the SSHExec class could not find the dependent class, com.jcraft.jsch.UserInfo which is part of jsch-0.1.44.jar.
I don't see a way to set the classpath for Ant from within the build script. I have the following code, which adds a path element to the script, but I don't think Ant uses this unless explicitly associated to a task or another element.
<path id="web-jars">
<fileset dir="${web-lib}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${app-lib}"> <!-- this is where jsch resides -->
<include name="**/*.jar" />
</fileset>
</path>
It seems that I need to use taskdef to define the sshexec and scp tasks:
<taskdef name="sshexec" classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec"
classpathref="web-jars"/>
MyEclipse complains about this, "taskdef A class needed by class org.apache.tools.ant.taskdefs.optional.ssh.SSHExec cannot be found: com/jcraft/jsch/UserInfo"
It's clearly in the classpathref, web-jars. And I can't run anything in the script because of this malformed or misconfigured taskdef.
The problem here is that the SSHExec class is loaded from a classloader which itself has no access to your web-jars class loader. Supplying this classpath for the taskdef does not change this. Each class can only load classes from its own classloader and any parent class loaders, but the web-jars classloader is not a parent class loader of SSHExec's class loader (it is likely the other way around, since SSHExec seems to be found here).
It looks like this:
ClassLoader web-jars -------------> application CL -------------> bootstrap CL
taskdef
=> look for SSHExec here
=> look first in parent class loader
=> look for SSHExec here
=> look first in parent class loader
=> look for SSHExec here
=> not found
=> look in our own classpath
=> found, load the class
=> it somehow uses interface UserInfo
=> look for UserInfo here
=> look first in parent class loader
=> look for UserInfo here
=> not found
=> look in our own classpath
=> not found, throw exception.
The VM has no idea to look for UserInfo (and the other JSch classes) in the web-jars classloader.
I suppose the SSHExec task is somewhere in the usual ant classpath, i.e. loaded by the application class loader. Then removing SSHExec from ant's classpath (or adding jsch.jar to it) seems to be the only solution here.
Create ~/.ant/lib and copy jsch.jar in there as part of the build initialisation. Any tasks which do scp/sshexec work should depend on this init target.
<target name="init">
<property name="user.ant.lib" location="${user.home}/.ant/lib"/>
<mkdir dir="${user.ant.lib}"/>
<copy todir="${user.ant.lib}">
<fileset dir="${basedir}/build/tools" includes="jsch-*.jar"/>
</copy>
</target>
<target name="mytarget" depends="init">
<scp todir="user#host"><fileset dir="..."/></scp>
</target>
The Ant within Eclipse unfortunately won't pick this up immediately as it does not read ~/.ant/lib on every execution; After running mytarget within Eclipse once and watching it fail, then go to:
Window>Preferences>Ant>Runtime and press Restore Defaults - this will add any .jar files from ~/.ant/lib to the Global Entries section and you should be good to go.
I am using Ant under WinXp to build MSVisual c++ projects.
To access "devenv.com", being computer-independent, I would like to use the vsvars32.bat script.
Unfortunately, it seems that environment variables are defined only in the "exec" scope.
Example:
<exec executable='"${env.VS90COMNTOOLS}vsvars32.bat/>
<echo message="${DevEnvDir}" />
<echo message="${env.DevEnvDir}" />
<property environment="env2"/>
<echo message="${env2.DevEnvDir}" />
I never get the expected result.
How can I use the "vsvars32.bat" script and access to its env. vars?
Is there better way to achieve this?
Instead of calling vsvars32.bat directly, call it from little helper script that writes the environment settings to a file using set.
Helper script vsenvwrap.bat:
#echo off
call "%VS90COMNTOOLS%\vsvars32.bat"
set > vsenv.txt
In your build.xml call the helper script, then read the settings file vsenv.bat:
<exec executable="vsenvwrap.bat" />
<property file="vsenv.txt" prefix="env2" />
You can then delete the vsenv.txt file during your build, or in your clean target.
This uses the fact that environment variable listings mostly conform to the format required by java property files.
You can create a small batch file that runs your ant script and in that batch file execute vsvars32.bat before calling ant.
#echo off
setlocal
set TEMP_HOME=%~dp0
call "%VS90COMNTOOLS%\vsvars32.bat"
call "%ANT_HOME%\bin\ant.bat" %*
endlocal