Unable to transfer file with <scp> Ant task and username containing spaces - ant

I am using the following Ant script with username and IP address. I want to transfer a file. If username has a space in it, the file is not transferring.
<project name="Hello World Project" default="info">
<property file="build.properties"/>
<target name="info">
<scp file="/home/smith/Gjs/ws2/abc.txt"
todir="Srupanan k:rakesh#19.1.21.6:~/" trust="true"/>
</target>
</project>
Is there any way to use <scp> with a username containing a space?

I checked the source code and from what I saw, the ant task does not mind spaces at all, it only checks for the : and # in the toDir.
There might be a problem in the Jsch library it uses to handle the ssh connection, although I doubt it would be easy to debug and fix. You should however make sure you're using an up-to-date installation of ant.
You could also try to use the sftp flag of the ant task in hope that the sftp implementation will handle spaces better.
If all fails you should still be able use an exec task to upload via a native scp call.
Good luck !

Related

How to identify unused tasks in ant xml files

I was given 20 xml files which are run when the build.xml is called.... I was asked to clear the junk in those files. How do I identify the unused tasks in those xml files?
There's probably no simple, fool-proof solution to this (you'd really just have to ask everyone in your company what they're using), however one very useful tool I like to use with complicated Ant scripts is the "Grand" graphing tool. https://ant-grand.github.io/Grand/grand.html
Download the library and place it in a lib directory, then create a new Ant script like this:
<?xml version="1.0"?>
<project default="graph">
<typedef classpath="lib/grand.jar" resource="net/ggtools/grand/antlib.xml" />
<target name="graph">
<grand buildfile="${script.file}" output="${script.file}.dot">
<filter name="prefixed" />
</grand>
<exec executable="dot">
<arg line="-Grankdir=TB -Tpng ${script.file}.dot -o ${script.file}.png"/>
</exec>
</target>
</project>
Now you can call the script with
ant -f graph.xml -Dscript.file=/path/to/ant/script/build.xml
and you will end up with a build.xml.png image file that shows a clean target dependency tree for whichever script you pointed it to.
I've added an image that was generated from one of my own relatively complicated scripts as an example. When a target has an arrow pointing to another target, that means the former is using the latter as a dependency (and thus the latter can't be removed without disrupting the former). Anything that isn't being pointed to as a dependency can be removed without hurting anything else, however that doesn't necessarily mean it's useless on its own.

Ant scp task error

I have a problem with my Ant scp task.
When I'm trying to send a file to a server, I get the follow error:
com.jcraft.jsch.JSchException: 2: No such file
Here is my task:
<scp verbose="yes"
todir="${deploy.user}#${app.deploy.server}:/home/software/public_html/${app.appName}"
password="${deploy.password}" sftp="true" trust="true" >
<fileset dir="${basedir}/jnlp/extensions/production" includes="*.jnlp"/>
</scp>
Anyone have an idea about what might be the problem?
Certain Ant tasks, although standard tasks, require third party libraries. If you look at that table, you'll see that you need to install the jsch.jar version 0.142 or later.
Easiest thing to do is install this in $ANT_HOME/lib. That will solve your problem. However, it now creates an issue where you have a library dependency in your build.xml file that other users must be aware of. They'll have to know to manually install the required jar in their $ANT_HOME/lib, or define an $ANT_LIB directory and put it there.

Runnable jar file without ant java task - how is this possible?

I've inherited some code from a previous developer, which gets built using Ant into an executable jar file that runs by double clicking. The application runs, but under some conditions crashes with an OutOfMemoryError. To investigated this, I'd like to add the -XX:+HeapDumpOnOutOfMemory jvm arg to the Ant buildfile, and as I understand it, the <jvmarg value="-XX:+HeapDumpOnOutOfMemory" /> element needs to go under a <java ...> task. However, there is no <java ...> task to be found in this or any other Ant buildfiles in this code base.
How is this even possible? How can the jar file be executable without a <java ...> task?
I'm asking primarily to find out what in fact makes my jar file executable so that I can figure out where to put that <jvmarg /> element to debug the OOME.
Thanks!
A <java> task doesn't create an executable jar file. It executes a Java program.
I don't think it's possible to specify VM parameters when starting an executable jar file by double-clicking on it. If you want to pass VM parameters, open a command prompt and execute the jar this way:
java -XX:+HeapDumpOnOutOfMemory -jar nameOfTheJar.jar
Your jar is executable because it has a Main-Class defined in the META-INF/MANIFEST.MF file. Double clicking it to run doesn't do anything with Ant. Ant is simply used to package the jar.
In order to add the parameter and still launch via a double click you could create a shortcut that runs the command in JB Nizet's answer

Ant FTP and Multiple File get (mget)

Is there any way to use mget within Ant, without using the exec task?
Here is the rundown. I have to connect to a third party server that does not support globbing with FTP get, the server requires the client use mget to do a glob.
Here is my task:
<ftp server="host" userid="user" password="pass" action="get">
<fileset dir="mydir">
<include name="pdf/*_PDF.ZIP.pgp"/>
</fileset>
</ftp>
It does not return any files. When I log in directly (Linux FTP command line client) I can see files. "get *" fails but "mget *" works.
Any ideas how to get Ant to use mget instead of get?
Ant uses commons-net.jar for the FTPTask.
If you don't care about platform independence the easiest way would be to use a specific executable and the exec task. You could check in the mget.exe along with the project so the user does not need to install it.
If you need platform independence you will probably need to write you own FTP task. You could copy the one in the Ant source code and make the necessary modifications. You could also choose another FTP library if you want but I think that commons net should have the necessary features.

How to load an optional task into ant without -lib or global installation?

I want to use the FTP task in ant, and I have found the appropriate jar files and got everything working fine. I have put the jar files in a "libs" directory alongside the other files used in the build. The only problem is that the user must run "ant -lib commons-net-ftp-2.0.jar" to make a build; I would really prefer that it were possible to just run "ant" with no arguments.
Reading the ant optional tasks intallation page, I see that there are five ways one can load up extra libraries in ant, and none of them are really what I'm looking for. I do not want to force the user to make any modifications to their system to run this task; it should be possible to just load it from the "libs" directory inside of our product's source folder. So that means setting the global CLASSPATH is also out (which is a bad idea anyways).
The last option, as noted in the documentation, is the preferred approach... loading the jarfiles individually from the build script itself. I have done this in the past with the ant-contrib tasks and JUnit, and would like to do that here, but I don't see how I can accomplish this. The FTP task doesn't support a nested classpath element, and I don't know the XML resource I would need to load this library via a taskdef. How can I load the libraries from within ant?
Edit: In response to the answers and questions which have been posted here so far, I'm using ant 1.7.1. Making an ftp taskdef definitely does not work; that throws the following error:
BUILD FAILED
/my/path/build.xml:13: taskdef class org.apache.tools.ant.taskdefs.optional.net.FTP cannot be found
Perhaps this is because the classname is wrong. How exactly do I find the classname I'm supposed to use if I only have a jarfile? It's not documented anywhere, and I couldn't find anything in the jar itself resembling that path.
The problem you are having is due to the different class-loaders in use. The Commons Net classes must be loaded by the same class-loader that loads the FTP task. Because the FTP task is loaded by Ant on start-up, you need to add the Commons Net to Ant's classpath so that it is loaded by the same class-loader. That's why the documentation gives you 4 different ways to do this.
I agree that none of them are ideal (the CLASSPATH environment variable being the worst). One way around this is to supply a shell script with your project that invokes Ant and passes the apporpriate -lib argument. You then get people to use this rather than invoking Ant directly. In fact, you could deviously name it 'ant' so that it gets run instead of the existing 'ant' on the path (this only works if the current directory is on the path, ahead of other directories).
The fifth option in the documentation is great in theory. They finally fixed the class-loading problems in 1.7.0. Unfortunately, as you mention, nobody retro-fitted the FTP task to take a classpath. You could try submitting an enhancement request, but this won't help in the short term.
There is one other option, which isn't any better than the others. Instead of making sure that the Commons Net classes are loaded by the class-loader that loads the FTP task, you could make sure that the FTP task is loaded by the class-loader that loads the Commons Net classes. To do this you have to remove the ant-commons-lib.jar file from the 'lib' directory of the Ant installation. This means that the FTP task won't get loaded on start-up. This is actually why the optional tasks are broken up into so many separate JARs - so that they can be individually removed. Put this JAR file alongside the Commons Net JAR file so that it can be loaded at the same time. Then you can do something like this (I tried this and it works):
<taskdef name="ftp"
classname="org.apache.tools.ant.taskdefs.optional.net.FTP">
<classpath>
<pathelement location="${basedir}/lib/ant-commons-net.jar"/>
<pathelement location="${basedir}/lib/commons-net-2.0.jar"/>
</classpath>
</taskdef>
<ftp server="yourserver.com"
userid="anonymous"
password="blah">
<fileset dir="somedirectory"/>
</ftp>
But this is probably a worse option than just using the -lib switch (with or without a wrapper script). The only other thing I can think of is to try to find a third-party FTP task to use instead of the default one.
I have a solution:
you can download a new "classloader" task from http://enitsys.sourceforge.net/ant-classloadertask/ and load it whith:
<taskdef resource="net/jtools/classloadertask/antlib.xml"
classpath="XXX/ant-classloadertask.jar"/>
Naw can do things like loading classes with the same classloader that ant use for his task:
<classloader loader="system" classpath="XXX/commons-net-2.0.jar"/>
or "loader="project""
Then you definde your task:
<taskdef name="ftp" classname="org.apache.tools.ant.taskdefs.optional.net.FTP"/>
and go :-)
So I succeeded in doing this for the ant-salesforce.jar that you get when trying to do salesforce work (fun...)
Check to see if the jar has an xml file in it that looks something like this:
<antlib>
<typedef name="compileAndTest" classname="com.salesforce.ant.CompileAndTest"/>
....
</antlib>
Then in ant give it a taskdev that reads that file from inside the given jar, like this:
<taskdef resource="com/salesforce/antlib.xml" classpath="lib/ant-salesforce.jar" />
Hope that helps some.
Ah, man, this is just so nasty. I run ant from eclipse. I don't want to reconfigure ant in eclipse for new workspaces, so here's what I decided to do, to decouple running the task and configuring ant. I extracted the ftp task to a separate build file. Next I added a native call to the command line to start a completely new ant process with the required libraries on the path:
<target name="deploy-ftp">
<exec command="ant">
<arg line="-buildfile ftp.xml deploy-ftp -lib lib/ant"/>
</exec>
</target>
Now the master build file can be run without any special arguments and no modifications are required to the ant installation. It's nasty though, since the ftp task runs in a completely clean environment. None of the properties and paths from the master build file are available. Luckily I had all of these in a separate property file anyway, so I only needed a single import.
I would like to add a big thanks to Dan Dyer. Without your extensive explanation of what's going on behind the scenes, I wouldn't have found this solution.
Will this work assuming libs is directly under you project's base directory
<taskdef name="ftp" classname="org.apache.tools.ant.taskdefs.optional.net.FTP">
<classpath>
<pathelement location="${basedir}\libs\commons-net-1.4.0.jar"/>
</classpath>
</taskdef>
Your users all have ant installed on their machines but you can't / don't want to make them add the FTP jar? Can you bundle ant with your project make tasks that call YOUR ant bundle, with the jars placed so it'll work as follows?
<taskdef name="ftp" classname="org.apache.tools.ant.taskdefs.optional.net.FTP">
<classpath>
<pathelement location="\lib\commons-net-1.4.0.jar"/>
</classpath>
</taskdef>
<target name="testFtp">
<ftp server="blah" userid="foo" password="bar">
<fileset file="test.file" />
</ftp>
</target>

Resources