Search code examples
javaeclipsemavenmaven-plugintycho

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


I have a parent maven pom and a child pom. I have to do some copying directory after parent pom but before child pom. How can i do that?


Solution

  • Maven defines a list of lifecycles that are executed in order when you tell Maven to build your project. See Lifecycles Reference for an ordered list of those phases.

    If you run

    mvn clean test
    

    Maven executes all lifecycles up to and including test.

    Assuming you have a multi-module Maven project and a sub-module needs to copy resources generated by the parent module before running its tests, you can use the maven-resources-plugin in your child module and bind it to the generate-resources phase:

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

    The generate-resources phase is executed before the test phase. So if you run

    mvn clean test
    

    in the directory of your parent module, this will copy everything from <parent>/generated-resources to <child>/target/generated-resources after your parent module ran and before the child module runs its tests.