Deployment 使用Maven进行部署

Deployment 使用Maven进行部署,deployment,maven-2,Deployment,Maven 2,我使用Maven为具有4个嵌套子项目的项目执行此任务: 对于每个子级:jar-up资源目录,包括项目依赖项 上移到父项目 只需一个命令,即可将所有创建的归档文件提取到各种远程目标(完整安装),其中可能包括http服务器、应用程序服务器、文件服务器等(主要是*NIX)。目的地在子项目级别提供 还可以从单个子项目解压/复制(部分安装) 文件不是Java—主要是各种脚本和HTML 我正在研究各种插件来帮助完成这项任务:汇编、依赖、antrun、解压。依赖项看起来很有希望,但我不仅需要解压依赖项jar,

我使用Maven为具有4个嵌套子项目的项目执行此任务:

  • 对于每个子级:jar-up资源目录,包括项目依赖项
  • 上移到父项目
  • 只需一个命令,即可将所有创建的归档文件提取到各种远程目标(完整安装),其中可能包括http服务器、应用程序服务器、文件服务器等(主要是*NIX)。目的地在子项目级别提供
  • 还可以从单个子项目解压/复制(部分安装)
  • 文件不是Java—主要是各种脚本和HTML

    我正在研究各种插件来帮助完成这项任务:汇编、依赖、antrun、解压。依赖项看起来很有希望,但我不仅需要解压依赖项jar,还需要解压(子)项目内容。此外,由于我不能将操作严格限制在Maven生命周期中,我如何触发远程安装?mvn依赖项:解包?这不是很直观的描述。是否可以在不编写插件的情况下创建自定义目标(例如project:install)


    使用Maven是公司的标准,所以请不要提供替代方案-我非常坚持我所拥有的

    好的,我认为以下内容可能会满足您的需要。这种方法的缺点是,在执行后续构建时,每次部署之间都会有一个间隔。这可以接受吗

    在每个项目中定义一个同名的概要文件(比如“发布”)。在该配置文件中,您可以定义一个配置,以使用antrun插件通过FTP交付文件(见下文)

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

    注意,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>
    
    我将考虑使用

    类似的东西可以用来从子项目中获取文件并将它们填充到输出目录中

    <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>
    
    
    xyzzy
    拉链
    ../子项目1/目标/
    /福
    *jar先生
    ../subproject1/target/html输出/
    /福
    *.html
    *.js
    *.css
    ../子项目2/目标/
    /酒吧
    **/**
    **/*.排除此项
    
    Maven并不是真正设计用来将JAR部署到远程位置的;它的主要用途是编译和打包工件。程序集和依赖项目标主要用于收集要打包到工件中的依赖项和文件

    话虽如此,maven确实有一个部署目标,它使用了一个名为Wang的组件。这主要是为了部署到maven存储库。有一个名为Cargo的插件,可以用来将工件部署到远程服务器,但它本身不会分解jar内容(这一切都依赖于目标应用服务器)。您可以自己扩展Maven旅行车的功能


    另外,可以打包一个定制的生命周期,但这正进入一些相当低级的maven mojo(双关语)。

    如果没有密码短语就无法工作

        <profile>
            <id>publish</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>scp</id>
                                <phase>deploy</phase>
                                <configuration>
                                    <tasks>
                                        <scp todir="user@host: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>
    
    
    出版
    maven antrun插件
    scp
    部署
    跑
    org.apache.ant
    ant jsch
    1.9.4
    
    然而,我最喜欢的是

        <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="user@host:some/remote/dir/." />
                                        </exec>
                                    </target>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    
    
    上传文件
    maven antrun插件
    上传文件
    部署
    跑
    

    虽然我不知道它在不同平台上的兼容性如何。

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

  • 使用命令mvn install-Dftp.password=password
  • 由于出于安全原因,我想从命令提示符传递密码,所以我使用了-Dftp.password=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="user@server.com:/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>
    
    org.apache.maven.plugins
    maven antrun插件
    ftp
    安装
    
        <plugin>          <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
    
        <executions>
            <execution>
                <id>ftp</id>
                <phase>install</phase>
                <configuration>
                    <tasks>
                        <scp todir="user@server.com:/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>