57

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

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

在根 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:有一个相关的帖子在 maven 和 maven 插件中选择依赖版本没有回答我的问题

4

4 回答 4

61

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

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

在我的根 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 - pluginManagement 和父子关系的问题),我只声明插件依赖项:

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

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

于 2012-06-29T07:57:15.457 回答
6

对于父 POM 来控制子使用的插件版本,您应该在父 POM<plugin>的一个<pluginManagement>部分中声明。

您在部分中定义com.google.gwt:gwt-user为 a 。<dependency><dependencyManagement>

我不确定您是打算gwt-user用作插件还是依赖项,但它应该在两者中列为相同的实体,以便继承工作。

于 2012-06-28T23:41:18.640 回答
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> 

不是最漂亮的解决方案,但工作:-)

于 2014-02-11T10:24:53.217 回答
0

就我而言,我使用的是 jetty maven 插件,它依赖于 hsqldb。我从 sonatype 书中复制了一些示例行(我认为那是我从中获得这些行的地方)以使用 jetty 插件,该插件将 groupId 指定为 hsqldb。我正在使用 2.3.2 版的 hsqldb。在我的依赖管理部分的父 pom 和我的持久性模块中,groupId 是 org.hsqldb。groupIds 不匹配是导致我出错的原因,因为在那个旧的 groupId 下没有版本 2.3.2。一旦我将 groupId 从 hsqldb 更改为 org.hsqldb 一切都开始工作了。

于 2014-08-09T17:59:34.113 回答