在 Maven 多模块项目中,如何禁用一个子项目中的插件?

发布于 2024-12-10 04:10:50 字数 500 浏览 3 评论 0原文

我有一个 Maven 多模块项目(天啊,我已经在这个网站上写过太多次这种打开方式了)。几乎所有模块(即其中包含代码的模块)都应该运行 maven-site-plugin 来生成有关代码覆盖率等的报告。这些模块有详细的共享配置 - 报告运行、覆盖/排除某些插件的哪些文件等。

但是,有一些模块处理打包——运行程序集插件来生成 tarball 等。这些从运行站点报告中没有任何好处——有没有要分析的代码,没有要报告的测试。

因此,我有很多模块需要共享插件配置,还有一些模块需要不运行插件,最好根本不运行。如果我将插件放在父 POM 的 部分中,我可以执行前者(共享配置),但当我需要时,我似乎无法关闭该插件案件。如果我将配置推送到每个模块自己的 POM,我可以执行后者(避免运行插件),但在这种情况下我找不到共享配置信息的好方法。

我想要的——共享配置,对于有时被子模块禁用的插件——甚至可能吗?如果是这样,怎么办?

I have a maven multi-module project (boy, I've written that opening way too many times on this site). Almost all the modules (that is, the ones that have code in them) should run the maven-site-plugin to generate reports about code coverage, etc. These have a detailed shared configuration -- which reports to run, which files to cover/exclude for certain plugins, etc.

However, there are a few modules that deal with packaging -- running the assembly plugin to generate a tarball, etc. These gain nothing from running a site report -- there's no code to analyze, no tests to report on.

So I have a lot of modules that need to share plugin configuration, and a few modules that need to not run the plugin, preferably at all. I can do the former (share configuration) if I put the plugin in the <build> section of the parent POM, but I can't seem to turn off the plugin when I need to in this case. I can do the latter (avoid running the plugin) if I push configuration down to each module's own POM, but I can't figure out a good way to share the configuration information in this case.

Is what I want -- shared configuration, for a plugin that's sometimes disabled by a child module -- even possible? If so, how?

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

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

发布评论

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

评论(2

阳光①夏 2024-12-17 04:10:50

通过“运行插件”,我假设您的意思是该插件绑定到生命周期阶段,并且您希望在某些模块中取消绑定它。首先,您可以考虑更改 POM 继承,以便不需要插件的模块有一个父级,而需要插件的模块有不同的父级。如果您不想这样做,那么您可以在子模块中显式地将执行阶段设置为“无”。例如,如果您有这样的父pom配置:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
        <execution>
            <id>i-do-something</id>
            <phase>initialize</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                ... lots of configuration
            </configuration>
        </execution>
    </executions>
</plugin>

那么在子模块中,您可以这样做:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
        <execution>
            <id>i-do-something</id>
            <phase/>
        </execution>
    </executions>
</plugin>

因为它是相同的插件和相同的执行id,所以它会覆盖父级中指定的配置,现在插件未绑定到子项目的一个阶段。

By "run the plugin", I'm assuming you mean that the plugin is bound to a lifecycle phase, and you'd like to unbind it in some modules. First, you could consider changing your POM inheritance so that the modules that don't need the plugins have one parent and the ones that do have a different parent. If you don't want to do that, then you can explicitly set the execution phase to "nothing" in a child module. E.g. if you had a parent pom configuration like this:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
        <execution>
            <id>i-do-something</id>
            <phase>initialize</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                ... lots of configuration
            </configuration>
        </execution>
    </executions>
</plugin>

Then in a child module, you could do this:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
        <execution>
            <id>i-do-something</id>
            <phase/>
        </execution>
    </executions>
</plugin>

Because it's the same plugin and the same execution id, it overrides the configuration specified in the parent, and now the plugin isn't bound to a phase in the child project.

有木有妳兜一样 2024-12-17 04:10:50

如果您希望在父 pom 中抑制的执行带有 id 标记,则 Ryan Stewart 的答案有效。但是,如果父 pom 没有使用 id 标记执行(当然,您无法编辑该父 pom),那么我发现执行以下操作会抑制父 pom 的操作。

  1. 首先将执行阶段设置为none
  2. 创建另一个执行,给它一个id 并在其中执行您需要它执行的操作。
  3. 运行 mvn help: effective-pom 来确认它已经正确地抑制了您需要从父 pom 中抑制的内容。

这是一个例子:
这就是我的父 pom 的样子:

    <plugin>
      <artifactId>maven-source-plugin</artifactId>
      <version>2.1.2</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>jar</goal>
          </goals>
        </execution>
      </executions>
      <inherited>true</inherited>
    </plugin>

我需要将目标更改为 jar-no-fork。请注意,父 pom 中的执行没有我可以用来禁用它的 id 。因此,这是添加到我的子 pom 中的内容:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>none</phase>
                    </execution>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

因此, effective-pom 的外观如下:

      <plugin>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.2.1</version>
        <executions>
          <execution>
            <phase>none</phase>
            <goals>
              <goal>jar</goal>
            </goals>
            <configuration>
              <archive>
                <compress>false</compress>
              </archive>
            </configuration>
          </execution>
          <execution>
            <id>attach-sources</id>
            <goals>
              <goal>jar-no-fork</goal>
            </goals>
            <configuration>
              <archive>
                <compress>false</compress>
              </archive>
            </configuration>
          </execution>
        </executions>
        <inherited>true</inherited>
        <configuration>
          <archive>
            <compress>false</compress>
          </archive>
        </configuration>
      </plugin>

这确保了目标 jar 永远不会运行,并且只有目标 jar-no-fork< /code> 执行——这就是我想要实现的目标。

Ryan Stewart's answer works if execution that you wish to suppress in the parent pom is tagged with an id. If, however, the parent pom doesn't tag the execution with an id (and, of course, you can't edit that parent pom) then I found that doing the following suppresses the parent pom's action.

  1. First set the phase of the execution to none
  2. Create another execution, give it an id and do in it what you need it to do.
  3. run mvn help:effective-pom to confirm that it has correctly suppressed what you needed suppressed from the parent pom.

Here's an example:
This is how my parent pom looked like:

    <plugin>
      <artifactId>maven-source-plugin</artifactId>
      <version>2.1.2</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>jar</goal>
          </goals>
        </execution>
      </executions>
      <inherited>true</inherited>
    </plugin>

I needed to change the goal to jar-no-fork. Note that the execution in parent pom doesn't have an id that I could use to disable it. So here's what added to my child pom:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>none</phase>
                    </execution>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

As a result this is how the effective-pom looks like:

      <plugin>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.2.1</version>
        <executions>
          <execution>
            <phase>none</phase>
            <goals>
              <goal>jar</goal>
            </goals>
            <configuration>
              <archive>
                <compress>false</compress>
              </archive>
            </configuration>
          </execution>
          <execution>
            <id>attach-sources</id>
            <goals>
              <goal>jar-no-fork</goal>
            </goals>
            <configuration>
              <archive>
                <compress>false</compress>
              </archive>
            </configuration>
          </execution>
        </executions>
        <inherited>true</inherited>
        <configuration>
          <archive>
            <compress>false</compress>
          </archive>
        </configuration>
      </plugin>

This ensured that the goal jar never runs and only the goal jar-no-fork executes -- which is what I wanted to achieve.

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