java - 多模块 Java 项目是否应该有多个 Gradle 项目

标签 java gradle module structure

我开始进入 java 模块化,我想知道是否应该将多模块项目拆分为子 Gradle 项目。如果是这种情况或不是这种情况,目录结构会是什么样子?我见过一些非 Gradle 项目的结构冲突示例,例如:

reverse/dns/module/module-info.java 
                ./packages..
reverse/dns/module/module-info.java 
                ./packages..
reverse.dns.module/
            module-info.java // module-info is in the root directory and its name corresponds to parent folder
            reverse/dns/module/packages..

它看起来像这样吗?
如果我有两个模块,我真的很想知道如何构建一个 Gradle 项目。如果与任何内容冲突,我也打算使用 jlink 来创建图像。

最佳答案

我问了这个问题的一个变体:

I'm using gradle and the jlink plugin and have an application that I want to split into two modules, are the modules supposed to be under the src/main/java directory or should it be refactored to src/mod1/ and src/mod2? Another question that comes from this is testing and where to place junit test classes. I'm mostly wondering about the directory structure of all of this. Thanks!


Nick Maiorano并收到此回复:

For your first question, the Java compiler and runtime have no opinion on this. They support either types of directory structures. But Java editors like Intellij and Eclipse kind of force you into having separate source roots for each module. If you plan on having different teams maintaining their own modules, then you'll have to have seperate roots and you would use their modules just like a 3rd party library with module dependencies through a jar or jmod file. Otherwise you could have different modules under a single source but you'd be fighting against Eclipse's/Intellij's module support (but it is possible).


Your second question is much easier: unit test classes should be in the same module as their test subjects. They should have access to every class in the module so it makes sense that they be part of the module. They should be in a separate directory but the same package name as their test subjects (so that they're not blocked from accessing package private methods).


这符合我决定通过拥有两个独立的 Gradle 子项目来构建应用程序的方式,每个子项目都有自己的 src/main/java 目录。我的最终目录结构如下所示:
wit-core/
├── build.gradle
├── core.vim
└── src
    ├── main
    │   └── java
    │       ├── dev
    │       │   └── joshpetit
    │       │       └── wit
    │       │           └── core
    │       │               ├── base
    │       │               ├── interpret
    │       │               └── model
    │       └── module-info.java
    └── test
        └── java
            └── dev
                └── joshpetit
                    └── wit
                        └── core
                            ├── base
                            └── utils
wit-gui/
├── build.gradle
├── Session.vim
└── src
    ├── main
    │   └── java
    │       ├── dev
    │       │   └── joshpetit
    │       │       └── wit
    │       │           └── gui
    │       │               └── launcher
    │       └── module-info.java
    └── test
        └── java
            └── dev
                └── joshpetit
                    └── wit
                        └── gui
                            └── launcer
非常感谢和我一起解析这个问题的人!
如果还有其他人对此感到疑惑,我建议您遵循@FranciscoMatea 的建议并引用 junit's项目结构。

关于java - 多模块 Java 项目是否应该有多个 Gradle 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64954605/

相关文章:

java - 指定的子项已经有父项。您必须首先在子级的父级上调用removeView() (Android)

java - Java 中的内存泄漏。如何清除列表?

java - Apache Lucene 查询迭代所有查询/术语

gradle - 将一些处理附加到另一个Gradle任务

logging - 如何使用 Flyway gradle 插件配置 sql 日志记录?

TypeScript:导入和导出 "at the same time"

java - 处理多个传入和传出连接的最佳设计模式

java - Gradle 包含来自 webapp 目录的 Freemarker 文件

javascript - 将函数传递给使用 webpack 导入类的 web worker

haskell - 在 ghci 中加载已编译模块时出现“加载接口(interface)失败”错误