[英]How to separate interface and implementation into different modules
Hi everyone i am creating a project in Spring Boot with multi-module maven, what i would like to obtain is to seperate the interface and implementation in two different modules, like this:大家好,我正在使用多模块 maven 在 Spring Boot 中创建一个项目,我想要获得的是在两个不同的模块中分离接口和实现,如下所示:
public interface MyTestInterface{
String testMethod();
}
public class MyTestImpl implements MyTestInterface{
@Override
String testMethod(){
return "foo";
};
}
I can't get this result, could you give me an example of how to get it?我无法得到这个结果,你能给我一个如何得到它的例子吗? Also is it a good thing to do this?
这样做也是一件好事吗? As for the poms at the moment I only have a B versus A addiction in the poms.
至于目前的 poms,我对 poms 只有 B 与 A 的瘾。
<dependency>
<groupId>test.some</groupId>
<artifactId>B</artifactId>
</dependency>
The pom of the project that contains A and B module is some likes this:包含 A 和 B 模块的项目的 pom 是这样的:
<modules>
<module>A</module>
<module>B</module>
</modules>
First let's clarify something here.首先让我们在这里澄清一些事情。
Your question has to do with apache-maven
modules, not with java modules.您的问题与
apache-maven
模块有关,而不是与 java 模块有关。
So now that we are clear let us focus on the issue that you have.因此,既然我们已经清楚了,让我们专注于您遇到的问题。
From comments:来自评论:
I couldn't get what I want, i can't import my interface from module A to B
我无法得到我想要的,我无法将我的接口从模块 A 导入到 B
Yes because what you use is是的,因为您使用的是
<dependency>
<groupId>test.some</groupId>
<artifactId>B</artifactId>
</dependency>
which means that you have this dependency in module A. So you try to import Module B
into Module A
.这意味着您在模块 A 中有这种依赖关系。因此您尝试将
Module B
导入到Module A
中。
What however your requirement needs is exactly the opposite.但是,您的要求恰恰相反。 You need
Module A
inside Module B
.您需要
Module A
内Module B
。
For this you have to go to .pom
of Module B
and add the following为此,您必须转到
Module B
的.pom
并添加以下内容
<dependency>
<groupId>test.some</groupId>
<artifactId>A</artifactId>
</dependency>
and also remove the previous dependency that you have in .pom
inside Module A
because if not you will have circular dependency issue.并删除您在
Module A
内的.pom
中的先前依赖项,因为如果没有,您将遇到循环依赖问题。
POM
and modules under them!POM
和模块! Follow this按照这个
Create a maven project with packaging type as POM (It should look something like this, sorry for formatting issues)创建一个打包类型为 POM 的 maven 项目(它应该看起来像这样,对于格式问题很抱歉)
<modelVersion>4.0.0</modelVersion> <groupId>com.multimodule</groupId> <artifactId>com.example.multimodule</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>interface-module</module> <module>implementation-module</module> </modules>
Create an interface-module with packaging JAR with parent pointing to project you created in point 1 (See below)创建一个带有包装 JAR 的接口模块,其父指向您在第 1 点创建的项目(见下文)
<parent> <groupId>com.multimodule</groupId> <artifactId>com.example.multimodule</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.example.interface-module</groupId> <artifactId>interface-module</artifactId> <version>1.0</version> <packaging>jar</packaging>
Create an implementation-module with packaging JAR with parent pointing to project you created in point 1 and dependency
to project created in point 2创建一个带有打包 JAR 的实现模块,其父项指向您在第 1 点创建的项目以及对第 2 点创建的项目的
dependency
项
<parent> <groupId>com.multimodule</groupId> <artifactId>com.example.multimodule</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.example.implementation-module</groupId> <artifactId>implementation-module</artifactId> <version>1.0</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.example.interface-module</groupId> <artifactId>interface-module</artifactId> <version>1.0</version> </dependency> </dependencies>
Likewise for other modules !其他模块也是如此! I was able to achieve what you asked , let me know if you need a reference, will upload and send GitHub link!
我能够实现您的要求,如果您需要参考,请告诉我,将上传并发送 GitHub 链接!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.