开发者

Creating Pluggable Project with Maven

开发者 https://www.devze.com 2023-02-02 12:20 出处:网络
I\'m currently working on a maven java web application project that I\'m required to extend to add 开发者_高级运维extra functionality but without changing the original code base. So the web applicatio

I'm currently working on a maven java web application project that I'm required to extend to add 开发者_高级运维extra functionality but without changing the original code base. So the web application should forward the new requests to another jar that will handle them. The problem is that we need the jar to be pluggable so that we can replace it or add more jar as more plugins to the project without having to change the original code base including not changing the maven pom.xml file for each new jar ( plugin ) so I cannot hard code the dependency on this jar in the pom file

So is there is a way in setting the maven profile while packaging my project to instruct maven to which plugins to pick up?


Wouldn't you want ability to plugin jars at runtime (not just at build-time)? I would recommend a system where you build your web app separately. Then you build plugin jars as separate projects. At runtime, defined a folder on the file system where you will look for plugin jars then construct UrlClassLoader instance to access classes in those jars.


Srry for answering so late , but we also had a similar requirements so what we did is we created four maven project

  1. One would your main project where you want the pluggable nature.

  2. Second would be the interface which would declare an set of interface class that the pluggable plugins would need to implement to get called in the main project.

  3. Third would an POM build project which will act as a parent project for all the custom plugins as module in pom.xml similar to the spring boot starter project dependency. Any new plugging that needs to be created will have this POM Project as parent project.This project is basically an helper project for CICD so that our custom plugins jar are build and pushed to the our custom repository
  4. This would be an reference project containing the all the custom plugins as dependencies in its pom.xml and this projects information will be in the main project so that when the main project build all the custom plugins becomes the classpath of main project

Your main parent plugin project will have the second interface plugin reference as dependency in its pom.xml so that the custom plugins gets to implement the interface.It will also have the reference of the fourth project so that all the custom plugins are in the classpath of our main project.

Now when you build the parent POM project all the custom plugins will get build as jar and store in ur .M2 folder.

Now to get access to these custom plugins in your main project you need to have the interface reference dependency in the pon.xml as dependencies and the, now the build would be

  1. To mvn install the interface project
  2. To mvn install the parent pom project
  3. To mvn install the fourth project
  4. To mvn install the main project

Once the fourth project is build you will have all the custom plugins in your classpath of your main project. Then you can use service loader class or netbeans lookup method to load all the class that implements the interface that was defined in the interface project.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号