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

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

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

其他回答

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

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

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

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

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

...
<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:上传

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

为了复制一个文件使用:

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