Maven Wagon SCP is not able to establish a connection

后端 未结 4 1889
自闭症患者
自闭症患者 2020-12-17 03:14

I am trying to copy resources to another location. I am using maven wagon-ssh plugin to do this. It works fine locally, I am having issues when using Hudson/Jenkins.

4条回答
  •  旧时难觅i
    2020-12-17 03:33

    This is what we use to populate known_hosts file on jenkins node:

      
        org.codehaus.groovy.maven
        gmaven-plugin
        
          
              check-known-hosts
              initialize
              
                execute
              
              
                
                    import com.jcraft.jsch.*;
                    import org.apache.maven.wagon.providers.ssh.knownhost.*;
    
                    def keyString = "" // host key - the line from known_hosts after key type (ssh-rsa)
    
                    FileKnownHostsProvider fkhp = new FileKnownHostsProvider();
    
                    JSch sch = new JSch();
                    sch.setKnownHosts(new ByteArrayInputStream(fkhp.getContents().getBytes()));
    
                    def host = project.properties.serverAddress // define someserveraddress.com in  
    
                    if (host != null) {
                      HostKeyRepository hkr = sch.getHostKeyRepository();
                      HostKey[] hk = hkr.getHostKey( host , null );
    
                      StringWriter stringWriter = new StringWriter();
    
                      String knownHost = host + " " + "ssh-rsa" + " " + keyString;
    
                      if ( hk != null )
                      {
    
                        PrintWriter w = new PrintWriter( stringWriter )
                        def containsKey = false;
                        for ( HostKey key : hk )
                        {
                          def toAdd =  key.getHost() + " " + key.getType() + " " + key.getKey();
                          w.println(toAdd)  ;
                          containsKey = knownHost.equals(toAdd);
                        }
                        if (!containsKey) {
                          println "Adding key for " + host + " to known_hosts"
                          w.println(knownHost);
                          fkhp.storeKnownHosts(stringWriter.toString() );
                        } else {
                          println "Key for " + host + " is already present in known_hosts"
                        }
                      }
                    }
                
              
          
        
        
          
            org.apache.maven.wagon
            wagon-ssh-common
            2.10
          
          
            com.jcraft
            jsch
            0.1.54
          
        
      
    

    Seems to work pretty well.

提交回复
热议问题