Multi module Java projects 应该有 Multiple Gradle Project
Should Multi module Java projects have Multiple Gradle Project
我开始研究 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 模块化,我想知道是否应该将多模块项目拆分为子 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 项目结构。