【发布时间】:2015-11-23 13:03:39
【问题描述】:
我正在努力学习更好地理解 Maven 的用法。 我对在多个模块中使用包名称的正确方法有疑问。
我会用一个例子来解释我的问题。
我们有以下项目。
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)
具有以下类。
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)
现在,当我们有了这些东西后,我们将创建以下 Demo 类。
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);
}
}
我们还需要导入包。 你应该在你的项目中使用哪个选项,或者如果选项不是听,那么正确的方法是什么?
选项 1
将所有模块包命名为与模块相同的名称: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;
选项 2
将API模块包命名为与项目根名称相同: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;
选项 3
将所有模块包命名为项目根包名: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;
欢迎所有反馈、建议等。提前谢谢!
【问题讨论】:
-
我通常坚持选项 1,因为我觉得它更清楚