使用 Maven 创建可插入项目

发布于 2024-10-10 08:05:29 字数 273 浏览 2 评论 0原文

我目前正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

五里雾 2024-10-17 08:05:29

您不想在运行时(而不仅仅是在构建时)插入 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.

空城之時有危險 2024-10-17 08:05:29

很抱歉这么晚才回答,但我们也有类似的要求,所以我们所做的是创建了四个 Maven 项目

  1. 一个是您想要可插拔特性的主项目。

  2. 第二个接口将声明一组接口类,可插入插件需要实现这些接口类才能在主项目中调用。

  3. 第三个是 POM 构建项目,它将充当所有自定义插件的父项目,作为 pom.xml 中的模块,类似于 Spring Boot Starter 项目依赖项。任何需要创建的新插件都会将此 POM 项目作为父项目。该项目基本上是 CICD 的辅助项目,以便构建我们的自定义插件 jar 并将其推送到我们的自定义存储库。
  4. 这将是一个包含所有内容的参考项目自定义插件作为其 pom.xml 中的依赖项,并且此项目信息将位于主项目中,以便当主项目构建时,所有自定义插件都会成为主项目的类路径

您的主父插件项目将具有第二个接口插件引用,如下所示在其 pom.xml 中依赖,以便自定义插件实现接口。它还将具有第四个项目的引用,以便所有自定义插件都位于我们主项目的类路径中。

现在,当您构建父 POM 项目时,所有自定义插件都将构建为 jar 并存储在您的 .M2 文件夹中。

现在要在主项目中访问这些自定义插件,您需要在 pon.xml 中将接口引用依赖项作为依赖项,现在的构建将是

  1. To mvn install the interface project
  2. To mvn install theparent pom project
  3. To mvn安装第四个项目
  4. mvn 安装主项目

一旦第四个项目构建完成,您将在主项目的类路径中拥有所有自定义插件。然后,您可以使用服务加载器类或netbeans查找方法来加载所有实现接口项目中定义的接口的类。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文