Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven package names with multiple modules

I'm trying and learning to understand the usage of Maven better. I have a question about the right way of using the package names with multiple modules.

I will use an example to explain my question.

We have the following project.

project
    project-api             Interfaces (implemented by the modules)
    project-core            Logic
    project-persistence     Data (models, repository etc)
    project-utils           Utilities (hashing, calculating etc)
    project-gui             Graphical User Interface
    project-cli             Command Line Interface
    project-dist            Distribution (generate the JAR's together)

With the following classes.

project-api
    Repository              Interface

project-core
    AbstractRepository      Abstract class (implements Repository)
    Core                    Class

project-persistence
    SampleRepository        Class (extends AbstractRepository)
    Sample                  Class (model)

project-utils
    Calculator              Class

project-gui
    Demo                    Class (main)

Now when we have this stuff, we will create the following Demo class.

public class Demo() {

    public static void main(String[] args) {
        Core core = new Core();
        core.start();

        Repository sampleRepository = new SampleRepository();
        Sample sample = sampleRepository.get();

        Calculator.calc(sample);
    }

}

We also need to import the packages. Which option should you use in your project, or if the option is not listen what will be the right way to go?

Option 1

Name all the modules packages the same as the module -name: com.company.project.api

package com.company.project.gui

import com.company.project.api.persistence.repository.Repository;
import com.company.project.core.Core;
import com.company.project.persistence.repository.SampleRepository;
import com.company.project.persistence.models.Sample;
import com.company.project.utils.Calculator;

Option 2

Name the API module package the same as the project root name: com.company.project

package com.company.project.gui

import com.company.project.persistence.repository.Repository;
import com.company.project.core.Core;
import com.company.project.persistence.repository.SampleRepository;
import com.company.project.persistence.models.Sample;
import com.company.project.utils.Calculator;

Option 3

Name all the modules packages the same as the project root package name: com.company.project

package com.company.project

import com.company.project.repository.Repository;
import com.company.project.Core;
import com.company.project.repository.SampleRepository;
import com.company.project.models.Sample;
import com.company.project.Calculator;

All feedback, suggestions etc are welcome. Thank you in advance!

like image 226
nberlijn Avatar asked Oct 31 '22 20:10

nberlijn


1 Answers

Good that you've considered different options as there's no de facto standard out there for module naming; it's a matter of personal preference. So long as you're consistent and keep your module names terse, and follow the Maven naming conventions for groupID and artifactID, you're good to go.

In any event, I think #1 is the best option. Generally, it's a a good approach to try to draw a parallel between Java packages and classes, to Maven groupIDs and artifactIDs.

like image 119
Keith Avatar answered Nov 11 '22 02:11

Keith