【问题标题】:How deploy maven3 artifact to remote server using scp如何使用 scp 将 maven 工件部署到远程服务器
【发布时间】:2013-10-04 05:41:53
【问题描述】:

我想为自己创建的工件创建自己的 maven 存储库,但在尝试将 maven 3 工件部署到自定义服务器时遇到问题。为了更好地解释这一点,我将提供一些信息:

  • 我正在使用 Maven 3
  • 我正在使用 Eclipse Keppler
  • 我正在使用 Jenkins
  • 远程服务器正在运行 Ubuntu Server 11.04
  • Jenkins 正在 Ubuntu 服务器上运行
  • 我的本地计算机正在运行 Windows XP

我的第一次尝试是使用我的机器。我在 Eclipse 中运行 Maven 进行部署,一切正常。我将以下内容添加到我的项目 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>

然后在我的 settings.xml 中添加

<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>  

所以在我的本地机器上它可以工作,但我需要使用 Jenkins 来完成这项工作。我修改了 Jenkins settings.xml,因为它在 Linux 上运行,所以不需要 sshExecutable。 Jenkins settings.xml 看起来像

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

     </server>  
 </servers>  

然后我将 pom.xml 修改为只执行 scp 而不是 scpexe

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

但是根据这个页面https://cwiki.apache.org/confluence/display/MAVEN/Maven+3.x+Compatibility+Notesmaven 3 不支持scp。我以任何方式运行它,我从 Jenkins 日志中收到以下错误消息

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 : 

如果我使用 scpexe 而不是 scp,我会收到另一条错误消息

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 : 

我可以进行部署的唯一方法是分两步进行

  1. 配置 Jenkins 以实现安装目标
  2. 从命令行运行以下命令

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

我尝试了很多东西,包括将该命令写入 Jenkins 目标,但每次我在 Jenkins 中使用 scp 命令时,构建都会失败。

任何关于如何解决此问题的想法将不胜感激。

【问题讨论】:

    标签: linux maven jenkins scp


    【解决方案1】:

    我很想看看是否有任何真正的 Maven 解决方案。我一直使用 Maven Antrun 插件解决这个问题,如下所示:

    <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>
    

    对此有几点说明:我通过运行到deploy 阶段并提供deployment.server 设置来激活此配置文件。为了方便起见,我将相应的设置添加到我的settings.xml,这样我就不必每次都在命令行中提供这些:

    <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>
    

    skip 是实际的 deploy 目标,因为它将在我运行到 deploy 阶段时执行,这是我不想要的。

    【讨论】:

    • 很抱歉,但这对我不起作用。我应该把那个大的“个人资料”放在哪里?在我的 pom.xml 中?如果答案是肯定的,我的 pom.xml 在哪里?在构建标签或其他位置?谢谢!!!
    • 第一个(大)片段进入您的 POM 文件。这是一个profile,它位于POM 文件XML 结构中,如下所示:project.profiles.profile。第二个片段进入您的settings.xml。您可以在 Maven 安装的 conf 文件夹中找到此文件。标准settings.xml 中有示例向您展示放置位置。
    • 运行时出现以下错误Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter
    • 好像执行了正常的deploy目标,也就是说skip被忽略了,也就说明profile(id=deploy)被忽略了。该配置文件应通过为deployment.server 设置任何值来激活,因此settings.xml 中的设置可能不正确。作为测试,您能否在命令行上尝试类似-Ddeployment.server=10.10.10.10 的操作,以查看是否至少选择了 POM 中的配置文件?
    • 在这种情况下,我收到以下错误消息message : Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run (default) on project myproject: Execution default of goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run failed...
    【解决方案2】:

    Verhagen 的答案是正确的,但更纯粹的 maven 解决方案是:https://stackoverflow.com/a/3303525/1881318

    【讨论】:

    • 非常感谢您的帮助,但我已经使用 Archiva 解决了这个问题,因为 maven 3 不允许 scp
    • 我不确定 maven 如何不允许 scp。话虽这么说,我确实在使 scp 使用私钥而不是密码时遇到问题。见my question on scp
    猜你喜欢
    • 2012-07-01
    • 1970-01-01
    • 2010-11-01
    • 2012-07-09
    • 2012-01-15
    • 1970-01-01
    • 2017-10-01
    • 2014-04-09
    • 1970-01-01
    相关资源
    最近更新 更多