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

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

问题描述

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

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

  1. 对于每个孩子:jar-up 资源目录,包括项目依赖
  2. 上移到父项目
  3. 使用单个命令将所有创建的存档提取到各种远程目标(完整安装),其中可能包括 http 服务器、应用程序服务器、文件服务器等(主要是 *NIX).在子项目级别提供目的地
  4. 还应该可以从单个子项目中解压/复制(部分安装)

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

Files are not Java - mostly various scripts and HTML

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

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?

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

推荐答案

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

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?

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

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

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

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.

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

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.

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

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天全站免登陆