在多模块项目中从 Maven 执行脚本

发布于 2024-08-27 01:19:27 字数 1814 浏览 4 评论 0原文

我有这个多模块项目。

在每个构建的开始,我想运行一些bat 文件。

因此,我执行了以下操作:

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

当我从根目录运行 mvn verify -Pdeploy-db 时,我会在每个模块中一遍又一遍地执行此脚本。

我希望它只在根模块中执行一次。

我缺少什么?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月蹉跎了容颜 2024-09-03 01:19:27

我可能是错的,但是当您将插件添加到 部分时,每个子模块都会继承它并“运行”它。

我认为您应该将 exec-maven-plugin 及其 移至正常的 部分。

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.

笙痞 2024-09-03 01:19:27

因此,您遇到的问题是您试图在父 POM 中做某事。这不是 Maven 中父 pom 的设计方式(即不是“maven 方式”)。您应该只在“叶节点”pom 中执行操作,父级仅用于聚合并放置应在每个子级中重用的共享行为。

因此,如何调用脚本的简单答案是分析子级之间的依赖关系,以确定首先需要发生哪个(如果需要强制执行此操作,则施加依赖关系),然后将插件添加到该子级。如果由于某种原因它不太适合该子项,您可以创建另一个只执行此操作的子项。

附带说明一下,永远不要在 Maven 中引用相对文件路径。您正在使用“../database/schemas/import_databases.bat”。如果 import_databases.bat 不在项目目录中,那么假设它在父目录中就会造成混乱。您应该使用类似“${basedir}/src/main/scripts/import_databases.bat”的内容

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文