【问题标题】:Dependency management does not work for multi-module project依赖管理不适用于多模块项目
【发布时间】:2014-01-23 10:41:19
【问题描述】:

我有一个包含多个模块的 Maven 项目,我正在尝试对其进行设置,以便将模块依赖项自动构建到正确的生命周期阶段,以便将依赖模块构建到请求的生命周期阶段。

在示例中,模块plugin构建了一个Maven插件,用于生成源代码并被模块main使用。如果我只是尝试使用mvn -am -pl main compile,则编译模块plugin,但不会执行process-classes 生命周期阶段(这是插件可用所必需的)。编译模块 main 然后失败,然后出现以下错误:

[ERROR] Failed to parse plugin descriptor for example:plugin:1.0.0-SNAPSHOT (/Users/ims/Dropbox/IMS/Projects/PARITy_R4/codegen-test-simple/plugin/target/classes): No plugin descriptor found at META-INF/maven/plugin.xml -> [Help 1]

Maven 或它的插件是否能够解决多模块项目中模块的依赖关系并将它们构建到其他模块所需的阶段?如果是这样,我需要如何设置项目才能使其正常工作?

这些是我项目的 POM:

pom.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    <groupId>example</groupId>
    <artifactId>project</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>plugin</module>
        <module>main</module>
    </modules>
</project>

插件/pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    <groupId>example</groupId>
    <artifactId>plugin</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>maven-plugin</packaging>
    <parent>
        <groupId>example</groupId>
        <artifactId>project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <goalPrefix>configurator</goalPrefix>
                </configuration>
                <executions>
                    <execution>
                        <id>default-descriptor</id>
                        <goals>
                            <goal>descriptor</goal>
                        </goals>
                        <phase>process-classes</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

ma​​in/pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    <groupId>example</groupId>
    <artifactId>main</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <parent>
        <groupId>example</groupId>
        <artifactId>project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <build>
        <plugins>
            <plugin>
                <groupId>example</groupId>
                <artifactId>plugin</artifactId>
                <version>1.0.0-SNAPSHOT</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>codegen</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

  • 你为什么使用maven-reactor-plugin?你不需要它。我假设您使用的是 Maven 3?另一个问题是:为什么要创建一个插件并在同一个反应器中使用它?为什么不从插件部分制作一个单独的项目? BTW:你能更新一下标题吗,因为与dependencyManagement没有关系。
  • 我删除了对maven-reactor-plugin 的依赖。这是我尝试一些使用反应器插件的解决方案时的遗留问题。
  • @khmarbaise 我在同一个项目中使用它的原因是我希望能够在对其进行更改的同时运行该插件。如果我在另一个项目中使用它,那么每当我更改它时,我都必须mvn install 它,这对我来说似乎很容易出错。我已经习惯了 make 项目中的这些类型的设置,其中可以通过在生成代码生成器二进制文件的目标上运行代码生成器的目标的依赖项来实现。
  • 听起来像是在测试你的插件。为此,可以使用 maven-invoker-plugin 等工具为您的插件进行集成测试。

标签: maven


【解决方案1】:

如果您查看reference documentation for the Maven lifecycle,您会发现compileprocess-classes 之前。

如果您希望执行此步骤,则需要改用mvn -am -pl main process-classes

但我建议您始终使用mvn ... install - 它还会运行测试并确保main 使用的插件实际上是您认为应该使用的插件:如果不安装,构建将使用旧的/过时的来自本地存储库的版本(Maven 将不会神奇地确定“哦,我的反应器中有一个插件,我将使用它而不是本地存储库中的版本”)。

【讨论】:

  • 我从未安装过有问题的插件,并且构建仍然适用于mvn -am -pl main process-classes。所以在我看来,Maven 将使用在同一个多模块项目中定义的插件。
  • 奇数。如果您使用-X 运行mvn,您应该能够看到带有绝对路径的类路径。你能检查一下它使用的是你期望的路径吗?
  • 你在说哪个类路径?运行mvn -X process-classes 时,我只能在输出中找到用于编译每个模块源的类路径。对于模块main,就是这样:[DEBUG] Classpath: [.../codegen-test-simple/main/target/classes]
  • -X 向您展示了如何配置不同的插件。我已经看到这会转储 Maven 本身用来启动它们的类路径。但是我现在也无法创建示例,所以也许我错了:-/
  • Maven 的设计理念是大多数项目都是相似的,少数人应该理解并解决常见问题。其余的应该只使用预定义的服务。结果可能看起来/感觉“错误”,但大多数时候,这是因为您没有意识到一些重要的事实。
猜你喜欢
  • 2011-10-14
  • 2021-08-04
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 2013-06-22
  • 2010-11-15
  • 2017-05-13
  • 2016-01-27
相关资源
最近更新 更多