首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Maven进行部署

使用Maven进行部署
EN

Stack Overflow用户
提问于 2009-09-10 15:23:32
回答 5查看 21.6K关注 0票数 23

我使用Maven对包含4个嵌套子项目的项目执行以下任务:

jar-up资源目录包括项目dependencies

  • Move到父项目
  1. 只需一个命令就可以解压所有创建的归档文件到各种远程目标(完全安装),其中可能包括http服务器、应用服务器、文件服务器等(主要是*NIX)。目标位置在子项目级
  2. 上提供,也可以从单个子项目(部分安装)

解压/复制

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

我正在寻找各种插件来帮助完成这项任务: assembly,dependency,antrun,unzip。依赖项看起来很有希望,但我不仅需要解压缩依赖项jars,还需要解压缩(子)项目内容。另外,由于我不能对Maven生命周期进行严格的操作,我如何触发远程安装呢?mvn依赖关系:解包?这不是很有描述性或直观性。是否可以在不编写插件的情况下创建自定义目标(例如project:install)?

使用Maven是公司的标准,所以请不要提供替代方案-我很难使用我已有的东西

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-09-10 16:41:19

好的,我认为下面的代码可能会满足你的需求。这种方法的缺点是,在执行后续构建时,每次部署之间会有一段时间间隔。这可以接受吗?

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

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

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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>  
票数 15
EN

Stack Overflow用户

发布于 2015-06-27 03:13:07

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

  1. 使用命令mvn install -Dftp.password=password

由于出于安全原因,我想从命令提示符传递密码,所以在执行上述命令后,我使用了-Dftp.password=password,所有来自maven项目目标文件夹的jar文件都将部署到server.com上的MAVEN文件夹中

代码语言:javascript
复制
    <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>
票数 5
EN

Stack Overflow用户

发布于 2014-07-01 20:08:51

如果没有口令,则无法工作。

代码语言:javascript
复制
    <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>

然而,我最喜欢的是

代码语言:javascript
复制
    <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>

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1405979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档