Home > Enterprise >  How to separate interface and implementation into different modules
How to separate interface and implementation into different modules

Time:06-03

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:

  1. Module A:
   public interface MyTestInterface{
   String testMethod();
}
  1. Module B :
  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.

        <dependency>
            <groupId>test.some</groupId>
            <artifactId>B</artifactId>
        </dependency>

The pom of the project that contains A and B module is some likes this:

    <modules>
        <module>A</module>
        <module>B</module>

    </modules>

CodePudding user response:

First let's clarify something here.

Your question has to do with apache-maven modules, not with java modules.

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

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.

What however your requirement needs is exactly the opposite. You need Module A inside Module B.

For this you have to go to .pom of Module B and add the following

    <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.

CodePudding user response:

You need a parent maven project with packaging as POM and modules under them!

Follow this

  1. Create a maven project with packaging type as POM (It should look something like this, sorry for formatting issues)

    <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>

  2. Create an interface-module with packaging JAR with parent pointing to project you created in point 1 (See below)

    <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>

  3. 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

    <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!

  • Related