发布项目中的单个软件包作为Maven构件。

huangapple go评论79阅读模式
英文:

Publish a single package from a project as maven artifact

问题

我正在开发一个由许多使用Java(spring boot)编写的API组成的微服务系统,我遇到了一些情况,其中在一个微服务中我必须使用在另一个微服务中定义的模型(例如,当进行API到API的请求并且需要将结果解析为对象时)。这些模型通常在“models”包中定义,不依赖于其他包。我并不太喜欢从一个项目复制/粘贴到另一个项目,因为这容易引入错误,并且会带来很多额外的开销(例如,当需要编辑模型时)。我想知道是否有一种方法,也许可以通过maven配置,将仅模型包提取到单独的jar中,然后将其发布为maven构件(我们有内部仓库)。这将允许我们自由地在一个项目中使用属于另一个项目的模型类,而不必担心维护两个相同模型定义的更新内容。这也比将模型作为完全独立的项目要好得多。

英文:

I'm working on a microservices system composed of many APIs written in Java (spring boot), I'm facing some instances wherein a microservice I have to use models defined in another one (e.g. when an API-to-API request is made and the results need to be parsed into an object). The models are usually defined in a 'models' package and do not depend on other packages. I don't really like the idea to copy/paste from a project to another, as it is error-prone and easily introduces a lot of overhead (e.g. when a model needs to be edited). I was wondering if there is a way, maybe with a maven configuration, to extract only the model package in a separate jar, and then publish it as a maven artifact (we have an internal repository). This would allow us to freely use models classes belonging to a project into another, without worrying about maintaining updated two definitions of the same model. It'd also be much better than having the models as a completely separate project.

答案1

得分: 1

以下是翻译好的内容:

好的,最好提取整个模块而不是包,只需将其添加到pom.xml中。

我不知道我是否正确理解了你的意思 -
让我们考虑一下情况:
你有一个项目A,在该项目中有一个获取端点(get endpoint),假设你有一个名为User的类。
这个类应该是“共享的”,所以我们将它放在ProjectA-API模块中。你将API打包为jar,并将其添加到ProjectA-War模块的依赖中。同时,你也将其添加到ProjectB-War的pom中,然后你可以在调用者和被调用者站点都使用相同的User模型类。

这对你有帮助吗?还是你指的是其他情况?

用于可视化的简单示意图 - 发布项目中的单个软件包作为Maven构件。

英文:

Well it's better to extract whole module instead of package, just add it in pom.xml.

I don't know if I understood you correctly -
Let's consider the situation:
You have Project A where you have get endpoint, lets say you have class User.
This class should be "shared", so let's put it in module ProjectA-API. You define API packaging as jar, and you add it in dependencies in ProjectA-War module. And as well you are adding it to pom in ProjectB-War, and then you can use the same model class for User both in caller's and calling site.

Is it helpful or you meant something different?

Small diagram for visualizing - 发布项目中的单个软件包作为Maven构件。

答案2

得分: 0

没有。

解决方案是将模型放在一个单独的项目中。

或者,您可以构建多模块项目,这样构建许多构件就不会那么麻烦。

英文:

No.

The solution is to have the models a separate project.

Alternatively, you can construct multi-module-projects, so that building many artifacts is less of a hassle.

huangapple
  • 本文由 发表于 2020年4月9日 16:46:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61117247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定