在外部 SSH 命令中部署工件

为了使用 SSH 部署工件,您必须首先在 POM 的distributionManagement 元素中指定 SSH 服务器的使用,并 在构建元素中指定一个扩展,该扩展 将拉入使用 SSH 部署所需的 SSH 工件:

<project>
  <parent>
    <groupId>com.stchome</groupId>
    <artifactId>mavenFull</artifactId>
    <version>1.0</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.1-SNAPSHOT</version>
  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <!-- Enabling the use of FTP -->
  <distributionManagement>
    <repository>
      <id>ssh-repository</id>
      <url>scpexe://repository.mycompany.com/repository</url>
    </repository>
  </distributionManagement>

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

</project>

如果您从 Unix 部署或安装了 Cygwin,则不需要在settings.xml 文件中进行任何其他配置,因为所有内容都将从环境中获取。但是,如果您在 Windows 上使用plink 之类的东西,那么您将需要以下内容:

<settings>
  ...
  <servers>
    <server>
      <id>ssh-repository</id>
      <username>your username in the remote system if different from local</username>
      <privateKey>/path/to/your/private/key</privateKey> <!-- not needed if using pageant -->
      <configuration>
        <sshExecutable>plink</sshExecutable>
        <scpExecutable>pscp</scpExecutable>
        <sshArgs>other arguments you may need</sshArgs>
      </configuration>
    </server>
  </servers>
  ...
</settings>

当然,在尝试使用 Maven 进行部署之前,您应该确保可以手动登录到指定的 SSH 服务器。一旦您确认一切都设置正确,您现在可以使用 Maven 部署您的工件:

mvn deploy

有时您可能在部署时遇到权限问题,如果是这样,您可以像这样设置文件和目录权限:

 <settings>
   ...
   <servers>
     <server>
       <id>ssh-repository</id>
       <!--
        |
        | Change default file/dir permissions
        |
        -->
       <filePermissions>664</filePermissions>
       <directoryPermissions>775</directoryPermissions>
       <configuration>
         <sshExecutable>plink</sshExecutable>
         <scpExecutable>pscp</scpExecutable>
       </configuration>
     </server>
   </servers>
   ...
 </settings>

注意: 如果您使用的是 Putty,它会期望私钥是PPK 格式而不是标准格式,因此请确保您使用puttygen 将您的 openssh 格式密钥转换为PPK 格式或生成另一种格式。Windows 用户可以在此处找到 Putty 工具 。