Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How deploy maven3 artifact to remote server using scp

I want to have my own maven repository for artifacts created by myself but I have a problem trying to make a deploy of maven 3 artifact to a custom server. To explain this better I'm going to give some information:

  • I'm using Maven 3
  • I'm using Eclipse Keppler
  • I'm using Jenkins
  • The remote server is running Ubuntu Server 11.04
  • Jenkins is running on the Ubuntu server
  • My local machine is running Windows XP

My first attempt was with my machine. I run Maven in Eclipse to make the deploy, and everything works fine. I add the following to my projects pom

    <build>
           ...
        <extensions>
            <extension>
                <groupId>org.apache.maven.wagon</groupId>
                <artifactId>wagon-ssh-external</artifactId>
                <version>1.0-beta-6</version>
            </extension>
        </extensions>
          ...
      </build>

...

<distributionManagement>
      <repository>
          <id>my server id</id>
          <name>my repository name</name>
          <url>scpexe://my server//path/to/my/repository</url>
      </repository>
  </distributionManagement>

And in my settings.xml I add

<servers>  
      <server>  
          <id>my server id</id>  
         <username>server username</username>   
         <password>server password</password> 

         <configuration>
             <sshExecutable>plink</sshExecutable>
             <scpExecutable>pscp</scpExecutable>
         </configuration>  

     </server>  
 </servers>  

So in my local machine it works, but I need to get this work using Jenkins. I modified the Jenkins settings.xml, because it runs on Linux, so doesn't need sshExecutable. The Jenkins settings.xml looks like

<servers>  
      <server>  
          <id>my server id</id>  
         <username>server username</username>   
         <password>server password</password> 

     </server>  
 </servers>  

Then I modified the pom.xml to execute just scp and not scpexe

<distributionManagement>
      <repository>
          <id>my server id</id>
          <name>my repository name</name>
          <url>scp://my server//path/to/my/repository</url>
      </repository>
  </distributionManagement>

But according to this page https://cwiki.apache.org/confluence/display/MAVEN/Maven+3.x+Compatibility+Notes maven 3 does not support scp. I run it any way and I got the following error message from Jenkins log

mavenExecutionResult exceptions not empty
message : Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project myproject: Failed to deploy artifacts/metadata: No connector available to access repository my_repository (scp://my server//path/to/my/repository) of type default using the available factories WagonRepositoryConnectorFactory
cause : Failed to deploy artifacts/metadata: No connector available to access repository my_repository (scp://my server//path/to/my/repository) of type default using the available factories WagonRepositoryConnectorFactory
Stack trace : 

If I use scpexe instead of scp I got another error message

mavenExecutionResult exceptions not empty
message : Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project pruebanueva: Failed to deploy artifacts: Could not transfer artifact {$groupId}:{$artifactId}:{$package}:{$version} from/to my_repository (scpexe://my server//path/to/my/repository): Error executing command for transfer
cause : Failed to deploy artifacts: Could not transfer artifact {$groupId}:{$artifactId}:{$package}:{$version} from/to my_repository (scpexe://my server//path/to/my/repository): Error executing command for transfer
Stack trace : 

The only way I could make deploy, was doing it in two steps

  1. Configuring Jenkins to make just the install goal
  2. Running the following command from command line

mvn deploy:deploy-file -DgroupId=$groupId -DartifactId=$artifactId -Dversion=$version -Dpackaging=jar -Dfile=path/to/file.jar -Durl=scp://my server//path/to/my/repository -DrepositoryId=my repository id

I tried many things, including writing that command into Jenkins goal, but everytime I use the scp command in Jenkins the build fails.

Any idea of how to solve this issue will be appreciated.

like image 652
Angelo Avatar asked Sep 27 '13 19:09

Angelo


1 Answers

I am interested to see if there's any real Maven solutions to this. I have always fixed this using the Maven Antrun plugin as follows:

<profile>
  <id>deploy</id>
  <activation>
    <property>
      <name>deployment.server</name>
    </property>
  </activation>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <phase>deploy</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <target>
                <echo>deploying to server: ${deployment.server}</echo>
                <taskdef classname="org.apache.tools.ant.taskdefs.optional.ssh.Scp" name="scp" />
                <scp file="${project.build.directory}/${project.artifactId}.war" password="${deployment.password}" todir="${deployment.userName}@${deployment.server}:" trust="true" verbose="true" />
                <!-- <sshexec command="echo unity | sudo -S cp ${project.build.finalName}.jar $( if [ -e /station ]; then echo /station/lib; else echo /opt/pkg-station*/webapps/station*/WEB-INF/lib; fi )" host="${targetStation}" password="unity" trust="true" username="wps"></sshexec> -->
              </target>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.25</version>
          </dependency>
          <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-jsch</artifactId>
            <version>1.7.1</version>
          </dependency>
        </dependencies>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <configuration>
          <skip>true</skip>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>

A few notes on this: I activate this profile with a combination of running to the deploy phase, and providing a deployment.server setting. For my convenience then, I add the corresponding settings to my settings.xml so that I don't have to provide these all on the command-line every time:

<profile>
    <id>alwaysActiveProfile</id>
    <properties>
        <deployment.server>10.10.10.10</deployment.server>
        <deployment.userName>userName<deployment.userName>
        <deployment.password>password</deployment.password>
    </properties>
</profile>

I skip the actual deploy goal because it will be executed when I run to the deploy phase, which I don't want.

like image 76
Sander Verhagen Avatar answered Sep 21 '22 17:09

Sander Verhagen