我有一些配置文件和各种文档,我想使用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应该擅长于毫不费力地完成这些标准任务。

任何建议吗?


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

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


好吧,maven不应该擅长执行细粒度的任务,它不是bash或ant之类的脚本语言,而是声明性语言——您说—我需要一场战争或一只耳朵,然后您就得到了它。然而,如果你需要自定义战争或耳朵应该看起来像内部,你有一个问题。它不像ant那样是程序性的,而是声明性的。 这在一开始有一些好处,但到最后可能会有很多坏处。

我猜最初的概念是有好的插件,“只是工作”,但现实是不同的,如果你做非标准的东西。

然而,如果你在poms上投入足够的精力和少量的自定义插件,你会得到一个更好的构建环境,比如ant(当然这取决于你的项目,但对于更大的项目来说越来越正确)。


不要回避Antrun插件。仅仅因为有些人倾向于认为Ant和Maven是对立的,但事实并非如此。如果你需要执行一些不可避免的一次性定制,请使用copy任务:

<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?

再说一次,我相信有更好的办法。


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


上面的蚂蚁解决方案是最容易配置的,但我有幸使用了Atlassian的maven-upload-plugin。我找不到好的文档,下面是我如何使用它:

<build>
  <plugin>
    <groupId>com.atlassian.maven.plugins</groupId>
    <artifactId>maven-upload-plugin</artifactId>
    <version>1.1</version>
    <configuration>
       <resourceSrc>
             ${project.build.directory}/${project.build.finalName}.${project.packaging}
       </resourceSrc>
       <resourceDest>${jboss.deployDir}</resourceDest>
       <serverId>${jboss.host}</serverId>
       <url>${jboss.deployUrl}</url>
     </configuration>
  </plugin>
</build>

像“${jboss.”上面引用的Host}定义在我的~/.m2/settings.xml中,并使用maven配置文件激活。这个解决方案并不局限于JBoss,这只是我给变量命名的方式。我有开发,测试和现场的资料。因此,要在测试环境中上传我的耳朵到jboss实例,我将执行:

mvn upload:upload -P test

下面是settings.xml的一个片段:

<server>
  <id>localhost</id>
  <username>username</username>
  <password>{Pz+6YRsDJ8dUJD7XE8=} an encrypted password. Supported since maven 2.1</password>
</server>
...
<profiles>
  <profile>
    <id>dev</id>
    <properties>
      <jboss.host>localhost</jboss.host> 
      <jboss.deployDir>/opt/jboss/server/default/deploy/</jboss.deployDir>
      <jboss.deployUrl>scp://root@localhost</jboss.deployUrl>
    </properties>
  </profile>
  <profile>
    <id>test</id>
    <properties>
       <jboss.host>testserver</jboss.host>
       ...

注: 有这个插件的Atlassian maven repo在这里:https://maven.atlassian.com/public/

我建议下载源代码,并查看其中的文档,以了解插件提供的所有功能。

`


<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.3</version>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include> **/*.properties</include>
            </includes>
        </resource>
    </resources>
    ...
</build>

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


我能够拼凑出一些不同的来源来回答这个问题:

...
<repository>
    <id>atlassian</id>
    <name>Atlassian Repo</name>
    <url>https://maven.atlassian.com/content/repositories/atlassian-public</url>
</repository>
...
<dependency>
    <groupId>com.atlassian.maven.plugins</groupId>
    <artifactId>maven-upload-plugin</artifactId>
    <version>1.1</version>
</dependency>
...
<plugin>
    <groupId>com.atlassian.maven.plugins</groupId>
    <artifactId>maven-upload-plugin</artifactId>
    <version>1.1</version>
    <configuration>
        <serverId>jira-repo</serverId>
        <resourceSrc>
            ${project.build.directory}/${project.build.finalName}.${project.packaging}
        </resourceSrc>
        <resourceDest>opt/jira/webapps</resourceDest> <!-- note: no leading slash -->
        <url>scp://root@jira</url>
    </configuration>
</plugin>
...

从~ / .m2 / settings.xml:

...
<servers>
  <server>
    <id>jira-repo</id>
    <username>myusername</username>
    <password>mypassword</password>
  </server>
</servers>
...

然后运行命令:(-X用于调试)

mvn -X upload:上传


maven依赖插件为我节省了大量处理ant任务的时间:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>install-jar</id>
            <phase>install</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>...</groupId>
                        <artifactId>...</artifactId>
                        <version>...</version>
                    </artifactItem>
                </artifactItems>
                <outputDirectory>...</outputDirectory>
                <stripVersion>true</stripVersion>
            </configuration>
        </execution>
    </executions>
</plugin>

依赖项:copy是documentend,并有更多有用的目标,如unpack。


我对copy-maven-plugin有很好的经验。与maven-resources-plugin相比,它具有更方便和简洁的语法。


为了复制一个文件使用:

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

对于简单的复制任务,我可以推荐copy-rename-maven-plugin。它是直接和简单的使用:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>com.coderplus.maven.plugins</groupId>
        <artifactId>copy-rename-maven-plugin</artifactId>
        <version>1.0</version>
        <executions>
          <execution>
            <id>copy-file</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <sourceFile>src/someDirectory/test.environment.properties</sourceFile>
              <destinationFile>target/someDir/environment.properties</destinationFile>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

如果你想复制多个文件,替换<sourceFile>…</destinationFile> part with

<fileSets>
  <fileSet>
    <sourceFile>src/someDirectory/test.environment.properties</sourceFile>
    <destinationFile>target/someDir/environment.properties</destinationFile>
  </fileSet>
  <fileSet>
    <sourceFile>src/someDirectory/test.logback.xml</sourceFile>
    <destinationFile>target/someDir/logback.xml</destinationFile>
  </fileSet>                
</fileSets>

此外,如果需要,您可以在多个阶段指定多个执行,第二个目标是“重命名”,它只是做它所说的,而其余配置保持不变。要了解更多用法示例,请参阅用法页面。

注意:这个插件只能复制文件,不能复制目录。(感谢@james。Garriss发现了这一局限性。)


复制任意文件的一种通用方法是利用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必须与设置中定义的服务器相匹配。


If someone wants total control over the path of the source and destination paths, then using maven-antrun-plugin's copy task is the best option. This approach will allow you to copy between any paths on the system, irrespective of the concerned paths being within the mvn project or not. I had a situation where I had to do some unusual stuff like copy generated source files from target directory back to the src directory for further processing. In my situation, this was the only option that worked without fuss. Sample code snippet from pom.xml:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <executions>
            <execution>
                <phase>process-resources</phase>
                <configuration>
                <tasks>
                    <copy file="${basedir}/target/myome/minifyJsSrcDir/myome.min.js" todir="${basedir}/src/main/webapp/app/minifyJsSrcDir"/>
                </tasks>
                </configuration>
                <goals>
                <goal>run</goal>
                </goals>
            </execution>
        </executions>
</plugin>