使用Maven进行部署 [英] Using Maven for deployment

查看:152
本文介绍了使用Maven进行部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个项目的任务是使用Maven的4个嵌套子项目:


  1. 对于每个孩子:jar-up资源目录,包括项目依赖关系

  2. 移动到父项目

  3. 使用单个命令将所有创建的归档解压缩到各个远程目标(完全安装)中,可能包括http服务器,应用服务器,文件服务器等(主要是* NIX)。目的地在子项目级别提供

  4. 还可以从个别子项目(部分安装)中解压缩/复制

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



我正在看各种插件来帮助任务:汇编,依赖, antrun,解压缩。依赖关系看起来很有希望,但是我不仅需要解压缩依赖项,还可以解压缩(子)项目内容。此外,由于我不能真正紧密的操作Maven生命周期如何触发远程安装? mvn依赖:unpack?这不是很描述或直观。是否可以创建一个自定义的目标(例如:项目:安装)而不需要编写插件?



使用Maven是公司的标准,所以请不要提供替代品 - 我我几乎坚持我所拥有的

解决方案

好的,我认为以下可能会做你所需要的。这种方法的缺点是在执行后续版本时,每个部署之间将有一个间隔。这是可以接受的吗?



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



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



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

  ;简档> 
<个人资料>
< id> publish< / id>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-antrun-plugin< / artifactId>
<执行>
< 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>运行< / goal>
< / goals>
< / execution>
< / executions>
<依赖关系>
<依赖关系>
< groupId> commons-net< / groupId>
< artifactId> commons-net< / artifactId>
< version> 1.4.1< / version>
< / dependency>
<依赖关系>
< groupId> ant< / groupId>
< artifactId> ant-commons-net< / artifactId>
< version> 1.6.5< / version>
< / dependency>
<依赖关系>
< groupId> ant< / groupId>
< artifactId> ant-nodeps< / artifactId>
< version> 1.6.5< / version>
< / dependency>
< / dependencies>
< / plugin>
<属性>
< 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 。 p>

 <个人资料> 
<个人资料>
< id> deploy-artifact1< / id>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-dependency-plugin< / artifactId>
<执行>
< execution>
< id>拷贝依赖关系< / 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>
<执行>
< 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>运行< / goal>
< / goals>
< / execution>
< ; / executions>
< / plugin>
< properties>
<! - 如果属性在目标之间不同,则将其与工件名称限定 - >
< ftp.host> hostname< /ftp.host>
< ftp.remotedir> / opt / path / to / install< /ftp.remotedir>
< ftp.userid& /ftp.userid>
< ftp.password> mypassword< /ftp.password>
< ftp.depends> yes< /ftp.depends>
< ftp.verbose> no< ; /ftp.verbose>
< / properties>
< / profile>
< / profiles>


I have this task for the project with 4 nested subprojects using Maven:

  1. For each child: jar-up resource directory including project dependencies
  2. Move up to the parent project
  3. With a single command extract all created archives into various remote destinations (full install), that may include http server, app server, file server, etc. (mostly *NIX). Destination is provided on subproject level
  4. It should also be possible to unzip/copy from the individual subproject (partial install)

Files are not Java - mostly various scripts and HTML

I'm looking at the various plugins to help with the task: assembly, dependency, antrun, unzip. Dependency looks promising but I need to unzip not only dependency jars but the (sub)project content as well. Also since I can't really tight the operation to the Maven lifecycle how would I trigger remote install? mvn dependency:unpack? That's not very descriptive or intuitive. Is is possible to create a custom goal (e.g. project:install) without writing a plugin?

Using Maven is company standard so please do not offer alternatives - I'm pretty much stuck with what I have

解决方案

Ok, I think the following might do what you need. The drawback of this approach is that there will be an interval between each deployment as the subsequent build is executed. Is this acceptable?

Define a profile in each project with the same name (say "publish"). Within that profile you can define a configuration to use the antrun-plugin to deliver the files with FTP (see below).

In the parent project you'll have a modules element, defining each project as a module. If you run mvn install -P publish, each project will be built in turn with the publish profile enabled, and the final artifact published to the target during the install phase. If you need to deploy additional files, modify the include element accordingly.

Note the parameters for the FTP task have been set as properties, this allows them to be overridden from the command-line and/or inherited from the parent 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>


Update: based on your comment: You could use the dependency plugin to download each dependency, except that a parent can't have a dependency on a child, and it will be built before the child. It would have to be another project. you also need to have somewhere the information for where to deploy them to. At the moment you have the target information in the individual projects so it isn't accessible in the deployer project.

Taking this approach, you can define multiple profiles in the new project, one for each artifact. Each profile defines a dependency:copy execution to obtain the jar and an antrun execution for one of the projects. Common configuration (such as the dependencies for the antrun plugin) can be pulled out of the profiles. Also be aware that the properties will be merged if you define multiple profiles, so yo may need to qualify them with the artifact name, for example 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>  

这篇关于使用Maven进行部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆