插件依赖关系的Maven依赖关系管理

问题描述 投票:40回答:4

最近,我遇到了以下问题:

当我为我的项目设置依赖项管理时,我使用带有依赖项的插件进行了子操作,我希望与依赖项管理中声明的依赖项同步。

在根pom中,我在我的依赖管理中声明:

<dependencyManagement>
    <dependencies>
      ...
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-user</artifactId>
            <version>2.4.0</version>
        </dependency>
      ...
    <dependencies>
<dependencyManagement>

在孩子pom中,我有一个需要gwt-user的插件:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>gwt-maven-plugin</artifactId>
    <version>2.4.0</version>
    <dependencies>
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-user</artifactId>
            <version>2.4.0</version>
        </dependency>
            ...
        </dependencies>
  ...
</plugin>

但是,如果我删除gwt-maven-plugin中使用的依赖版本,则编译失败。

还有另一种方法可以实现吗?

PS:有一个相关的帖子Choosing dependency version in maven and maven plugin不回答我的问题

java maven
4个回答
49
投票

根据以下链接,似乎不可能:

这是我发现的一种解决方法,我希望与大家分享,以防其他人遇到同样的问题:

在我的根pom中,我定义了一个属性,一个依赖关系管理和一个插件管理:

<properties>
    <gwtVersion>2.4.0</gwtVersion>
    <gwtMavenPluginVersion>2.4.0</gwtMavenPluginVersion>
</properties>

<dependencyManagement>
   <dependencies>
    ...
    <dependency>
        <groupId>com.google.gwt</groupId>
        <artifactId>gwt-user</artifactId>
        <version>${gwtVersion}</version>
    </dependency>
    ...
   <dependencies>
<dependencyManagement>

<build>    
  <pluginManagement>
        <plugins>
            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>gwt-maven-plugin</artifactId>
            <version>${gwtMavenPluginVersion}</version>
            <dependencies>
                <dependency>
                    <groupId>com.google.gwt</groupId>
                    <artifactId>gwt-user</artifactId>
                    <version>${gwtVersion}</version>
                </dependency>
                ...
            </dependencies>
            ...
        </plugins>
  ...
  </pluginManagement>
</build>

在我的孩子pom中,使用插件管理提供的关系(请参阅Maven2 - problem with pluginManagement and parent-child relationship),我只是声明了插件依赖:

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>gwt-maven-plugin</artifactId>
</plugin>

现在,如果我更改属性中的版本,它会自动影响所有直接依赖项和插件依赖项。


4
投票

要让父POM控制子项使用的插件版本,您应该在父POM的<plugin>部分中声明<pluginManagement>

你在com.google.gwt:gwt-user部分将<dependency>定义为<dependencyManagement>

我不确定你是打算使用gwt-user作为插件还是作为依赖项,但它应该被列为同一个实体,以便继承工作。


2
投票

另一种可能性是导入父POM的所有依赖项:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>gwt-maven-plugin</artifactId>
    <version>2.4.0</version>
    <dependencies>
        <dependency>
             <groupId>${project.groupId}</groupId>
             <artifactId>${project.artifactId}</artifactId>
             <version>${project.version}</version>
        </dependency>
            ...
        </dependencies>
  ...
</plugin> 

不是最美丽的解决方案,但工作:-)


0
投票

在我的情况下,我使用jetty maven插件,依赖于hsqldb。我从sonatype书中复制了一些样本行(我认为这是我从中得到的行)来使用jetty插件,它将groupId指定为hsqldb。我使用的是hsqldb的2.3.2版本。在dependencyManagement部分和我的持久性模块中的父pom中,groupId是org.hsqldb。不匹配的groupIds导致我得到一个错误,因为在那个旧的groupId下没有版本2.3.2。一旦我将groupId从hsqldb更改为org.hsqldb,一切都开始工作了。

© www.soinside.com 2019 - 2024. All rights reserved.