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

时间:2021-01-15 22:45:53

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.

我有一个maven多模块项目(男孩,我在这个网站上写了太多次开放)。几乎所有模块(即其中包含代码的模块)都应运行maven-site-plugin来生成有关代码覆盖率等的报告。这些模块具有详细的共享配置 - 报告运行,要覆盖哪些文件/排除某些插件等

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.

但是,有一些模块可以处理打包 - 运行程序集插件以生成tarball等。这些都不会从运行站点报告中获得任何好处 - 没有可以分析的代码,也没有可以报告的测试。

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.

所以我有很多需要共享插件配置的模块,以及一些不需要运行插件的模块,最好是根本不需要。如果我把插件放在父POM的 部分,我可以做前者(共享配置),但在这种情况下,我似乎无法关闭插件。如果我将配置下推到每个模块自己的POM,我可以做后者(避免运行插件),但在这种情况下我无法想出一个分享配置信息的好方法。

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

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

2 个解决方案

#1


63  

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:

通过“运行插件”,我假设您的意思是插件已经绑定到生命周期阶段,并且您希望在某些模块中取消绑定它。首先,您可以考虑更改POM继承,以便不需要插件的模块具有一个父节点和具有不同父节点的模块。如果您不想这样做,那么您可以在子模块中将执行阶段明确设置为“nothing”。例如。如果您有这样的父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>

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.

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

#2


1  

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.

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

  1. First set the phase of the execution to none
  2. 首先将执行阶段设置为none
  3. Create another execution, give it an id and do in it what you need it to do.
  4. 创建另一个执行,给它一个id并在其中执行你需要它做的事情。
  5. run mvn help:effective-pom to confirm that it has correctly suppressed what you needed suppressed from the parent pom.
  6. 运行mvn help:effective-pom确认它已经正确地压缩了你需要从父pom中抑制的内容。

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

这是一个例子:这是我父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>

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:

我需要将目标改为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>

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

结果这就是有效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>

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

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

#1


63  

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:

通过“运行插件”,我假设您的意思是插件已经绑定到生命周期阶段,并且您希望在某些模块中取消绑定它。首先,您可以考虑更改POM继承,以便不需要插件的模块具有一个父节点和具有不同父节点的模块。如果您不想这样做,那么您可以在子模块中将执行阶段明确设置为“nothing”。例如。如果您有这样的父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>

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.

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

#2


1  

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.

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

  1. First set the phase of the execution to none
  2. 首先将执行阶段设置为none
  3. Create another execution, give it an id and do in it what you need it to do.
  4. 创建另一个执行,给它一个id并在其中执行你需要它做的事情。
  5. run mvn help:effective-pom to confirm that it has correctly suppressed what you needed suppressed from the parent pom.
  6. 运行mvn help:effective-pom确认它已经正确地压缩了你需要从父pom中抑制的内容。

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

这是一个例子:这是我父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>

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:

我需要将目标改为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>

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

结果这就是有效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>

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

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