maven antrun ssh or scp hides the output

2019-02-21 02:53发布

问题:

I want to run an ssh & scp commands using antrun in mvn.
the ssh & scp commands runs correctly, and the plugins that declared inthe next phases - run. but the output of all commands after the ssh/scp - isn't written to any output!

What is the problem?!

here is my pom.xml:

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>group</groupId>
    <artifactId>artifact</artifactId>
    <version>0.0.0-1-SNAPSHOT</version>
    <name>name</name>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <id>echoKuku</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <sshexec verbose="true" output="o.txt" failonerror="false" host="${Ip}" trust="yes"
                                    username="${UserName}" command="echo kuku" keyfile="${Keyfile}" />
                                <taskdef name="sshexec" classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec"
                                    classpathref="maven.plugin.classpath" />
                            </target>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>commons-net</groupId>
                        <artifactId>commons-net</artifactId>
                        <version>1.4.1</version>
                    </dependency>
                    <dependency>
                        <groupId>ant</groupId>
                        <artifactId>ant-commons-net</artifactId>
                        <version>1.6.5</version>
                    </dependency>
                    <dependency>
                        <groupId>ant</groupId>
                        <artifactId>ant-jsch</artifactId>
                        <version>1.6.5</version>
                    </dependency>
                    <dependency>
                        <groupId>jsch</groupId>
                        <artifactId>jsch</artifactId>
                        <version>0.1.29</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>download-jsch</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <overWriteSnapshots>true</overWriteSnapshots>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>jsch</groupId>
                                    <artifactId>jsch</artifactId>
                                    <version>0.1.29</version>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
    <properties>
        <javaVersion>1.7</javaVersion>
        <Ip>1.2.3.4</Ip>
        <UserName>root</UserName>
        <Keyfile>${user.home}/.ssh/id_rsa</Keyfile>
    </properties>
    <dependencies>
        <dependency>
            <groupId>jsch</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.29</version>
        </dependency>
    </dependencies>
</project>

the file jsch-0.1.29.jar was created in the target dir, but the output is:

C:\Users\azberger\somePath>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building name 0.0.0-1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The artifact jsch:jsch:jar:0.1.29 has been relocated to com.jcraft:jsch:jar:0.1.29
[INFO]
[INFO] --- maven-antrun-plugin:1.6:run (echoKuku) @ artifact ---
[INFO] Executing tasks

main:
  [sshexec] Connecting to 1.2.3.4:22
kuku

C:\Users\azberger\somePath>

Update: (16/3/16 8:11 GMT)

scp - works fine.
sshexec with a file script (commandResource="[file]") - works fine.
sshexec with a single command (command="[command]") - causes problem, as mention above.

回答1:

Give it a try with newer versions of JSCH dependencies (1.8.4 for ant-jsch and 0.1.53 for jsch, each with different group ids). It fixed the problem on my side:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>server-copy</id>
            <goals>
                <goal>run</goal>
            </goals>
            <phase>process-sources</phase>
            <configuration>
                <target>
                    <echo message="Pushing to host..." />
                    <sshexec host="hostname" username="user" trust="true" 
                        password="pass" failonerror="true"  
                        command="mkdir -p /home/user/test/test"/> 
                    <scp trust="yes"
                        file="some-file"
                        todir="user:pass@hostname:/path/to/some-file" 
                        />
                </target>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-jsch</artifactId>
            <version>1.8.4</version>
        </dependency>
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.53</version>
        </dependency>
    </dependencies>
</plugin>


回答2:

Thank you very much. After two long evenings I found the solution.

I had the latest version of ant and not org.apache.ant which was changed at version 1.6 So latest version is today 1.10.1 and it works very well.



标签: maven ssh ant scp