2011/07/14

Deploying with maven-antrun-plugin

I really like to use maven in my software projects but sometimes it can be a real pain in the "keyboard".

After trying to deploy a package using the maven-deploy-plugin into a ssh/sftp server without success I changed the tactical for old and very known tool: ant

Well, it just worked!

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <dependencies>
        <dependency>
            <groupId>ant</groupId>
            <artifactId>ant-jsch</artifactId>
            <version>1.6.5</version>
        </dependency>
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.44</version>
        </dependency>
    </dependencies>               
    <executions>
        <execution>
            <id>upload</id>
            <phase>deploy</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <scp
                        todir="username:password@hostname:/path/destiny"
                        port="22" trust="true" verbose="true"
                        file="${basedir}/target/ProjectXYZ-${project.version}-dist.tar.bz2">
                    </scp>
                </target>
            </configuration>
        </execution>                   
    </executions>
</plugin>  

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <skip>true</skip>
    </configuration>

</plugin> 


The maven-antrun-plugin calls the ant task scp to do the transfer to the remote site and needs the declared dependencies.
The trust attribute allows me to accept unknown machines and avoid the exception if it is the case.
The verbose attribute show some progress (useful when the file is big).

The transference must be done every time I release a version of the project and so I attached it to the deploy phase. As I only need the package I deactivated the maven-deploy-plugin using the skip declaration in the configuration.
The file being transferred was build using the maven-assembly-plugin.

mvn deploy

Good, no more manual sftp.

2 comments:

  1. Hello,
    First thanks for the post.
    I have the same code in my pom and the execution of the scm command does nothing. The traces in cmd are:

    [INFO] Error stacktraces are turned on.
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building eee
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-antrun-plugin:1.3:run (default-cli) @ eee ---
    [INFO] Executing tasks
    [scp] Connecting to localhost:22


    I put the verbose mode and, i get the same result.
    ¿Do you know what i doing wrong?
    Thanks a lot

    ReplyDelete
  2. Well, you should see something like this:

    [INFO] --- maven-antrun-plugin:1.6:run (upload) @ GeoUtil ---
    [INFO] Executing tasks

    main:
    [scp] Connecting to your-machine:22
    [scp] Sending: ProyectXYZ-1.0-SNAPSHOT-20110728-095348-dist.tar.bz2 : 10010042
    ................................................. 50%
    ................................................. 100%
    [scp] File transfer time: 0.76 Average Rate: 13,223,305.15 B/s
    [scp] done.
    [INFO] Executed tasks

    Checklist:

    There is a SSH server in your localhost using the port 22, isn't it?
    You are using a old version of antrun (1.3), try 1.6.
    Your firewall, if any, must allow inbound connections to the 22 port.
    Try another machine where you know the SSH connections work already.

    I tried independently with a nonexistent port and a nonexistent machine and the message seems to be the same but you didn't show all the output...

    [INFO] Executing tasks

    main:
    [scp] Connecting to 192.168.0.3:22
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1:17.934s
    [INFO] Finished at: Thu Jul 28 10:14:08 CLT 2011
    [INFO] Final Memory: 29M/70M
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.6:run (upload) on project GeoUtil: An Ant BuildException has occur
    ed: com.jcraft.jsch.JSchException: java.net.ConnectException: Connection timed out: connect -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

    ReplyDelete