Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven dependency management for plugin dependencies

Tags:

java

maven

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

like image 843
JBE Avatar asked Jun 28 '12 23:06

JBE


People also ask

What is Maven plugin Management?

From Maven documentation: pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one.

What is dependency management plugin?

Based on the configured dependency management metadata, the Dependency Management Plugin will control the versions of your project's direct and transitive dependencies and will honour any exclusions declared in the poms of your project's dependencies.

What is Maven dependency plugin used for?

The dependency plugin provides the capability to manipulate artifacts. It can copy and/or unpack artifacts from local or remote repositories to a specified location.


2 Answers

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

  • http://maven.40175.n5.nabble.com/dependency-management-within-plugin-dependencies-td78367.html
  • https://issues.apache.org/jira/browse/MNG-2496

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.

like image 58
JBE Avatar answered Sep 20 '22 05:09

JBE


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.

like image 27
matt b Avatar answered Sep 21 '22 05:09

matt b