Maven Ant SCP error

2019-08-24 03:23发布

问题:

when I run a "maven-antrun-plugin" (maven 3.0.3) with the ant-command:

 <scp />

I get: Problem: failed to create task or type scp [ERROR] Cause: the class org.apache.tools.ant.taskdefs.optional.ssh.Scp was not found.

I checked in my $ANT_HOME/lib and the jar is there (ant-jsch.jar).

What's the problem?

BTW I'm using ubuntu 11.10. Ant is downloaded from the website (as from the ubuntu's repository I got the same problem)

回答1:

Did you forget to set the task definition

<taskdef name="scp" classname="org.apache.tools.ant.taskdefs.optional.ssh.Scp" classpathref="lib/ant-jsch.jar" />


回答2:

For one, maven antrun plugin does not use the installed ant on your system for its operation.

scp is a task which is not included in ant's default jar. To get it running, you need to follow the steps documented in this example from maven antrun plugin page.

In your case, I guess you would need to include ant-jsch.jar and jsch.jar as plugin dependencies.

  <dependencies>
      <dependency>
        <groupId>com.jcraft</groupId>
        <artifactId>jsch</artifactId>
        <version>1.4.1</version>
      </dependency>
      <dependency>
        <groupId>org.apache.ant</groupId>
        <artifactId>ant-jsch</artifactId>
        <version>0.1.45</version>
      </dependency>
    </dependencies>


回答3:

I had the same problem and fixed it! Remember, you need BOTH ant-jsch.jar (native from ant or java installation) and updated jsch (in my case it was jsch-0.1.46.jar) in ant lib dir.

You need to load the newest jsch.jar from http://www.jcraft.com/jsch/ and add to libs folder (but nor replace ant-jsch.jar).

The mistake a lot of developers do:

  1. ant-jsch.jar out of date (in this case task is unknown for ant)
  2. jsch not exists or out of date in ant lib dir
  3. Native ant-jsch.jar replaced with jsch (i did it...)

In last 2 cases ant knows scp command, but says it is not available.



回答4:

If you don't want to add the jsch jar to $ANT_HOME/lib, or still have trouble getting the ant syntax of scp correct (like I did) you can fall back to just using exec:

<exec executable="scp">
  <arg value="-r"/>
  <arg value="${LOCALDIR}/images"/>
  <arg value="remoteHost:/var/www/html"/>
</exec>

Where remoteHost is defined in $HOME/.ssh/config.



回答5:

I had a similar problem recently. On a windows system, with Eclipse it came down to verifying:

  • that ANT works
  • SCP works by itself,
  • that ANT and SCP worked within the Eclipse development environment.

A simple ANT file can be run within eclipse named build.xml. Sample contents would be:

<?xml version="1.0" encoding="UTF-8"?>
<project default="main" basedir=".">

<echo message="A 'testant' task."/>
 <target name="main" >
     <scp trust="yes" file="testfileinRootDirectory.xml" todir="username:password@hostname.org.domain:/home/username/existingfolder"/>
  </target>
</project>

If you can run this from within Eclipse, then you have some form of ANT working, and also have some form of WinSCP working.

If you cannot get it working in Eclipse, then open a command window in the directory that has the build.xml and run ant from a command prompt. (It will pick up and run a build.xml file automatically).

If it does not work, then check the entries that you have within Eclipse - as it could be using a different set of paths for ANT than a command line run of Maven might be. From within Eclipse check out: Window|
Preferences|
Ant|
Runtime|
Examing the Classpath entries.

The Ant Home Entry may be Eclipse specific: e.g.: c:\eclipse\plugins\org.apache.ant_1.8.3_v20120321-1730\lib

The Global Entries may be different (e.g. your installation location) - such as c:\dev\ant - or wherever your %ANT_HOME% directory has been set to be after an installation.

Eclipse however may be searching for SCP jars in the c:\Users\username\.ant\lib directory.

One possible solution:
- Install ANT to the c:\Users\username\.ant directory
- Set the ANT_HOME to be c:\Users\username\.ant
- Within Eclipse set the Ant Home option (same screen as the Runtime options) to be same as that on a command line (c:\Users\username\.ant).

Note: A possible disadvantage is that the User and System settings may be the same for ANT, however you just need to get the optional jars into one location in future in order to get things working.

Testing Maven:

Similar to Raghuram's answer above, getting the dependecies right is important. Use the full sample code from Cedric Walter's page - Apache Maven copy local file to a remote server server using SSH