3

I have this multi-module project.

In the beginning of each build I would like to run some bat file.

So i did the following:

<profile>
            <id>deploy-db</id>
            <build>
                <plugins>
 <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1.1</version>
        </plugin>
                </plugins>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>exec-maven-plugin</artifactId>
                            <version>1.1.1</version>
                            <executions>
                                <execution>
                                    <phase>validate</phase>
                                    <goals>
                                        <goal>exec</goal>
                                    </goals>
                                    <inherited>false</inherited>
                                </execution>
                            </executions>
                            <configuration>
                                <executable>../database/schemas/import_databases.bat</executable>
                            </configuration>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>

when i run the mvn verify -Pdeploy-db from the root I get this script executed over and over again in each of my modules.

I want it to be executed only once, in the root module.

What is there that I am missing ?

Thanks

matt b
  • 138,234
  • 66
  • 282
  • 345
Roman
  • 7,933
  • 17
  • 56
  • 72
  • 1
    That's a "good" question. The problem here is that **the effects** of the profile are inherited and the maven exec plugin gets thus also executed for all children. Need to dig that (but I'm afraid there won't be a clean solution). – Pascal Thivent Mar 29 '10 at 08:54
  • No really , I love Maven it is great in its idea ( like Communism :-) ). But when it comes to simple things like that one , it absolutely fails.... It seems that I will have to write a custom plugin to get this done clean. – Roman Mar 29 '10 at 14:36
  • Does this answer your question? [Maven profile - How to run plugin once for parent and more for modules?](https://stackoverflow.com/questions/36216342/maven-profile-how-to-run-plugin-once-for-parent-and-more-for-modules) – Lordroran Mar 15 '23 at 11:12

2 Answers2

1

I might be mistaken but when you add a plugin to the <pluginManagement> section each and every sub-module inherits it and "runs" it.

I think that you should move you exec-maven-plugin and its <execution> to the normal <plugins> section.

Yaneeve
  • 4,751
  • 10
  • 49
  • 87
  • 1
    This is not quite right. PluginManagment only configures the plugin. Not telling him anything about being applied. In short , that does not work ... – Roman Mar 28 '10 at 12:57
  • when you add the execution section - that does say something about its invocation... – Yaneeve Mar 28 '10 at 13:03
  • 1
    @Roman, run `mvn help:effective-pom` from the sub-modules to see what is inherited/what is applied and what is not – matt b Mar 28 '10 at 13:13
  • 1
    No, the problem is not the `pluginManagement` (doesn't do anything by itself). – Pascal Thivent Mar 28 '10 at 16:53
0

So the issue you're having is that you're trying to do something in the parent POM. That's not how parent poms are designed in maven (i.e. not "the maven way"). You should only perform actions in "leaf node" poms, the parents are just for aggregation and putting shared behavior that should be reused in each child.

So the simple answer at how to call your script is to analyze the dependencies between your children to determine which needs to happen first (and impose dependency if necessary to enforce this), then add the plugin to that child. If it doesn't fit well in that child for some reason, you can make another child that just performs this action.

On a side note, never reference relative file paths in maven. You're using "../database/schemas/import_databases.bat". If import_databases.bat isn't inside the project directory then assuming it's in the parent directory is asking for a mess. You should instead use something like "${basedir}/src/main/scripts/import_databases.bat"

Jared
  • 1,887
  • 3
  • 20
  • 45