1

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?

Michael A
  • 9,480
  • 22
  • 70
  • 114
Jeevan
  • 447
  • 2
  • 8
  • 19
  • Have you tried something already? Or something that is similar that should be improved? Makes it easier for us if we have something we can start from, and not have to start from scratch. – ndsmyter Jul 20 '15 at 06:27
  • I have not tried any, but found something here (http://stackoverflow.com/questions/586202/best-practices-for-copying-files-with-maven) which says copy can be done. Now searching for how to do pre or post build tasks. – Jeevan Jul 20 '15 at 06:43

1 Answers1

2

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.

hzpz
  • 7,536
  • 1
  • 38
  • 44
  • Do we need to put this in Child POM or Parent POM? – Jeevan Jul 20 '15 at 09:56
  • The snippet needs to be in your child pom. I updated my answer accordingly. – hzpz Jul 20 '15 at 10:07
  • If Child POM, how to make it as pre-build or post-build? – Jeevan Jul 20 '15 at 10:07
  • In Child POM it runs some test cases, I want this to happen before those tests run. This is my child POM http://paste.ubuntu.com/11908479/ – Jeevan Jul 20 '15 at 10:11
  • I updated my answer again. Hope it clarifies the concept of lifecycles a bit. – hzpz Jul 20 '15 at 10:23
  • It isn't working, I think I have to find a way to say that this need to happen just after mvn clean. It is trying to do my build first. – Jeevan Jul 20 '15 at 10:28
  • But this code you gave works in isolation, i want to bring it as a pre build. Can some one help in that. Thanks @hzpz – Jeevan Jul 20 '15 at 10:34
  • It is supposed to do (almost) exactly that. Please post both your parent and child pom.xml and the Maven command you are using. – hzpz Jul 20 '15 at 10:34
  • This is the one you gave which copy works http://paste.ubuntu.com/11908583/ but it should be like this http://paste.ubuntu.com/11908596/ which is copying the files as post build not pre build which I want. The parent pom has nothing except calling this as a module. – Jeevan Jul 20 '15 at 10:40
  • You lost me. Which is your parent POM and which is your child POM? Why do you need to copy the resources after the parent if it does nothing? – hzpz Jul 20 '15 at 10:56
  • This is my parent POM http://paste.ubuntu.com/11908696/ This is child POM http://paste.ubuntu.com/11908596/ In Child pom there are two plugins, how to make one plugin which is our copy run first and the other plugin run 2nd – Jeevan Jul 20 '15 at 11:09
  • Ah, now that seems like a different question than the one in your original post. The rcptt plugin also binds itself to the `generate-resources`. If two (or more) plugins bind to the same phase, the order in pom.xml defines the execution order. Specify `maven-resources-plugin` before `ci4rcptt-maven-plugin` and Maven will run the plugins in the right order. – hzpz Jul 20 '15 at 11:25
  • This is my current POM http://paste.ubuntu.com/11913560/ where I have arranged in proper order. When I comment out (remove from compilation) one of the plugin, the other one is working fine. But when I include both of them, I get this http://paste.ubuntu.com/11913568/ which shows the copy has not happened and it is trying to access the directory where copied folder does not exist. – Jeevan Jul 21 '15 at 09:45
  • Ah, it seems the rcptt plugin is hijacking the `default-resources` execution which for some reason always comes first. Try to bind the resources plugin to the `initialize` phase and see if that works: `initialize`. – hzpz Jul 21 '15 at 09:52
  • It says we have a duplicate phase http://paste.ubuntu.com/11913615/ - sorry my mistake - im running it, its compiling i will update once its finished. – Jeevan Jul 21 '15 at 10:04
  • As the error message says: you have a duplicate `` tag in your pom.xml. You must replace the old `generate-resources` with the new `initialize`, don't just add it. – hzpz Jul 21 '15 at 10:06
  • Thank you so much hzpz . It hepled a lot. – Jeevan Jul 21 '15 at 11:45
  • what are the other phase than initialize? where can i find those? – Jeevan Aug 12 '15 at 08:58
  • See the link that I included in my answer: http://maven.apache.org/ref/3.3.3/maven-core/lifecycles.html – hzpz Aug 12 '15 at 09:00
  • Actually it is not copying any .* files, i.e., hidden linux files. How to do that ? – Jeevan Aug 12 '15 at 09:43
  • Please create another question for that. – hzpz Aug 12 '15 at 09:47
  • Sure, http://stackoverflow.com/questions/31961775/maven-copy-files-and-folders-plugin-not-copeying-linux-dot-hidden-files thanks – Jeevan Aug 12 '15 at 09:53