我有一些配置文件和各种文档,我想使用Maven2将它们从开发环境复制到dev-server目录。奇怪的是,Maven在这项任务上似乎并不强大。

以下是一些选项:

简单地使用Maven中的复制任务

< =“src /游戏/资源/文件副本配置。财产“tofile = ${项目配置}-配置服务器。财产" - >

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.

所有这些似乎都是不必要的特别任务:Maven应该擅长于毫不费力地完成这些标准任务。

任何建议吗?


当前回答

复制任意文件的一种通用方法是利用Maven Wagon传输抽象。它可以通过文件、HTTP、FTP、SCP或WebDAV等协议处理各种目的地。

有一些插件提供了通过使用Wagon复制文件的工具。最值得注意的是:

Out-of-the-box Maven Deploy Plugin There is the deploy-file goal. It it quite inflexible but can get the job done: mvn deploy:deploy-file -Dfile=/path/to/your/file.ext -DgroupId=foo -DartifactId=bar -Dversion=1.0 -Durl=<url> -DgeneratePom=false Significant disadvantage to using Maven Deploy Plugin is that it is designated to work with Maven repositories. It assumes particular structure and metadata. You can see that the file is placed under foo/bar/1.0/file-1.0.ext and checksum files are created. There is no way around this. Wagon Maven Plugin Use the upload-single goal: mvn org.codehaus.mojo:wagon-maven-plugin:upload-single -Dwagon.fromFile=/path/to/your/file.ext -Dwagon.url=<url> The use of Wagon Maven Plugin for copying is straightforward and seems to be the most versatile.

在上面的例子中,<url>可以是任何支持的协议。请参阅现有马车供应商的列表。例如

在本地复制文件:file:///copy/to 拷贝文件到SSH远程主机:scp://host:22/copy/to

上面的例子在命令行中传递插件参数。或者,可以直接在POM中配置插件。然后调用就像mvn deploy:deploy-file@configured-execution-id那样简单。或者它可以绑定到特定的构建阶段。

请注意,对于像SCP这样的协议,你需要在你的POM中定义一个扩展:

<build>
  [...]
  <extensions>
    <extension>
      <groupId>org.apache.maven.wagon</groupId>
      <artifactId>wagon-ssh</artifactId>
      <version>2.12</version>
    </extension>
  </extensions>

如果要复制到的目标需要身份验证,则可以通过服务器设置提供凭据。传递给插件的repositoryId/serverId必须与设置中定义的服务器相匹配。

其他回答

另一种方法是使用汇编插件将这些东西捆绑到工件中。然后你可以使用依赖插件在你想要的地方解包这些文件。在依赖插件中也有复制目标来复制工件。

总结一下上面的一些不错的答案:Maven被设计用来构建模块并将结果复制到Maven存储库。任何将模块复制到部署/安装程序输入目录的操作都必须在Maven核心功能的上下文之外进行,例如使用Ant/Maven复制命令。

复制任意文件的一种通用方法是利用Maven Wagon传输抽象。它可以通过文件、HTTP、FTP、SCP或WebDAV等协议处理各种目的地。

有一些插件提供了通过使用Wagon复制文件的工具。最值得注意的是:

Out-of-the-box Maven Deploy Plugin There is the deploy-file goal. It it quite inflexible but can get the job done: mvn deploy:deploy-file -Dfile=/path/to/your/file.ext -DgroupId=foo -DartifactId=bar -Dversion=1.0 -Durl=<url> -DgeneratePom=false Significant disadvantage to using Maven Deploy Plugin is that it is designated to work with Maven repositories. It assumes particular structure and metadata. You can see that the file is placed under foo/bar/1.0/file-1.0.ext and checksum files are created. There is no way around this. Wagon Maven Plugin Use the upload-single goal: mvn org.codehaus.mojo:wagon-maven-plugin:upload-single -Dwagon.fromFile=/path/to/your/file.ext -Dwagon.url=<url> The use of Wagon Maven Plugin for copying is straightforward and seems to be the most versatile.

在上面的例子中,<url>可以是任何支持的协议。请参阅现有马车供应商的列表。例如

在本地复制文件:file:///copy/to 拷贝文件到SSH远程主机:scp://host:22/copy/to

上面的例子在命令行中传递插件参数。或者,可以直接在POM中配置插件。然后调用就像mvn deploy:deploy-file@configured-execution-id那样简单。或者它可以绑定到特定的构建阶段。

请注意,对于像SCP这样的协议,你需要在你的POM中定义一个扩展:

<build>
  [...]
  <extensions>
    <extension>
      <groupId>org.apache.maven.wagon</groupId>
      <artifactId>wagon-ssh</artifactId>
      <version>2.12</version>
    </extension>
  </extensions>

如果要复制到的目标需要身份验证,则可以通过服务器设置提供凭据。传递给插件的repositoryId/serverId必须与设置中定义的服务器相匹配。

我只能假设你的${project.server. server。Config}属性是自定义的,并且在标准目录布局之外。

如果是,那么我将使用复制任务。

为了复制一个文件使用:

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>copy-resource-one</id>
                    <phase>install</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>

                    <configuration>
                        <outputDirectory>${basedir}/destination-folder</outputDirectory>
                        <resources>
                            <resource>
                                <directory>/source-folder</directory>
                                <includes>
                                    <include>file.jar</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
           </executions>
        </plugin>

为了复制带有子文件夹的文件夹,请使用下面的配置:

           <configuration>
              <outputDirectory>${basedir}/target-folder</outputDirectory>
              <resources>          
                <resource>
                  <directory>/source-folder</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>              
            </configuration>