使用 Maven 复制文件的最佳实践 [英] Best practices for copying files with Maven

查看:38
本文介绍了使用 Maven 复制文件的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有配置文件和各种文档,我想使用 Maven2 从开发环境复制到开发服务器目录.奇怪的是,Maven 似乎并不擅长这项任务.

一些选项:

  • 在 Maven 中简单使用 copy 任务
<块引用>

  • 使用 Ant 插件从 Ant 执行copy.

    • 构建一个 zip 类型的工件,以及 POM 的主要"工件(通常是 jar 类型),然后 解压em> 将该工件从存储库复制到目标目录中.

    • maven-resources 插件,如下所述.

    • Maven Assembly 插件——但这似乎需要很多手动定义,当我想简单地和传统地"做事情时.

    • 此页面甚至展示了如何构建插件复制!

    • maven-upload 插件,如下所述.

    • maven-dependency-plugincopy,如下所述.


所有这些似乎都是不必要的临时:Maven 应该擅长处理这些标准任务,而无需大惊小怪.

有什么建议吗?

解决方案

不要回避 Antrun 插件.正因为有些人倾向于认为Ant和Maven是对立的,其实不然.如果您需要执行一些不可避免的一次性定制,请使用复制任务:

[...]<构建><插件>[...]<插件><artifactId>maven-antrun-plugin</artifactId><执行><执行><阶段>部署</阶段><配置><目标><!--在这里放置任何 Ant 任务.你可以添加任何东西您可以在 <target> 之间添加和</目标>在一个构建.xml.--></目标></配置><目标><目标>跑</目标></目标></执行></执行></插件></插件></build>[...]</项目>

在回答这个问题时,我关注的是您所问问题的细节.如何复制文件?这个问题和变量名让我想到了一个更大的问题,比如:有没有更好的方法来处理服务器配置?"使用 Maven 作为构建系统来生成可部署的工件,然后在单独的模块中或完全在其他地方执行这些自定义.如果您共享了更多的构建环境,那么可能有更好的方法 - 使用插件来配置多个服务器.你能附上一个在服务器根目录中解压缩的程序集吗?你用的是什么服务器?

再说一次,我相信有更好的方法.

I have config files and various documents that I want to copy from the dev environment to the dev-server directory using Maven2. Strangely, Maven does not seem strong at this task.

Some of the options:

  • Simple use a copy task in Maven

<copy file="src/main/resources/config.properties" tofile="${project.server.config}/config.properties"/>

  • Use the Ant plugin to execute copy from Ant.

    • Construct an artifact of type zip, alongside the "main" artifact of the POM which is usually of type jar, then unpack that artifact from the repository into the target directory.

    • maven-resources plugin, as mentioned below.

    • Maven Assembly plugin -- but this seems to require a lot of manual definitions, when I want to do things simply and "conventionally."

    • This page even shows how to build a plugin to do copying!

    • maven-upload plugin, as mentioned below.

    • maven-dependency-plugin with copy, as mentioned below.


All these seem needlessly ad hoc: Maven is supposed to excel at doing these standard tasks without fuss and bother.

Any advice?

解决方案

Don't shy away from the Antrun plugin. Just because some people tend to think that Ant and Maven are in opposition, they are not. Use the copy task if you need to perform some unavoidable one-off customization:

<project>
  [...]
  <build>
    <plugins>
      [...]
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <phase>deploy</phase>
            <configuration>
              <target>

                <!--
                  Place any Ant task here. You can add anything
                  you can add between <target> and </target> in a
                  build.xml.
                -->

              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

In answering this question, I'm focusing on the details of what you asked. How do I copy a file? The question and the variable name lead me to a larger questions like: "Is there a better way to deal with server provisioning?" Use Maven as a build system to generate deployable artifact, then perform these customizations either in separate modules or somewhere else entirely. If you shared a bit more of your build environment, there might be a better way - there are plugins to provision a number of servers. Could you attach an assembly that is unpacked in the server's root? What server are you using?

Again, I'm sure there's a better way.

这篇关于使用 Maven 复制文件的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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