如何使用 Maven 将文件和目录复制为 post-build 或在特定位置预构建?

How to Copy files and directories using Maven as post-build or pre-build in a specific location?

我有一个 parent maven pom 和一个 child pom。我必须在 parent pom 之后但在 child pom 之前做一些目录复制。我怎样才能做到这一点?

Maven 定义了一个生命周期列表,当您告诉 Maven 构建您的项目时,这些生命周期将按顺序执行。有关这些阶段的有序列表,请参阅 Lifecycles Reference

如果你运行

mvn clean test

Maven 执行所有生命周期直至并包括 test

假设您有一个 multi-module Maven 项目并且 sub-module 需要在 运行 测试之前复制 parent 模块生成的资源,您可以使用maven-resources-plugin 在您的 child 模块中并将其绑定到 generate-resources 阶段:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-resources-from-parent</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/generated-resources
                </outputDirectory>
                <resources>
                    <resource>
                        <directory>../generated-resources</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

generate-resources阶段在test阶段之前执行。所以如果你 运行

mvn clean test

在您的 parent 模块的目录中,这将复制从 <parent>/generated-resources<child>/target/generated-resources 在您的 parent 模块 运行 和 child 模块 运行 是它的测试。