使用 Maven 创建可插入项目
我目前正在开发一个 Maven Java Web 应用程序项目,我需要扩展该项目以添加额外的功能,但不更改原始代码库。因此,Web 应用程序应该将新请求转发到另一个处理它们的 jar。问题是我们需要 jar 是可插拔的,以便我们可以替换它或添加更多 jar 作为更多插件到项目中,而不必更改原始代码库,包括不更改每个新 jar 的 maven pom.xml 文件(插件)所以我无法在pom文件中对这个jar的依赖进行硬编码
那么有没有一种方法可以在打包我的项目时设置maven配置文件来指示maven选择哪些插件?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不想在运行时(而不仅仅是在构建时)插入 jar 吗?我会推荐一个单独构建网络应用程序的系统。然后将插件 jar 构建为单独的项目。在运行时,在文件系统上定义一个文件夹,您将在其中查找插件 jar,然后构造 UrlClassLoader 实例来访问这些 jar 中的类。
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.
很抱歉这么晚才回答,但我们也有类似的要求,所以我们所做的是创建了四个 Maven 项目
一个是您想要可插拔特性的主项目。
第二个接口将声明一组接口类,可插入插件需要实现这些接口类才能在主项目中调用。
您的主父插件项目将具有第二个接口插件引用,如下所示在其 pom.xml 中依赖,以便自定义插件实现接口。它还将具有第四个项目的引用,以便所有自定义插件都位于我们主项目的类路径中。
现在,当您构建父 POM 项目时,所有自定义插件都将构建为 jar 并存储在您的 .M2 文件夹中。
现在要在主项目中访问这些自定义插件,您需要在 pon.xml 中将接口引用依赖项作为依赖项,现在的构建将是
一旦第四个项目构建完成,您将在主项目的类路径中拥有所有自定义插件。然后,您可以使用服务加载器类或netbeans查找方法来加载所有实现接口项目中定义的接口的类。
Srry for answering so late , but we also had a similar requirements so what we did is we created four maven project
One would your main project where you want the pluggable nature.
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.
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
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.