last day (14 days later) » 

12:57 PM
0
Q: Custom Maven plugins and libraries

AbbyCurrently, I have a few custom maven plugins which use the same libraries (JARs) as my application which uses it. It is pertinent to mention that the libraries are also custom libraries developed by us and not 3rd party. I want to let go of these libraries and integrate all of them directly insi...

 
Do you want to have within the same multi-project: 1. library code 2. plugin code using library code 3. plugin execution?
 
Yes. This may sound strange, but maintaining all these artifacts separately just for one application is becoming a nightmare - rebuilding everything, maintaining the versions etc - all this just to maintain one single application which in itself is not too complex. I just want to have one project to maintain - that's the whole idea.
 
Clear. I have same issue in my project. My concept is wrap library code in java.util.function.Consumer and create a generic plugin which finds concrete Consumer by java.util.ServiceLoader and run it. But it will take me day or two to provide regular answer here.
 
Ok, but how do I access these libraries then at compile time and runtime from the application itself. As I said these libraries are shared by both the application directly and by the plugins (which are used at build time on this application)
 
Plugin, I will name it mapconsumer-maven-plugin, will be released separately. The plugin will depend only on JDK. The plugin will find correct Consumer on classpath by ServiceLoader and call method accept. All required parameters will be passed in Map object. You have to implement own Consumer which will accept a map parameter and do what you need
 
12:57 PM
The consumer in that case would have to be inside the application (as it would need the library source code to work). In that case what you are effectively saying is to put all the plugin logic in the application and just call a particular service through the plugin. To give you an example, one of the libraries helps the plugin and the application to connect to the database.
 
Yes, you are following my idea. However, phrase "...put all plugin logic in the application..." is not precise. The logic don't to be part of application (part of WAR for example). It may be part of build, for example Maven module, part of multi-project but not included in final application
 
I am a bit lost now. Can you please help me with an example - let's say I have a library which helps me to connect to a database - returns a connection object for example and also executes my queries (common plumbing logic part of the library). This library is used by my application as well by my plugin to connect to the database. Which part would I put in the consumer and which as part of the plugin and app
 
mapconsumer-maven-plugin has no special logic: it just find Consumer and call apply. Library for connecting to database is part of application. Plugin logic is a consumer with use the library to connect database and do something. Open question is where put Consumer code. It may be part of library -> part of application. But you may add separated maven module (let say my-consumer) to multi-project with consumer code. My-consumer will be part of build but not part of application.
 
1:26 PM
but the question still remains - how would the maven plugin find consumer at build / test time during maven execution. Also since I have multiple maven plugins like that - some of which are used with test scope and some with runtime scope etc and some are executed specifically for a purpose - eg. at delivery time, how can I handle all that with one generic maven plugin
 
mapconsumer-maven-plugin should be able to scan current project classpath + dependencies. It not trivial but there are examples on stack. If the plugin see classpath, then it could find consumer. To find consumer I propose use ServiceLoader: the project with consumer code should expose it through META-INF/services, mapconsumer-maven-plugin shoud have parameter: what-consumer-should-be-called (full class name) and then found concrete implementation by ServiceLoader
 
2:23 PM
What about the second problem -" Also since I have multiple maven plugins like that - some of which are used with test scope and some with runtime scope etc and some are executed specifically for a purpose - eg. at delivery time, how can I handle all that with one generic maven plugin"
 
2:37 PM
mapconsumer is very similar to maven-antrun-plugin. If you want fire mapconsumer many times, you have to define many executions. For example one execution on phase test with consumer class "com.abc.MyTestConsumer" and second execution on phase deploy with consumer class "com.abc.MyDatabaseUpdateConsume"
Unfortunately Maven has some difficulties with plugin execution order for complex, multi-profile build.
 
 
1 hour later…
3:46 PM
In that case, we will lose all the services provided by maven plugin execution - exception handling etc. The plugin essentially is just a simple service which would call another Java service.
 
4:20 PM
You are right, Consumer has no access to Maven "ecosystem"
 
 
2 hours later…
6:18 PM
However, if Consumer needs access to Maven API, generic plugin can pass it throug map parameter, for example
<execution>
<goal>apply</goal>
<configuration>
<map>
<project>${project}</[project>...
 

last day (14 days later) »