Maven2-pluginManagement和父子关系的问题 [英] Maven2 - problem with pluginManagement and parent-child relationship

查看:107
本文介绍了Maven2-pluginManagement和父子关系的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自maven 文档

pluginManagement:是在侧面插件中可以看到的元素.插件管理包含插件元素的方式几乎相同,除了它不是为该特定项目构建配置插件信息,而是要配置从该项目继承的项目构建.但是,这仅配置在子级的plugins元素内实际引用的插件.子级有权覆盖pluginManagement定义.

pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children. The children have every right to override pluginManagement definitions.

现在:如果我的父母POM中有这个

Now : if I have this in my parent POM

  <build>
   <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.0</version>
          <executions>
             Some stuff for the children
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

我在父项目上运行了mvn help:effective-pom,得到了我想要的东西,即直接在构建中的插件部分(执行工作的插件部分)仍然是空的.

and I run mvn help:effective-pom on the parent project I get what I want, namely the plugins part directly under build (the one doing the work) remains empty.

现在,如果我执行以下操作:

Now if I do the following :

  <build>
   <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.0</version>
          <executions>
             Some stuff for the children
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
<plugins>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.0.2</version>
    <inherited>true</inherited>
    <configuration>
      <source>1.6</source>
      <target>1.6</target>
    </configuration>
  </plugin>
</plugins>
  </build>

mvn help:effective-pom我再次得到了我想要的东西,插件只包含了声明的内容,而pluginManagement部分被忽略了.

mvn help:effective-pom I get again just what I want, the plugins contains just what is declared and the pluginManagement section is ignored.

但随着以下内容而改变

  <build>
   <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.0</version>
          <executions>
             Some stuff for the children
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
          <version>2.0</version>
          <inherited>false</inherited> <!-- this perticular config is NOT for kids... for parent only -->
            <executions>
             some stuff for adults only
            </execution>
          </executions>
       </plugin>
    </plugins>
  </build>

并运行mvn help:effective-pom pluginManagement部分中的内容将添加到已声明的内容之上.这样:

and running mvn help:effective-pom the stuff from pluginManagement section is added on top of what is declared already. as such :

  <build>
   <pluginManagement>
     ...
   </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
          <version>2.0</version>
          <inherited>false</inherited> <!-- this perticular config is NOT for kids... for parent only -->
              <executions>
                 Some stuff for the children
                </execution>
            <executions>
             some stuff for adults only
            </execution>
          </executions>
       </plugin>
    </plugins>
  </build>

是否可以从父pom的部分中排除孩子的零件?实际上,我想要的是pluginManagement完全按照文档说明的方式运行,也就是说,我希望它仅适用于子项,而不适用于声明其的项目.

Is there a way to exclude the part for children from the parent pom's section ? In effect what I want is for the pluginManagement to behave exactly as the documentation states, that is I want it to apply for children only but not for the project in which it is declared.

我从来没有找到一个可以接受的解决方案,因此这个问题仍然悬而未决.下面提供了最近的解决方案,并且直到出现更好的解决方案为止,该解决方案是当前可以接受的解决方案.现在,有三种方法可以达到期望的结果(根据当前POM在继承层次结构中的位置来调整插件的行为):

I never did find an acceptable solution for this and as such the issue remains open. Closest solution was offered below and is currently the accepted solution for this question until something better comes up. Right now there are three ways to achieve the desired result (modulate plugin behaviour depending on where in the inheritance hierarchy the current POM is):

1-使用配置文件,它可以工作,但是您必须注意配置文件不会被继承,这有点违反直觉.将它们(如果已激活)应用于声明的POM,然后将该生成的POM向下传播.因此,激活子POM的配置文件的唯一方法是专门在命令行上(至少我没有找到其他方法).属性,文件和其他激活方式无法激活POM,因为触发器不在声明配置文件的POM中.

1 - using profiles, it will work but you must beware that profiles are not inherited, which is somewhat counter intuitive. They are (if activated) applied to the POM where declared and then this generated POM is propagated down. As such the only way to activate the profile for child POM is specifically on the command line (least I did not find another way). Property, file and other means of activation fail to activate the POM because the trigger is not in the POM where the profile is declared.

2-(这是我最后要做的事情)声明该插件未在父级中继承,并在需要它的每个孩子中重新声明(复制粘贴)该花絮.不是很理想,但是它很简单并且可以正常工作.

2 - (this is what I ended up doing) Declare the plugin as not inherited in the parent and re-declare (copy-paste) the tidbit in every child where it is wanted. Not ideal but it is simple and it works.

3-拆分父POM的聚合性质和父性质.然后,由于仅适用于父项的部分位于另一个项目中,因此现在可以按照最初的意图使用pluginManagement.但是,这意味着必须创建一个新的人工项目,该项目不会对最终产品有所贡献,而只会为罐头系统提供服务.这是概念性出血的明显案例.同样,这仅适用于我的具体情况,很难一概而论,因此我放弃了尝试使此工作变得更努力的方法,而转而支持2中描述的不太漂亮但包含更多内容的剪切和粘贴补丁.

3 - Split the aggregation nature and parent nature of the parent POM. Then since the part that only applies to the parent is in a different project it is now possible to use pluginManagement as firstly intended. However this means that a new artificial project must be created that does not contribute to the end product but only serves the could system. This is clear case of conceptual bleed. Also this only applies to my specific and is hard to generalize, so I abandoned efforts to try and make this work in favor of the not-pretty but more contained cut and paste patch described in 2.

如果由于我缺乏对Maven的了解或由于工具的发展而允许遇到此问题的人有更好的解决方案,请在此处发布解决方案以供将来参考.

If anyone coming across this question has a better solution either because of my lack of knowledge of Maven or because the tool evolved to allow this please post the solution here for future reference.

谢谢大家的帮助:-)

推荐答案

将插件配置添加到pluginManagement意味着,如果声明了插件,则将使用此配置,但是您仍然需要在任何插件的build部分中声明该插件想要使用它的POM.

Adding the plugin configuration to pluginManagement means that this configuration will be used if the plugin is declared, but you still need to declare the plugin in the build section of any POM that wants to use it.

在您引用的部分中对此进行解释的关键部分是:

The key part that explains this from the section you quoted is:

但是,这只会配置在子元素的plugins元素中实际引用的插件

However, this only configures plugins that are actually referenced within the plugins element in the children

因此,如果您在子pom中执行此操作,则将应用来自父项的配置:

So if you do this in the child pom the configuration from the parent will be applied:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
    </plugin>
  </plugins>
</build>

更新:要回答实际问题,pluginManagement部分中的内容始终与任何插件声明合并.为了避免父级执行此操作,可以在概要文件中定义pluginManagement部分,然后在子级项目而非父级项上激活该概要文件.然后,子项目必须声明该配置文件.

Update: To answer the actual question, the content from the pluginManagement section is always merged with any plugin declaration. To avoid the parent doing this, you can define the pluginManagement section within a profile, and activate that profile on child projects but not the parent. The child projects would then have to declare that profile.

例如:

<profiles>
  <profile>
    <id>for-children</id>
    <build>
      <pluginManagement>
        <plugins>
          <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.0</version>
            <executions>
              <!--Some stuff for the children-->
            </executions>
          </plugin>
        </plugins>
      </pluginManagement>
    </build>  
  </profile>
</profiles>
<build>
  <plugins>
    <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
        <version>2.0</version>
        <inherited>false</inherited> <!-- this perticular config is NOT for kids... for parent only -->
          <!--some stuff for adults only-->
        </executions>
     </plugin>
  </plugins>
</build>

这篇关于Maven2-pluginManagement和父子关系的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆