【问题标题】:Maven `help:effective-pom` only generating for a single project, not all projectsMaven `help:effective-pom` 只为单个项目生成,而不是所有项目
【发布时间】:2017-11-12 22:36:59
【问题描述】:

我想为多模块构建中的所有子项目生成一个有效的 pom。

help:effective-pom 文档 here 状态

它将遍历当前构建会话中的所有项目,为每个项目打印有效的 POM

我在this maven project 的根目录中运行mvn help:effective-pom,它只为根目录生成,而不是为子项目生成。

为什么不是为所有项目生成有效的 pom。注意:我有另一个真实世界的 maven 项目,其中mvn help:effective-pom 为每个子模块正确生成带有嵌套<project><projects> 标记。我不确定为什么它不适用于this maven project

这里是生成的有效pom

<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Generated by Maven Help Plugin on 2017-11-12T09:56:26                  -->
<!-- See: http://maven.apache.org/plugins/maven-help-plugin/                -->
<!--                                                                        -->
<!-- ====================================================================== -->

<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Effective POM for project                                              -->
<!-- 'org.gradle.test.performance:project:pom:1.0'                          -->
<!--                                                                        -->
<!-- ====================================================================== -->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.gradle.test.performance</groupId>
  <artifactId>project</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  <modules>
    <module>project0</module>
    <module>project1</module>
    <module>project2</module>
    <module>project3</module>
    <module>project4</module>
    <module>project5</module>
    <module>project6</module>
    <module>project7</module>
    <module>project8</module>
    <module>project9</module>
  </modules>
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>C:\code\gradle-maven-transform\example\smallJavaMultiProject\src\main\java</sourceDirectory>
    <scriptSourceDirectory>C:\code\gradle-maven-transform\example\smallJavaMultiProject\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>C:\code\gradle-maven-transform\example\smallJavaMultiProject\src\test\java</testSourceDirectory>
    <outputDirectory>C:\code\gradle-maven-transform\example\smallJavaMultiProject\target\classes</outputDirectory>
    <testOutputDirectory>C:\code\gradle-maven-transform\example\smallJavaMultiProject\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>C:\code\gradle-maven-transform\example\smallJavaMultiProject\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>C:\code\gradle-maven-transform\example\smallJavaMultiProject\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>C:\code\gradle-maven-transform\example\smallJavaMultiProject\target</directory>
    <finalName>project-1.0</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.3.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>install</phase>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.3</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>site</phase>
            <goals>
              <goal>site</goal>
            </goals>
            <configuration>
              <outputDirectory>C:\code\gradle-maven-transform\example\smallJavaMultiProject\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <outputDirectory>C:\code\gradle-maven-transform\example\smallJavaMultiProject\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <outputDirectory>C:\code\gradle-maven-transform\example\smallJavaMultiProject\target\site</outputDirectory>
          <reportPlugins>
            <reportPlugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
            </reportPlugin>
          </reportPlugins>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <outputDirectory>C:\code\gradle-maven-transform\example\smallJavaMultiProject\target\site</outputDirectory>
  </reporting>
</project>

【问题讨论】:

    标签: java maven


    【解决方案1】:

    EffectivePomMojo 是一个聚合器目标。这意味着它只为您运行它的模块调用。但你是对的,它也应该转储所有子项目。在您的情况下它不这样做的原因是因为这一行:

    if ( projects.get( 0 ).equals( project ) && projects.size() > 1 )
    

    在您的情况下,最上面的项目在图中是最后一个(因为任何其他模块都没有依赖于它)。如果您将它作为父级添加到 project0(并且仅用于该模块),那么它会将您的根 pom 放在图中的第一个位置,一切都会按您的预期工作。

    我无法为这种行为提出理由。因此,如果您在 bugtracker 中提出问题,每个人都会受益 :) 如果/当您得到答案时,请更新我们。

    PS:如果你知道如何调试 Maven 插件,这些问题很容易解决。为此,您可以使用mvnDebug 而不是mvn,然后打开插件的源代码并附加到调试会话。

    【讨论】:

    • 感谢您的输入,但不幸的是,错误修复无济于事,因为它需要我的实用程序的用户升级我绝对不想要的 maven。我需要一些“正常工作”的东西。
    • 您实际上不需要升级 Maven。您可以使用完全限定的插件 GAV 而不是快捷方式,它将使用您指定的任何版本:org.apache.maven.plugins:maven-help-plugin:2.2:effective-pom
    • 有趣,我可以从命令行执行此操作吗?或者需要把这个放在pom.xml
    • 在命令行中。仅当您要将插件绑定到测试、编译、打包等阶段或要预配置它们时,才需要在 pom.xml 中包含插件。
    • @StanislavBashkyrtsev 对stackoverflow.com/questions/55530477/…有什么帮助吗?
    【解决方案2】:

    除了根项目中的modules 元素之外,您还需要在每个子模块pom.xml 中指定parent 元素,例如:

    <parent>
        <groupId>org.gradle.test.performance</groupId>
        <artifactId>project</artifactId>
        <version>1.0</version>
    </parent>
    

    否则运行help:effective-pom命令时不会被拾取。

    编辑:关于为什么插件不能与聚合一起使用的一些精确度。 聚合(使用modules 元素完成)不同于继承(使用parent 元素)。但是插件文档清楚地提到:

    effective-pom 目标用于使生成的 POM 可见 从插值、继承和活动的应用 个人资料。

    并且没有提到聚合插件。我相信它基本上意味着:

    我将采用您提供给我的 pom,应用插值、继承和活动配置文件,但不应用聚合,并为您提供生成的 pom。处理它。

    【讨论】:

    • 感谢您的信息,虽然这可能使它工作......在 Maven 中没有要求多模块构建中的子模块必须从父级继承。多模块项目在没有继承的情况下在 Maven 中构建得非常好。如果某些模块继承自parent1 而其他模块继承自project2,会发生什么?这也是 maven 中的有效用例?
    • 来自文档:The effective-pom goal is used to make visible the POM that results from the application of interpolation, inheritance and active profiles. 它提到了继承,但是对于您使用聚合的模块 - 这是不同的,我认为这种行为是帮助插件所期望的;)不,没有要求像你这样的聚合模块也继承自同一个项目。如果某些模块从不同的父模块继承但聚合在一起,这是一个完全有效的情况,尽管它可能不被视为最佳实践。
    • 我参与过许多“聚合器”不是父级的项目(父级有一个单独的项目)。所以猜测这种情况也行不通。我正在寻找一个通用的解决方案来为项目的“聚合”(即通过mvn package 构建的集合)获得有效的pom。不幸的是,mvn help:effective-pom 似乎无法做到这一点。我想我会按照here 的讨论来调查模型构建器。再次感谢
    【解决方案3】:

    我最终得到了不同的解决方案。我没有接受单个根 pom.xml 作为输入,而是需要解析 pom.xml 的集合。我现在还嵌入了maven-model-builder 并通过DefaultModelBuildingRequest 调用DefaultModelBuilder

    例如:

    DefaultModelBuilderFactory factory = new DefaultModelBuilderFactory();
    DefaultModelBuilder builder = factory.newInstance();
    ModelBuildingRequest req = new DefaultModelBuildingRequest();
    req.setProcessPlugins(false);
    req.setModelResolver(createModelResolver());
    req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
    for (File pomFile : pomFiles) {
       req.setPomFile(pomFile);
       Model effectivePom = builder.build(req).getEffectiveModel();
       ...
    }
    

    任何有兴趣的人的代码here

    【讨论】:

    • 这不能回答问题。它甚至与问题无关。
    • 嗯...我的最终目标是为 maven 构建中的每个 pom.xml 提供一个有效的 pom。因为我已经证明help:effective-pom 并不总是从根项目中执行此操作,所以我剩下的选择是为每个pom.xml 调用help:effective-pom。我找到了一种不需要安装 maven 的方法,通过 maven API 比从 shell 调用 mvn help:effective-pom 快得多
    • 如果您能提供有关createModelResolver() 方法的更多详细信息,我将不胜感激,我不明白如何实例化ModelResolver
    • 查看我的 github 仓库的 this search
    • 虽然我必须同意其他人的观点,即这个答案解决了与您在问题中所要求的不同的问题,但它对我面临的问题帮助很大!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 2023-03-08
    • 2020-10-07
    • 2017-06-02
    • 1970-01-01
    • 2014-05-26
    • 2015-11-07
    相关资源
    最近更新 更多