2009-09-10 143 views
19

我有这样的任务使用Maven与4个嵌套子项目的项目:使用Maven部署

  1. 每个孩子:JAR-了资源目录包括项目的依赖
  2. 向上移至父项目
  3. 使用单个命令可将所有已创建的存档提取到各种远程目标(完全安装),其中可能包括http服务器,应用程序服务器,文件服务器等(主要是* NIX)。目标设置在子项目水平
  4. 它也应该能够解压缩/从各个子项目复制(部分安装)

的文件是不是Java - 主要是各种脚本和HTML

我期待在各种插件帮助完成任务:程序集,依赖关系,antrun,解压缩。依赖关系看起来很有希望,但我需要解压缩不仅依赖性jar而且还有(子)项目内容。另外,由于我无法真正将操作紧缩到Maven生命周期,我将如何触发远程安装? mvn依赖:解压?这不是很具描述性或直观性。是否可以在不编写插件的情况下创建自定义目标(例如,项目:安装)?

使用Maven是企业标准,所以请不要提供替代方案 - 我几乎坚持与我有什么

+1

哈哈。我希望我和我的公司的Maven“卡在一起”。 – SingleShot 2009-09-10 15:27:28

+0

我的意思并不是贬义:) – Bostone 2009-09-10 15:28:55

+0

我不明白“子项目级提供的目的地”是什么意思。你是否将所有文件传送到每个目的地? – 2009-09-10 15:31:13

回答

12

好吧,我想下面可能做你的需要。这种方法的缺点是,随着后续构建的执行,每个部署之间会有间隔。这可以接受吗?

在每个项目中定义一个具有相同名称的配置文件(称为“发布”)。在该配置文件中,您可以定义配置以使用antrun-plugin通过FTP传输文件(请参见下文)。

在父项目中,您将拥有一个modules元素,将每个项目定义为一个模块。如果您运行的是mvn install -P publish,则每个项目都将依次构建,并启用发布配置文件,并在安装阶段将最终工件发布到目标。如果您需要部署其他文件,请相应地修改include element

请注意FTP任务的参数已设置为属性,这样可以从命令行覆盖它们和/或从父POM继承它们。

<profiles> 
    <profile> 
    <id>publish</id> 
    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-antrun-plugin</artifactId> 
     <executions> 
     <execution> 
      <id>ftp</id> 
      <phase>install</phase> 
      <configuration> 
      <tasks> 
       <ftp action="send" 
        server="${ftp.host}" remotedir="${ftp.remotedir}" 
        userid="${ftp.userid}" password="${ftp.password}" 
        depends="${ftp.depends}" verbose="${ftp.verbose}"> 
       <fileset dir="${project.build.directory}"> 
        <include 
        name="${project.build.finalName}.${project.packaging}"/> 
       </fileset> 
       </ftp> 
      </tasks> 
      </configuration> 
      <goals> 
      <goal>run</goal> 
      </goals> 
     </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-nodeps</artifactId> 
      <version>1.6.5</version> 
     </dependency> 
     </dependencies> 
    </plugin> 
    <properties> 
     <ftp.host>hostname</ftp.host> 
     <ftp.remotedir>/opt/path/to/install</ftp.remotedir> 
     <ftp.userid>user</ftp.userid> 
     <ftp.password>mypassword</ftp.password> 
     <ftp.depends>yes</ftp.depends> 
     <ftp.verbose>no</ftp.verbose>   
    </properties> 
    </profile> 
</profiles> 

更新:基于您的评论:您可以使用依赖插件下载每个依赖,但家长不能对孩子的依赖性,它会在孩子前建成。这将是另一个项目。您还需要在某处提供将它们部署到哪里的信息。目前,您在各个项目中拥有目标信息,因此无法在部署人员项目中访问。

采取这种方法,您可以在新项目中定义多个配置文件,每个配件一个。每个配置文件定义一个依赖项:复制执行以获取jar和一个项目的antrun执行。常见配置(例如antrun插件的依赖关系)可以从配置文件中取出。另外请注意,如果您定义了多个配置文件,这些属性将被合并,因此您可能需要使用工件名称来限定它们,例如ftp.artifact1.host

<profiles> 
    <profile> 
    <id>deploy-artifact1</id> 
    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-dependency-plugin</artifactId> 
     <executions> 
     <execution> 
      <id>copy-dependency</id> 
      <phase>prepare-package</phase> 
      <goals> 
      <goal>copy</goal> 
      </goals> 
      <configuration> 
      <artifactItems> 
       <artifactItem> 
       <groupId>name.seller.rich</groupId> 
       <artifactId>artifact1</artifactId> 
       <version>1.0.0</version> 
       <type>jar</type> 
       <overWrite>false</overWrite> 
       </artifactItem> 
      </artifactItems> 
      <outputDirectory>${project.build.directory}/deploy-staging</outputDirectory> 
      <overWriteReleases>false</overWriteReleases> 
      </configuration> 
     </execution> 
     </executions> 
    </plugin> 
    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-antrun-plugin</artifactId> 
     <executions> 
     <execution> 
      <id>ftp</id> 
      <phase>install</phase> 
      <configuration> 
      <tasks> 
       <ftp action="send" 
        server="${ftp.host}" remotedir="${ftp.remotedir}" 
        userid="${ftp.userid}" password="${ftp.password}" 
        depends="${ftp.depends}" verbose="${ftp.verbose}"> 
       <fileset dir="${project.build.directory} includes="deploy-staging/"/> 
       </ftp> 
      </tasks> 
      </configuration> 
      <goals> 
      <goal>run</goal> 
      </goals> 
     </execution> 
     </executions> 
    </plugin> 
    <properties> 
     <!--if the properties differ between targets, qualify them with the artifact name--> 
     <ftp.host>hostname</ftp.host> 
     <ftp.remotedir>/opt/path/to/install</ftp.remotedir> 
     <ftp.userid>user</ftp.userid> 
     <ftp.password>mypassword</ftp.password> 
     <ftp.depends>yes</ftp.depends> 
     <ftp.verbose>no</ftp.verbose>   
    </properties> 
    </profile> 
</profiles> 
+0

这应该工作。个人资料是我一直想念的。这里有另一个想法应该帮助“打嗝” - 而不是定义子项目,因为模块将这些定义为父POM中的依赖关系,并使用依赖插件将工件JAR提取到其目标。这会工作吗? – Bostone 2009-09-10 17:09:31

+0

我忘了提及,在我们的企业中,我们使用NAC来映射/访问任何服务器,就像映射的目录(都是Win/* NIX),所以在那种情况下确实不需要FTP或SSH – Bostone 2009-09-10 17:24:45

+0

,插件的副本目标是您所需要的,只需将outputDirectory属性修改为目标路径 – 2009-09-10 17:45:25

0

我会看看使用maven-assembly-plugin来做到这一点。

像这样的东西可以用来抓取子项目中的文件并将它们填充到输出目录中。

<assembly> 
    <id>xyzzy</id> 
    <formats> 
    <format>zip</format> 
    </formats> 
    <fileSets> 
    <fileSet> 
     <directory>../subproject1/target/</directory> 
     <outputDirectory>/foo</outputDirectory> 
     <includes> 
     <include>*.jar</include> 
     </includes> 
    </fileSet> 
    <fileSet> 
     <directory>../subproject1/target/html-output/</directory> 
     <outputDirectory>/foo</outputDirectory> 
     <includes> 
     <include>*.html</include> 
     <include>*.js</include> 
     <include>*.css</include> 
     </includes> 
    </fileSet>  
    <fileSet> 
     <directory>../subproject2/target/</directory> 
     <outputDirectory>/bar</outputDirectory> 
     <includes> 
     <include>**/**</include> 
     </includes> 
     <excludes> 
     <exclude>**/*.exclude-this</exclude> 
     </excludes> 
    </fileSet> 
    </fileSets> 
</assembly> 
+1

据我了解 - 程序集插件会创建一些存档(jar,war,zip) - 这对我不起作用 - 我需要将实际的文件/文件夹复制到目标。实际上 - maven-dependency插件似乎是更适合,因为它可以“解压缩”,但是因为我无法将子项目定义为模块和依赖关系,所以我要去的路线实际上是在Rich的建议之后 - 在安装阶段定义配置文件和复制文件。 – Bostone 2009-09-10 21:18:46

+0

dir将创建一个目录 – sal 2009-09-10 21:57:15

0

Maven的设计并不是真的将jar部署到远程位置;它的主要用途是编译和打包工件。程序集和依赖目标主要用于收集依赖和文件以打包成一个工件。

话虽如此,maven确实有一个使用称为wagon的组件的部署目标。这主要是为了部署到一个Maven仓库。有一个名为Cargo的插件,可用于将工件部署到远程服务器,但不会自动分解jar内容(它依赖目标应用服务器来完成所有工作)。您可能可以自己扩展Maven Wagon功能。

此外,可以打包一个自定义的生命周期,但这是进入一个相当低级别的maven mojo(双关语意)。

+0

我最终使用配置文件和antrun - 两全其美! – Bostone 2009-09-10 22:06:46

+0

太棒了!我猜Antrun是避开谁le Maven的要求;) – 2009-09-11 00:27:39

1

没有密码短语不起作用。

<profile> 
     <id>publish</id> 
     <build> 
      <plugins> 
       <plugin> 
        <artifactId>maven-antrun-plugin</artifactId> 
        <executions> 
         <execution> 
          <id>scp</id> 
          <phase>deploy</phase> 
          <configuration> 
           <tasks> 
            <scp todir="[email protected]:some/remote/dir" 
             sftp="true" 
             keyfile="${user.home}/.ssh/devel-deploy.id_dsa" 
             failonerror="false" 
             verbose="true" 
             passphrase="nopass" 
            > 
             <fileset dir="${project.build.directory}"> 
              <include 
               name="${project.build.finalName}.${project.packaging}"/> 
             </fileset> 
            </scp> 
           </tasks> 
          </configuration> 
          <goals> 
           <goal>run</goal> 
          </goals> 
         </execution> 
        </executions> 
        <dependencies> 
         <dependency> 
          <groupId>org.apache.ant</groupId> 
          <artifactId>ant-jsch</artifactId> 
          <version>1.9.4</version> 
         </dependency> 
        </dependencies> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 

但是,我最喜欢的是

<profile> 
     <id>upload-devel</id> 
     <build> 
      <plugins> 
       <plugin> 
        <artifactId>maven-antrun-plugin</artifactId> 
        <executions> 
         <execution> 
          <id>upload-devel</id> 
          <phase>deploy</phase> 
          <configuration> 
           <target> 
            <exec executable="rsync" failonerror="false"> 
             <arg value="-aiz" /> 
             <arg value="${project.build.directory}/${project.artifactId}.${project.packaging}" /> 
             <arg value="[email protected]:some/remote/dir/." /> 
            </exec> 
           </target> 
          </configuration> 
          <goals> 
           <goal>run</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 

虽然我不知道这是怎么了兼容不同的平台。

+0

为了使'rsync'以这种方式工作,我必须在''maven-antrun-plugin'后面加' 1.8'。没有它,它并没有真正执行命令,即使它确实打印了'[INFO]已执行的任务'。 – Tom 2017-01-25 15:26:13

5

下面的POM将帮助将jar文件从项目构建目录复制到远程SFTP/FTP服务器。

  1. 使用命令MVN安装-Dftp.password =密码

因为我想通过从命令提示符下密码,为了安全起见,我用-Dftp.password =密码 后执行上述命令将maven项目目标文件夹中的所有jar文件部署在server.com上的MAVEN文件夹中。

<plugin>   <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-antrun-plugin</artifactId> 

    <executions> 
     <execution> 
      <id>ftp</id> 
      <phase>install</phase> 
      <configuration> 
       <tasks> 
        <scp todir="[email protected]:/MAVEN/" 
         sftp="true" port="22" trust="true" password="${ftp.password}" 
         failonerror="false" verbose="true" passphrase=""> 
         <fileset dir="${project.build.directory}"> 
          <include name="*.jar" /> 
         </fileset> 
        </scp> 
       </tasks> 
      </configuration> 
      <goals> 
       <goal>run</goal> 
      </goals> 
     </execution> 
    </executions> 
    <dependencies> 
     <dependency> 
      <groupId>org.apache.ant</groupId> 
      <artifactId>ant-jsch</artifactId> 
      <version>1.9.4</version> 
     </dependency> 
    </dependencies> 

</plugin>