63

Recently, I came accross the following problem :

As I set up dependency management for my project, I had child-pom using plugin with dependencies, that I want to be synchronized with dependencies declared in my dependency management.

In a root pom, I declared in my dependency management:

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

And in the child pom, I have a plugin which needs 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>

However, if I remove the dependency version used in gwt-maven-plugin, the compilation fails.

Is there another way to achieve it ?

PS: There is a related post Choosing dependency version in maven and maven plugin which does not answer my question

Community
  • 1
  • 1
JBE
  • 11,917
  • 7
  • 49
  • 51

4 Answers4

66

According to the following links, it seems not to be possible:

Here is a workaround I found, and I wanted to share with everyone, in case other people had the same problem:

In my root pom, I have defined a property, a dependency management and a plugin management:

<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>

And in my child pom, using the relationship provided by plugin management (see Maven2 - problem with pluginManagement and parent-child relationship), I just declare the plugin dependency:

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

Now if I change the version in the properties, it is automatically impacting all direct dependencies and plugin dependencies.

ToFi
  • 1,167
  • 17
  • 29
JBE
  • 11,917
  • 7
  • 49
  • 51
  • 2
    IMHO if the dependency is only needed in the plugin, there's no use for the dependencyManagement section – Franz Ebner Jun 24 '16 at 08:08
  • 3
    for futur reference. pluginManagement is under build tag – TecHunter Apr 04 '17 at 15:35
  • You're using the exact configuration from the managed plugin, but what if I need to specify a file filter or a package for the plugin to work on? What happens when I only specify the configuration for the plugin in my child pom? – Timo Aug 23 '18 at 09:47
8

For a parent POM to control which plugin versions the child uses, you should declare the <plugin> in a <pluginManagement> section of the parent POM.

You defined com.google.gwt:gwt-user as a <dependency> in the <dependencyManagement> section.

I'm not sure if you are intending to use gwt-user as a plugin or as a dependency, but it should be listed as the same entity in both for inheritance to work.

matt b
  • 138,234
  • 66
  • 282
  • 345
  • gwt-user is used as a plugin that has dependencies. OP is trying to get the version defined for both in only one location. – eis May 02 '13 at 13:19
  • btw, you can't inherit plugins from a parent that is defined in depedencyManagement. – Serkan Feb 17 '23 at 09:30
1

The other possibility is to import all the dependencies of the parent 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> 

Not the most beautiful solution, but working :-)

kavai77
  • 6,282
  • 7
  • 33
  • 48
0

In my case I was using the jetty maven plugin, with a dependency on hsqldb. I copied some sample lines from the sonatype book (I think that's where I got the lines from) for using the jetty plugin, which specified the groupId as hsqldb. I am using version 2.3.2 of hsqldb. In my parent pom in the dependencyManagement section and my persistence module the groupId is org.hsqldb. Having mismatched groupIds was what was causing me to get an error, because under that old groupId there isn't a version 2.3.2. Once I changed the groupId from hsqldb to org.hsqldb everything started working.

lumpynose
  • 967
  • 4
  • 12