You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by Jochen Wiedmann <jo...@gmail.com> on 2012/08/02 10:38:46 UTC

Pluggable dependency version

Hi,

a plugins dependencies are usually hard coded in the plugins POM. I
know, that the user can change the dependency version by configuring
the plugins dependency list appropriately. However, I feel that that
is somewhat inconvenient and would prefer to plugin a little bit more
dynamic, for example by reading the dependency version from the
projects dependency list. Can anayone point out to me, how to do that?

Thanks,

Jochen

-- 
In other words: what could be seen as a socially debilitating failure
of character can certainly work to your advantage too. (Linus
Torvalds, but the use in the signature tells something about me as
well.)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: Pluggable dependency version

Posted by Anders Hammar <an...@hammar.net>.
But you were not talking about an adapter solution, were you?

/Anders (mobile)
Den 6 aug 2012 16.28 skrev "Jesse Glick" <jg...@cloudbees.com>:

> On 08/03/2012 09:15 AM, Anders Hammar wrote:
>
>> What's the benefit of this approach rather than just having the user
>> adding the dependencies directly to the plugin (execution) declaration?
>>
>
> Linkage safety. If you have the-plugin built directly against
> the-build-tool@1.1.2, and the user configures it to run against
> the-build-tool@1.0-beta-3 or @1.6.3 etc., it
> is hard to say in advance what will happen - it might work fine, or it
> might consistently throw errors due to signature changes in the-build-tool,
> or it might work in
> most cases but throw errors in unusual circumstances when certain code
> paths are executed. Using an adapter allows the-plugin author to explicitly
> manage different
> versions of the-build-tool. If the-build-tool has a very simple and stable
> interface it would be overkill.
>
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.**org<de...@maven.apache.org>
> For additional commands, e-mail: dev-help@maven.apache.org
>
>

Re: Pluggable dependency version

Posted by Jesse Glick <jg...@cloudbees.com>.
On 08/03/2012 09:15 AM, Anders Hammar wrote:
> What's the benefit of this approach rather than just having the user adding the dependencies directly to the plugin (execution) declaration?

Linkage safety. If you have the-plugin built directly against the-build-tool@1.1.2, and the user configures it to run against the-build-tool@1.0-beta-3 or @1.6.3 etc., it
is hard to say in advance what will happen - it might work fine, or it might consistently throw errors due to signature changes in the-build-tool, or it might work in
most cases but throw errors in unusual circumstances when certain code paths are executed. Using an adapter allows the-plugin author to explicitly manage different
versions of the-build-tool. If the-build-tool has a very simple and stable interface it would be overkill.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: Pluggable dependency version

Posted by Anders Hammar <an...@hammar.net>.
What's the benefit of this approach rather than just having the user
adding the dependencies directly to the plugin (execution)
declaration? To me it seems as it would be the same number of xml rows
in the pom and it would make the plugin coding a lot easier.

/Anders

On Fri, Aug 3, 2012 at 2:56 PM, Jesse Glick <jg...@cloudbees.com> wrote:
> On 08/02/2012 04:58 AM, Anders Hammar wrote:
>>
>> not have a dependency on some library in the plugin itself but rely on it
>> being declared as a dependency in the Maven project where the plugin is
>> bound. That made it
>> totally controllable by the project.
>
>
> If the build-time code would not normally already be a dependency of the
> project - your main concern - then you could have the mojo configuration
> accept a parameter typed as a list of "dependencies".
> (org.apache.maven.model.Dependency[]?) These would then have to be resolved
> (transitively) against pluginRepositories, and used to construct a URL[] to
> pass to a URLClassLoader (possibly prepended to the project's regular
> dependencies). Thus the project could declare e.g.
>
> <dependencies>
>   <dependency>
>     <groupId>some.framework</>
>     <artifactId>the-api</>
>   </>
>   <dependency>
>     <groupId>some.framework</>
>     <artifactId>the-impl</>
>     <scope>runtime</>
>   </>
> </>
> <build>
>   <plugins>
>     <plugin>
>       <groupId>some.framework</>
>       <artifactId>the-plugin</>
>       <configuration>
>         <toolDependencies>
>           <dependency>
>             <groupId>some.framework</>
>             <artifactId>the-build-tool</>
>           </>
>         </>
>       </>
>     </>
>   </>
> </>
>
> Naturally you still need to be careful that a given version of the-plugin -
> specifically, that part of its code which will link against classes loaded
> by the URLClassLoader - is API-compatible with past, present, and
> foreseeable future versions of the-build-tool. If there are known breaking
> API changes, a single version of the plugin could also supply multiple
> adapters tuned to different version ranges of the-build-tool, or use
> reflection to try several method variants, or at least catch LinkageError
> and report the problem gracefully.
>
> A mojo like this could be awkward to write from scratch; would be best to
> have an archetype for it. In particular, if you are using direct Java
> linkage between the-plugin code and the-build-tool (rather than, say,
> reflection), and the-plugin does not have a direct dependency on any version
> of the-build-tool (i.e. <toolDependencies> is mandatory and not just an
> optional selection of an alternative version) and/or supports multiple
> incompatible version ranges of the-build-tool, then to build the-plugin
> itself you need a multimodule project:
>
> the-plugin-parent
>   the-build-tool-abstraction (jar)
>   the-build-tool-adapter-v1 (jar) -> the-build-tool-abstraction,
> the-build-tool @ [1.0.0,2)
>   the-build-tool-adapter-v2 (jar) -> the-build-tool-abstraction,
> the-build-tool @ [2.0.0,3)
>   the-plugin (maven-plugin) -> the-build-tool-abstraction
>
> Here the-plugin resolves <toolDependencies> and determines which major
> version of the-build-tool is being requested ("1" or "2"). It then
> constructs a URLClassLoader parented to the plugin ClassLoader (or whatever
> loads classes from the-build-tool-abstraction) including everything from
> <toolDependencies> (resolved from pluginRepositories) plus the appropriate
> the-build-tool-adapter-v* (also from pluginRepositories), creates an
> instance of an interface defined in the-build-tool-abstraction and
> implemented in the-build-tool-adapter-v* (selected via
> java.util.ServiceLoader, Plexus @Component,
> ClassLoader.loadClass("hardcoded"), etc.), and casts to
> the-build-tool-abstraction interface which the plugin can directly "see".
> Then it calls methods on this instance to run the build tool.
>
> Even if you are sure the-build-tool will be API-compatible it is not wise to
> mash everything into one project. While the-plugin could declare a
> <scope>provided</> dependency on the-build-tool, as the developer of
> the-plugin you would need to be careful to keep track of which
> src/main/java/**/*.java are permitted to refer to types in the-build-tool
> and which are not; furthermore, the URLClassLoader would then need to
> override loadClass to _not_ delegate loading of these classes to its parent
> (since then they would be loaded by the plugin class loader and fail to
> resolve against the-build-tool). Using a multimodule project as sketched
> above documents and enforces the runtime structure and means that the
> URLClassLoader can use normal parent delegation.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: Pluggable dependency version

Posted by Jesse Glick <jg...@cloudbees.com>.
On 08/02/2012 04:58 AM, Anders Hammar wrote:
> not have a dependency on some library in the plugin itself but rely on it being declared as a dependency in the Maven project where the plugin is bound. That made it
> totally controllable by the project.

If the build-time code would not normally already be a dependency of the project - your main concern - then you could have the mojo configuration accept a parameter typed 
as a list of "dependencies". (org.apache.maven.model.Dependency[]?) These would then have to be resolved (transitively) against pluginRepositories, and used to construct 
a URL[] to pass to a URLClassLoader (possibly prepended to the project's regular dependencies). Thus the project could declare e.g.

<dependencies>
   <dependency>
     <groupId>some.framework</>
     <artifactId>the-api</>
   </>
   <dependency>
     <groupId>some.framework</>
     <artifactId>the-impl</>
     <scope>runtime</>
   </>
</>
<build>
   <plugins>
     <plugin>
       <groupId>some.framework</>
       <artifactId>the-plugin</>
       <configuration>
         <toolDependencies>
           <dependency>
             <groupId>some.framework</>
             <artifactId>the-build-tool</>
           </>
         </>
       </>
     </>
   </>
</>

Naturally you still need to be careful that a given version of the-plugin - specifically, that part of its code which will link against classes loaded by the 
URLClassLoader - is API-compatible with past, present, and foreseeable future versions of the-build-tool. If there are known breaking API changes, a single version of the 
plugin could also supply multiple adapters tuned to different version ranges of the-build-tool, or use reflection to try several method variants, or at least catch 
LinkageError and report the problem gracefully.

A mojo like this could be awkward to write from scratch; would be best to have an archetype for it. In particular, if you are using direct Java linkage between the-plugin 
code and the-build-tool (rather than, say, reflection), and the-plugin does not have a direct dependency on any version of the-build-tool (i.e. <toolDependencies> is 
mandatory and not just an optional selection of an alternative version) and/or supports multiple incompatible version ranges of the-build-tool, then to build the-plugin 
itself you need a multimodule project:

the-plugin-parent
   the-build-tool-abstraction (jar)
   the-build-tool-adapter-v1 (jar) -> the-build-tool-abstraction, the-build-tool @ [1.0.0,2)
   the-build-tool-adapter-v2 (jar) -> the-build-tool-abstraction, the-build-tool @ [2.0.0,3)
   the-plugin (maven-plugin) -> the-build-tool-abstraction

Here the-plugin resolves <toolDependencies> and determines which major version of the-build-tool is being requested ("1" or "2"). It then constructs a URLClassLoader 
parented to the plugin ClassLoader (or whatever loads classes from the-build-tool-abstraction) including everything from <toolDependencies> (resolved from 
pluginRepositories) plus the appropriate the-build-tool-adapter-v* (also from pluginRepositories), creates an instance of an interface defined in 
the-build-tool-abstraction and implemented in the-build-tool-adapter-v* (selected via java.util.ServiceLoader, Plexus @Component, ClassLoader.loadClass("hardcoded"), 
etc.), and casts to the-build-tool-abstraction interface which the plugin can directly "see". Then it calls methods on this instance to run the build tool.

Even if you are sure the-build-tool will be API-compatible it is not wise to mash everything into one project. While the-plugin could declare a <scope>provided</> 
dependency on the-build-tool, as the developer of the-plugin you would need to be careful to keep track of which src/main/java/**/*.java are permitted to refer to types 
in the-build-tool and which are not; furthermore, the URLClassLoader would then need to override loadClass to _not_ delegate loading of these classes to its parent (since 
then they would be loaded by the plugin class loader and fail to resolve against the-build-tool). Using a multimodule project as sketched above documents and enforces the 
runtime structure and means that the URLClassLoader can use normal parent delegation.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: Pluggable dependency version

Posted by Anders Hammar <an...@hammar.net>.
You want me to help you go down the wrong path? :-)

Don't have the code here (customer closed source), but I believe I saw
it first in some other plugin. Have a look in JBoss's ws maven plugin:
http://anonsvn.jboss.org/repos/jbossws/projects/plugins/maven/jaxws-tools/trunk/src/main/java/org/jboss/ws/plugins/tools/AbstractToolsMojo.java
Look at how the classloader is created. I *think* this is what I did
(or something similar).

I'd like to stress the problem with forcing a Maven project to add
dependencies it shouldn't have. It's just plain wrong.

/Anders

On Thu, Aug 2, 2012 at 11:20 AM, Jochen Wiedmann
<jo...@gmail.com> wrote:
> On Thu, Aug 2, 2012 at 10:58 AM, Anders Hammar <an...@hammar.net> wrote:
>> What I have done in a customer specific plugin, is to not have a
>> dependency on some library in the plugin itself but rely on it being
>> declared as a dependency in the Maven project where the plugin is
>> bound. That made it totally controllable by the project.
>
> Sounds exactly like what I intend. Could you share some lines of code, please?
>
>
>
>
> --
> In other words: what could be seen as a socially debilitating failure
> of character can certainly work to your advantage too. (Linus
> Torvalds, but the use in the signature tells something about me as
> well.)
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: Pluggable dependency version

Posted by Jochen Wiedmann <jo...@gmail.com>.
On Thu, Aug 2, 2012 at 10:58 AM, Anders Hammar <an...@hammar.net> wrote:
> What I have done in a customer specific plugin, is to not have a
> dependency on some library in the plugin itself but rely on it being
> declared as a dependency in the Maven project where the plugin is
> bound. That made it totally controllable by the project.

Sounds exactly like what I intend. Could you share some lines of code, please?




-- 
In other words: what could be seen as a socially debilitating failure
of character can certainly work to your advantage too. (Linus
Torvalds, but the use in the signature tells something about me as
well.)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: Pluggable dependency version

Posted by Anders Hammar <an...@hammar.net>.
What I have done in a customer specific plugin, is to not have a
dependency on some library in the plugin itself but rely on it being
declared as a dependency in the Maven project where the plugin is
bound. That made it totally controllable by the project. I don't
remember the exact could, and my brain's still on summer vacation, but
I believe I created a classpath with the project's dependencies in the
plugin.

However, there are several drawbacks of that design:
* All versions of the library that you could possibly use have to be
API compatible (in theory good libraries are, but in reality...)
* You could force the project to declare a dependency for something
just used during build time (the project could have a dependency to
the API but you need the impl as well during build time for the
plugin) - as a Maven person I very much worry about this!

I wouldn't really recommend this solution, but rather think that the
best solution is to declare the dependencies where they should go. So
if a plugin has a dependency, it should be declared there.

/Anders

On Thu, Aug 2, 2012 at 10:38 AM, Jochen Wiedmann
<jo...@gmail.com> wrote:
> Hi,
>
> a plugins dependencies are usually hard coded in the plugins POM. I
> know, that the user can change the dependency version by configuring
> the plugins dependency list appropriately. However, I feel that that
> is somewhat inconvenient and would prefer to plugin a little bit more
> dynamic, for example by reading the dependency version from the
> projects dependency list. Can anayone point out to me, how to do that?
>
> Thanks,
>
> Jochen
>
> --
> In other words: what could be seen as a socially debilitating failure
> of character can certainly work to your advantage too. (Linus
> Torvalds, but the use in the signature tells something about me as
> well.)
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org