简体   繁体   中英

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!

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 aa good approach to try to draw a parallel between Java packages and classes , to Maven groupIDs and artifactIDs .

Generally I begin with the assumption that everything starts at the project parent in "class" name form.

This is because I consider a module with projectname_ prefix to be a single of a multi-module for development/build and project organization related purposes. Essentially meaning it isn't very useful outside the project, and its "API" is not at all a respectable API. Now, it might get more structured as we go on, but it doesn't start out like that. So I don't begin by assuming it has a real java package code hierarchy, when one has yet to shape out.

With that said, assuming my multi-module convention is as follows:

  • projectname-pom
    • projectname-parent-pom
    • projectname-core
    • projectname-app

All of which would use the root com.example.projectname , at least for where each one begins, though some may have java sub-packages.

Now as the project evolves, I might eventually create a sub-package that should be a module's main package. Likely as development work continued, it became clear what the code structure should evolve into. At that point the project might be:

  • projectname-pom:
    • projectname-parent-pom
    • projectname-lib-pom
      • projectname-lib-parent-pom
      • projectname-lib-core: com.example.projectname.lib
      • projectname-lib-extra: com.example.projectname.lib.extras
    • projectname-app: com.example.projectname.app

So I begin at option 3 when things are just starting out and very flat, and end at option 1 if/when the modules start to actually reflect the underlying java package code hierarchy.

Now assuming you have a thoughtful java package code hiarchy, then making that match the maven modules is ideal, however that takes more work, and if you do it before things have settled it ends up being worse than completely flat. Good hierarchy beats no hierarchy, but no hierarchy beats a bad hiarchy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM