Browse Source

fix(maven-aggregation project): 2.0版本 区分单模块与多模块

main
wandong 8 months ago
parent
commit
1a79ac5160
  1. 240
      README_EN.md
  2. 1
      donate/alipay_qr.png
  3. 1
      donate/wechat_qr.png
  4. 129
      readMe.md
  5. 184
      src/main/java/com/wd/maven/aggregation/AggregationProjectDialog.java
  6. 287
      src/main/java/com/wd/maven/aggregation/AggregationProjectGenerator.java
  7. 23
      src/main/java/com/wd/maven/aggregation/MavenAggregationProjectGenerator.java
  8. 4
      src/main/java/com/wd/maven/aggregation/QuickstartAggregationAction.java

240
README_EN.md

@ -0,0 +1,240 @@
# Maven-aggregation Quickstart
Maven-aggregation Quickstart is an IntelliJ IDEA plugin designed to quickly generate standard multi-module Maven project structures. With this plugin, you can create modular projects with one click and automatically configure dependencies between modules. It also supports custom module names and Java versions, making it perfect for rapid Java project startup.
---
## 📌 Key Features
- **One-click creation of Maven multi-module project structure**
- **Automatic generation of dependencies between modules**
- **Support for custom module names**
- **Support for Java version selection (8/11/17/21)**
- **Automatic generation of project README.md and .gitignore files**
- **Support for both single-module DDD architecture and multi-module architecture**
- **Automatic addition of common dependencies (Lombok, Hutool, SLF4J, Logback, Swagger, JUnit, etc.)**
---
## 🚀 Usage
### 1. Install the Plugin
1. Open IntelliJ IDEA.
2. Go to `Settings (Preferences)` > `Plugins`.
3. Click on the `Marketplace` tab.
4. Search for `Maven-aggregation Quickstart`.
5. Click `Install` to install the plugin.
6. Restart IntelliJ IDEA.
### 2. Create a Project
1. Create an empty project in IntelliJ IDEA.
2. Click on the menu `File > New > Create Maven Aggregation Project`.
3. Fill in the following information:
- GroupId: The organization identifier for the Maven project (e.g. `com.example`)
- ArtifactId: Project name
- Version: Project version (default `1.0.0`)
- Select architecture mode: Single-module DDD architecture or multi-module architecture
- Module names: Enter multiple module names (e.g. `api`, `service`, `pojo`)
- Java version: Select the Java version used by the project (8/11/17/21)
- Common dependencies: Select dependencies to add (Lombok, Hutool, SLF4J, etc.)
4. Click `Finish`, and the plugin will automatically generate the project structure.
---
## 📁 Project Structure Examples
### Multi-module Architecture Project Structure
```
your-project-name/
├── api/ # API interface module exposed to the outside world
├── service/ # Business logic module
├── mapper/ # Data access layer module
├── pojo/ # Entity class module
├── common/ # Public utility class and common component module
└── pom.xml # Parent Maven configuration file
Each module contains the following standard directory structure:
module-name/
├── src/
│ ├── main/
│ │ ├── java/ # Java source code
│ │ └── resources/ # Static resource files
│ └── test/
│ └── java/ # Test code
└── pom.xml # Module-level Maven configuration file
```
### Single-module DDD Architecture Project Structure
```
your-project-name/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/example/yourproject/
│ │ │ ├── application/ # Application layer
│ │ │ ├── domain/ # Domain layer
│ │ │ ├── infrastructure/ # Infrastructure layer
│ │ │ └── interfaces/ # Interface layer
│ │ └── resources/
│ └── test/
│ └── java/
├── pom.xml
├── README.md
└── .gitignore
```
---
## 🧩 Module Descriptions
### Multi-module Architecture Module Descriptions
| Module Name | Description |
|-------------|-------------|
| `api` | API interface module exposed to the outside world |
| `common` | Public utility class and common component module |
| `mapper` | Data access layer module (usually used for database operations) |
| `pojo` | Entity class module |
| `service` | Business logic module |
> Note: You can customize module names, and the plugin will automatically recognize and generate corresponding descriptions.
### Single-module DDD Architecture Layer Descriptions
| Layer | Description |
|-------|-------------|
| `application` | Application layer, responsible for business process control and use case implementation |
| `domain` | Domain layer, containing core business logic and domain models |
| `infrastructure` | Infrastructure layer, providing technical implementation and external dependencies |
| `interfaces` | Interface layer, responsible for providing external service interfaces |
---
## 🧪 Building the Project
The generated Maven project can be built using standard Maven commands:
```bash
# Clean the project
mvn clean
# Compile the project
mvn compile
# Run tests
mvn test
# Package the project
mvn package
# Install to local repository
mvn install
```
---
## 🛠️ Plugin Development and Building
If you want to modify or build the plugin from source code, please follow these steps:
### 1. Clone the Repository
```
git clone https://github.com/yourname/maven-aggregation.git
cd maven-aggregation
```
### 2. Build the Plugin
Use Gradle to build the plugin:
```bash
# Clean the project
./gradlew clean
# Build the project
./gradlew build
```
After building, the plugin `.jar` file will be generated in the `build/libs/` directory.
### 3. Install the Plugin in IntelliJ IDEA
1. Open IntelliJ IDEA.
2. Go to `Settings (Preferences)` > `Plugins`.
3. Click `Install Plugin from Disk...`.
4. Select `build/libs/maven-aggregation-1.1.1.jar`.
5. Install and restart IntelliJ IDEA.
---
## 💖 Support the Project
If you find this plugin helpful, please consider supporting the project. Your support will help us continue to improve and maintain this plugin. You can support us through the following ways:
<table>
<tr align="center">
<td>
<strong>Alipay</strong><br>
<em>(Chinese users)</em>
</td>
<td>
<strong>WeChat Pay</strong><br>
<em>(Chinese users)</em>
</td>
<td>
<strong>PayPal</strong><br>
<em>(International users)</em>
</td>
</tr>
<tr align="center">
<td><img src="donate/alipay_qr.png" width="150" alt="Alipay QR Code"></td>
<td><img src="donate/wechat_qr.png" width="150" alt="WeChat Pay QR Code"></td>
<td>
<a href="https://www.paypal.me/yourname">
<img src="https://img.shields.io/badge/PayPal-003087?style=for-the-badge&logo=paypal&logoColor=white" alt="PayPal">
</a>
</td>
</tr>
<tr align="center">
<td>
<strong>Buy Me a Coffee</strong><br>
<em>(International users)</em>
</td>
<td>
<strong>GitHub Sponsors</strong><br>
<em>(International users)</em>
</td>
<td>
<strong>Open Collective</strong><br>
<em>(International users)</em>
</td>
</tr>
<tr align="center">
<td>
<a href="https://www.buymeacoffee.com/yourname">
<img src="https://img.shields.io/badge/Buy%20Me%20a%20Coffee-ffdd00?style=for-the-badge&logo=buy-me-a-coffee&logoColor=black" alt="Buy Me a Coffee">
</a>
</td>
<td>
<a href="https://github.com/sponsors/yourname">
<img src="https://img.shields.io/badge/GitHub%20Sponsors-EA4AAA?style=for-the-badge&logo=github&logoColor=white" alt="GitHub Sponsors">
</a>
</td>
<td>
<a href="https://opencollective.com/yourname">
<img src="https://img.shields.io/badge/Open%20Collective-7FADF2?style=for-the-badge&logo=open-collective&logoColor=white" alt="Open Collective">
</a>
</td>
</tr>
</table>
---
## 📞 Contact Us
If you have any questions or suggestions, please contact:
📧 Email: 1938023944@qq.com
🌐 Website: [https://www.wandong.com](https://www.wandong.com)

1
donate/alipay_qr.png

@ -0,0 +1 @@
placeholder for alipay qr code

1
donate/wechat_qr.png

@ -0,0 +1 @@
placeholder for wechat qr code

129
readMe.md

@ -9,8 +9,10 @@ Maven-aggregation Quickstart 是一个 IntelliJ IDEA 插件,用于快速生成
- **一键创建 Maven 多模块项目结构**
- **自动生成模块间的依赖关系**
- **支持自定义模块名称**
- **支持选择 Java 版本**
- **自动生成 README.md 和 .gitignore 文件**
- **支持选择 Java 版本(8/11/17/21)**
- **自动生成项目 README.md 和 .gitignore 文件**
- **支持单模块DDD架构和多模块架构两种模式**
- **自动添加常用依赖(Lombok、Hutool、SLF4J、Logback、Swagger、JUnit等)**
---
@ -27,41 +29,69 @@ Maven-aggregation Quickstart 是一个 IntelliJ IDEA 插件,用于快速生成
### 2. 创建项目
1. 打开 IntelliJ IDEA
1. 在 IntelliJ IDEA 中创建一个空项目(Empty Project)
2. 点击菜单 `File > New > Create Maven Aggregation Project`
3. 填写以下信息:
- GroupId: Maven 项目的组织标识(例如:`com.example`)
- ArtifactId: 项目名称
- Version: 项目版本(默认 `1.0.0`
- 选择架构模式:单模块DDD架构或多模块架构
- 模块名称:输入多个模块名称(例如:`api`, `service`, `pojo`
- Java 版本:选择项目使用的 Java 版本
- Java 版本:选择项目使用的 Java 版本(8/11/17/21)
- 常用依赖:选择需要添加的依赖(Lombok、Hutool、SLF4J等)
4. 点击 `Finish`,插件会自动生成项目结构。
---
## 📁 项目结构示例
生成的项目目录结构如下:
your-project-name/
├── api/
├── service/
├── pojo/
├── common/
├── mapper
### 多模块架构项目结构
```
your-project-name/
├── api/ # 对外暴露的 API 接口模块
├── service/ # 业务逻辑模块
├── mapper/ # 数据访问层模块
├── pojo/ # 实体类模块
├── common/ # 公共工具类和通用组件模块
└── pom.xml # 父级 Maven 配置文件
每个模块都包含以下标准目录结构:
module-name/
├── src/ │
├── main/ │ │
├── java/ Java 源代码
│ │ └── resources/ 静态资源文件
│ └── test/ │ ├── java/ 测试代码
module-name/
├── src/
│ ├── main/
│ │ ├── java/ # Java 源代码
│ │ └── resources/ # 静态资源文件
│ └── test/
│ └── java/ # 测试代码
└── pom.xml # 模块级 Maven 配置文件
```
### 单模块DDD架构项目结构
```
your-project-name/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/example/yourproject/
│ │ │ ├── application/ # 应用层
│ │ │ ├── domain/ # 领域层
│ │ │ ├── infrastructure/ # 基础设施层
│ │ │ └── interfaces/ # 接口层
│ │ └── resources/
│ └── test/
│ └── java/
├── pom.xml
├── README.md
└── .gitignore
```
---
## 🧩 模块说明
### 多模块架构模块说明
| 模块名称 | 说明 |
|---------|------|
| `api` | 对外暴露的 API 接口模块 |
@ -72,32 +102,36 @@ module-name/
> 注意:你可以自定义模块名称,插件会自动识别并生成对应描述。
---
## 🧪 构建项目
### 单模块DDD架构层级说明
为了确保项目的顺利构建,请按照以下步骤操作:
| 层级 | 说明 |
|------|------|
| `application` | 应用层,负责业务流程控制和用例实现 |
| `domain` | 领域层,包含核心业务逻辑和领域模型 |
| `infrastructure` | 基础设施层,提供技术实现和外部依赖 |
| `interfaces` | 接口层,负责对外提供服务接口 |
1. **打开终端**:在你的项目根目录下打开一个新的终端窗口。
2. **执行 Maven 命令**:运行以下命令来清理并安装项目依赖。
---
```
# 1. 停止所有 Gradle 进程
./gradlew --stop
## 🧪 构建项目
# 2. 删除缓存目录(确保路径正确)
rm -rf /Users/wandong/.gradle/caches/
生成的 Maven 项目可以使用标准的 Maven 命令进行构建:
# 3. 删除本地配置缓存(如果存在)
rm -rf .gradle/configuration-cache/
```bash
# 清理项目
mvn clean
# 4. 清理项目构建文件(可选)
rm -rf build/ .gradle/
# 编译项目
mvn compile
# 5. 使用禁用配置缓存的方式重新构建
./gradlew build --no-configuration-cache
# 运行测试
mvn test
# 打包项目
mvn package
# 安装到本地仓库
mvn install
```
@ -111,39 +145,28 @@ rm -rf build/ .gradle/
```
git clone https://github.com/yourname/maven-aggregation.git
cd maven-aggregation
```
### 2. 构建插件
使用 Maven 构建插件:
```
# 1. 停止所有 Gradle 进程
./gradlew --stop
# 2. 删除缓存目录(确保路径正确)
rm -rf /Users/wandong/.gradle/caches/
# 3. 删除本地配置缓存(如果存在)
rm -rf .gradle/configuration-cache/
# 4. 清理项目构建文件(可选)
rm -rf build/ .gradle/
使用 Gradle 构建插件:
# 5. 使用禁用配置缓存的方式重新构建
./gradlew build --no-configuration-cache
```bash
# 清理项目
./gradlew clean
# 构建项目
./gradlew build
```
构建完成后,插件的 `.jar` 文件会生成在 `target/` 目录下。
构建完成后,插件的 `.jar` 文件会生成在 `build/libs/` 目录下。
### 3. 安装插件到 IntelliJ IDEA
1. 打开 IntelliJ IDEA。
2. 进入 `Settings (Preferences)` > `Plugins`
3. 点击 `Install Plugin from Disk...`
4. 选择 `target/maven-aggregation.jar`
4. 选择 `build/libs/maven-aggregation-1.1.1.jar`
5. 安装并重启 IntelliJ IDEA.
---

184
src/main/java/com/wd/maven/aggregation/AggregationProjectDialog.java

@ -6,9 +6,12 @@ import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTextField;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -19,8 +22,14 @@ public class AggregationProjectDialog extends DialogWrapper {
private JBTextField artifactIdField;
private JBTextField versionField;
private JBTextField modulesField;
private JBTextField domainNameField; // 用于单模块DDD架构
private JComboBox<String> javaVersionComboBox;
// 单模块/多模块选择
private JRadioButton singleModuleRadioButton;
private JRadioButton multiModuleRadioButton;
private ButtonGroup moduleTypeButtonGroup;
// 预定义模块复选框
private JBCheckBox apiCheckBox;
private JBCheckBox commonCheckBox;
@ -28,6 +37,10 @@ public class AggregationProjectDialog extends DialogWrapper {
private JBCheckBox pojoCheckBox;
private JBCheckBox serviceCheckBox;
private JBCheckBox addDependenciesCheckBox;
// 模块相关面板,用于动态显示/隐藏
private JPanel modulesPanel;
private JPanel modulesFieldsPanel;
public AggregationProjectDialog(Project project) {
super(project);
@ -36,15 +49,46 @@ public class AggregationProjectDialog extends DialogWrapper {
}
@Override
protected JComponent createCenterPanel() {
protected @Nullable JComponent createCenterPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(5, 5, 5, 5);
// Group ID
// 单模块/多模块选择
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.0;
panel.add(new JLabel("项目类型:"), c);
JPanel moduleTypePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
singleModuleRadioButton = new JRadioButton("单模块DDD架构", false);
multiModuleRadioButton = new JRadioButton("多模块架构", true);
moduleTypeButtonGroup = new ButtonGroup();
moduleTypeButtonGroup.add(singleModuleRadioButton);
moduleTypeButtonGroup.add(multiModuleRadioButton);
moduleTypePanel.add(singleModuleRadioButton);
moduleTypePanel.add(multiModuleRadioButton);
c.gridx = 1;
c.weightx = 1.0;
panel.add(moduleTypePanel, c);
// 领域名称(用于单模块DDD架构)
c.gridx = 0;
c.gridy = 1;
c.weightx = 0.0;
panel.add(new JLabel("领域名称:"), c);
c.gridx = 1;
c.weightx = 1.0;
domainNameField = new JBTextField("order");
panel.add(domainNameField, c);
// Group ID
c.gridx = 0;
c.gridy = 2;
c.weightx = 0.0;
panel.add(new JBLabel("Group ID:"), c);
c.gridx = 1;
@ -54,7 +98,7 @@ public class AggregationProjectDialog extends DialogWrapper {
// Artifact ID
c.gridx = 0;
c.gridy = 1;
c.gridy = 3;
c.weightx = 0.0;
panel.add(new JBLabel("Artifact ID:"), c);
@ -65,7 +109,7 @@ public class AggregationProjectDialog extends DialogWrapper {
// Version
c.gridx = 0;
c.gridy = 2;
c.gridy = 4;
c.weightx = 0.0;
panel.add(new JBLabel("Version:"), c);
@ -76,7 +120,7 @@ public class AggregationProjectDialog extends DialogWrapper {
// Java Version
c.gridx = 0;
c.gridy = 3;
c.gridy = 5;
c.weightx = 0.0;
panel.add(new JBLabel("Java Version:"), c);
@ -85,44 +129,62 @@ public class AggregationProjectDialog extends DialogWrapper {
javaVersionComboBox = new JComboBox<>(new String[]{"8", "11", "17", "21"});
panel.add(javaVersionComboBox, c);
// 创建模块相关面板
this.modulesPanel = new JPanel(new GridBagLayout());
GridBagConstraints mc = new GridBagConstraints();
mc.fill = GridBagConstraints.HORIZONTAL;
mc.insets = new Insets(5, 5, 5, 5);
// 预定义模块
c.gridx = 0;
c.gridy = 4;
c.weightx = 0.0;
panel.add(new JBLabel("预定义模块:"), c);
mc.gridx = 0;
mc.gridy = 0;
mc.weightx = 0.0;
this.modulesPanel.add(new JBLabel("预定义模块:"), mc);
// 模块复选框面板
JPanel modulesPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
this.modulesFieldsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
apiCheckBox = new JBCheckBox("api", true);
commonCheckBox = new JBCheckBox("common", true);
mapperCheckBox = new JBCheckBox("mapper", true);
pojoCheckBox = new JBCheckBox("pojo", true);
serviceCheckBox = new JBCheckBox("service", true);
modulesPanel.add(apiCheckBox);
modulesPanel.add(commonCheckBox);
modulesPanel.add(mapperCheckBox);
modulesPanel.add(pojoCheckBox);
modulesPanel.add(serviceCheckBox);
this.modulesFieldsPanel.add(apiCheckBox);
this.modulesFieldsPanel.add(commonCheckBox);
this.modulesFieldsPanel.add(mapperCheckBox);
this.modulesFieldsPanel.add(pojoCheckBox);
this.modulesFieldsPanel.add(serviceCheckBox);
c.gridx = 1;
c.weightx = 1.0;
panel.add(modulesPanel, c);
mc.gridx = 1;
mc.weightx = 1.0;
this.modulesPanel.add(this.modulesFieldsPanel, mc);
// 自定义模块
mc.gridx = 0;
mc.gridy = 1;
mc.weightx = 0.0;
this.modulesPanel.add(new JBLabel("自定义模块 (逗号分隔):"), mc);
mc.gridx = 1;
mc.weightx = 1.0;
modulesField = new JBTextField();
this.modulesPanel.add(modulesField, mc);
// 将模块面板添加到主面板
c.gridx = 0;
c.gridy = 5;
c.gridy = 6;
c.weightx = 0.0;
panel.add(new JBLabel("自定义模块 (逗号分隔):"), c);
c.gridwidth = 1;
panel.add(new JBLabel(), c); // 占位
c.gridx = 1;
c.weightx = 1.0;
modulesField = new JBTextField();
panel.add(modulesField, c);
c.gridwidth = 1;
panel.add(this.modulesPanel, c);
// 添加常用依赖选项
c.gridx = 0;
c.gridy = 6;
c.gridy = 7;
c.weightx = 0.0;
panel.add(new JBLabel("添加常用依赖:"), c);
@ -132,9 +194,36 @@ public class AggregationProjectDialog extends DialogWrapper {
addDependenciesCheckBox.setSelected(true); // 默认选中
panel.add(addDependenciesCheckBox, c);
// 添加选择监听器
singleModuleRadioButton.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
updateUIComponentsVisibility();
}
});
multiModuleRadioButton.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
updateUIComponentsVisibility();
}
});
// 初始更新UI组件可见性
updateUIComponentsVisibility();
return panel;
}
/**
* 根据选择的项目类型更新UI组件的可见性
*/
private void updateUIComponentsVisibility() {
boolean isMultiModule = multiModuleRadioButton.isSelected();
domainNameField.setVisible(!isMultiModule); // 单模块时显示领域名称
modulesPanel.setVisible(isMultiModule); // 多模块时显示模块选择
}
@Override
protected ValidationInfo doValidate() {
if (groupIdField.getText().trim().isEmpty()) {
@ -146,6 +235,10 @@ public class AggregationProjectDialog extends DialogWrapper {
if (versionField.getText().trim().isEmpty()) {
return new ValidationInfo("Version不能为空", versionField);
}
// 单模块时验证领域名称
if (singleModuleRadioButton.isSelected() && domainNameField.getText().trim().isEmpty()) {
return new ValidationInfo("领域名称不能为空", domainNameField);
}
return null;
}
@ -168,26 +261,47 @@ public class AggregationProjectDialog extends DialogWrapper {
public String[] getModules() {
List<String> modulesList = new ArrayList<>();
// 添加选中的预定义模块
if (apiCheckBox.isSelected()) modulesList.add("api");
if (commonCheckBox.isSelected()) modulesList.add("common");
if (mapperCheckBox.isSelected()) modulesList.add("mapper");
if (pojoCheckBox.isSelected()) modulesList.add("pojo");
if (serviceCheckBox.isSelected()) modulesList.add("service");
// 添加自定义模块
String customModules = modulesField.getText().trim();
if (!customModules.isEmpty()) {
modulesList.addAll(Arrays.asList(customModules.split("\\s*,\\s*")));
// 只有在多模块模式下才收集模块
if (multiModuleRadioButton.isSelected()) {
// 添加选中的预定义模块
if (apiCheckBox.isSelected()) modulesList.add("api");
if (commonCheckBox.isSelected()) modulesList.add("common");
if (mapperCheckBox.isSelected()) modulesList.add("mapper");
if (pojoCheckBox.isSelected()) modulesList.add("pojo");
if (serviceCheckBox.isSelected()) modulesList.add("service");
// 添加自定义模块
String customModules = modulesField.getText().trim();
if (!customModules.isEmpty()) {
modulesList.addAll(Arrays.asList(customModules.split("\\s*,\\s*")));
}
}
return modulesList.toArray(new String[0]);
}
/**
* 是否为单模块模式
*/
public boolean isSingleModule() {
return singleModuleRadioButton.isSelected();
}
/**
* 获取领域名称用于单模块DDD架构
*/
public String getDomainName() {
return domainNameField.getText().trim();
}
/**
* 是否添加常用依赖
*/
public boolean shouldAddDependencies() {
public boolean isAddDependencies() {
return addDependenciesCheckBox.isSelected();
}
// 重复方法已在原始代码中存在,保留原始实现
}

287
src/main/java/com/wd/maven/aggregation/AggregationProjectGenerator.java

@ -14,7 +14,35 @@ public class AggregationProjectGenerator {
private static final Logger LOG = Logger.getInstance(AggregationProjectGenerator.class);
/**
* 生成项目支持单模块DDD架构和多模块架构
* @param project 项目
* @param groupId 组织ID
* @param artifactId 项目ID
* @param version 版本
* @param modules 模块列表多模块时使用
* @param javaVersion Java版本
* @param addDependencies 是否添加常用依赖
*/
public void generateProject(Project project, String groupId, String artifactId, String version, String[] modules, String javaVersion, boolean addDependencies) {
// 默认使用多模块模式
generateProject(project, groupId, artifactId, version, modules, javaVersion, addDependencies, false, "");
}
/**
* 生成项目支持单模块DDD架构和多模块架构
* @param project 项目
* @param groupId 组织ID
* @param artifactId 项目ID
* @param version 版本
* @param modules 模块列表多模块时使用
* @param javaVersion Java版本
* @param addDependencies 是否添加常用依赖
* @param isSingleModule 是否为单模块模式
* @param domainName 领域名称单模块DDD架构时使用
*/
public void generateProject(Project project, String groupId, String artifactId, String version, String[] modules,
String javaVersion, boolean addDependencies, boolean isSingleModule, String domainName) {
String baseDir = project.getBasePath();
if ( baseDir == null || baseDir.isEmpty()) {
LOG.warn("Project base directory not found.");
@ -22,34 +50,53 @@ public class AggregationProjectGenerator {
}
Path projectPath = Paths.get(baseDir);
LOG.info("开始生成Maven聚合项目: " + artifactId);
try {
// 1. 先创建所有目录结构
createAllDirectoryStructures(projectPath, artifactId, modules);
// 2. 写入父 POM(包含完整的依赖管理)
writeParentPomWithDependencies(projectPath, groupId, artifactId, version, modules, javaVersion, addDependencies);
// 3. 写入子模块POM(包含依赖关系)
for (String module : modules) {
String moduleName = artifactId + "-" + module;
Path modulePath = projectPath.resolve(moduleName);
writeModulePomWithDependencies(modulePath, groupId, artifactId, version, moduleName, modules, addDependencies);
if (isSingleModule) {
// 单模块DDD架构
LOG.info("开始生成单模块DDD架构项目: " + artifactId);
// 1. 创建DDD目录结构
createDddDirectoryStructure(projectPath, groupId, domainName);
// 2. 写入单模块POM
writeSingleModulePom(projectPath, groupId, artifactId, version, javaVersion, addDependencies);
// 3. 生成辅助文件
writeDddReadme(projectPath, artifactId, domainName);
writeGitignore(projectPath);
LOG.info("单模块DDD架构项目生成完成: " + artifactId);
} else {
// 多模块架构(原有逻辑)
LOG.info("开始生成Maven聚合项目: " + artifactId);
// 1. 先创建所有目录结构
createAllDirectoryStructures(projectPath, artifactId, modules);
// 2. 写入父 POM(包含完整的依赖管理)
writeParentPomWithDependencies(projectPath, groupId, artifactId, version, modules, javaVersion, addDependencies);
// 3. 写入子模块POM(包含依赖关系)
for (String module : modules) {
String moduleName = artifactId + "-" + module;
Path modulePath = projectPath.resolve(moduleName);
writeModulePomWithDependencies(modulePath, groupId, artifactId, version, moduleName, modules, addDependencies);
}
// 4. 生成辅助文件
writeReadme(projectPath, artifactId, modules);
writeGitignore(projectPath);
LOG.info("Maven聚合项目生成完成: " + artifactId);
}
// 4. 生成辅助文件
writeReadme(projectPath, artifactId, modules);
writeGitignore(projectPath);
LOG.info("Maven聚合项目生成完成: " + artifactId);
} catch (Exception e) {
LOG.error("生成项目时发生错误", e);
}
}
/**
* 创建所有目录结构
* 创建所有目录结构多模块
*/
private void createAllDirectoryStructures(Path projectPath, String artifactId, String[] modules) {
for (String module : modules) {
@ -61,6 +108,206 @@ public class AggregationProjectGenerator {
}
}
/**
* 创建DDD目录结构单模块
*/
private void createDddDirectoryStructure(Path projectPath, String groupId, String domainName) {
// 基础路径
String basePackagePath = groupId.replace('.', '/') + "/" + domainName;
Path mainJavaPath = projectPath.resolve("src/main/java").resolve(basePackagePath);
Path testJavaPath = projectPath.resolve("src/test/java").resolve(basePackagePath);
Path resourcesPath = projectPath.resolve("src/main/resources");
// 创建基础目录结构
createDirectoryStructure(mainJavaPath);
createDirectoryStructure(testJavaPath);
createDirectoryStructure(resourcesPath);
// 创建DDD目录结构 - 应用层
createDirectoryStructure(mainJavaPath.resolve("application/service"));
createDirectoryStructure(mainJavaPath.resolve("application/dto"));
// 创建DDD目录结构 - 领域层
createDirectoryStructure(mainJavaPath.resolve("domain/model"));
createDirectoryStructure(mainJavaPath.resolve("domain/service"));
createDirectoryStructure(mainJavaPath.resolve("domain/event"));
createDirectoryStructure(mainJavaPath.resolve("domain/repository"));
// 创建DDD目录结构 - 基础设施层
createDirectoryStructure(mainJavaPath.resolve("infrastructure/persistence"));
createDirectoryStructure(mainJavaPath.resolve("infrastructure/message"));
createDirectoryStructure(mainJavaPath.resolve("infrastructure/external"));
// 创建DDD目录结构 - 接口层
createDirectoryStructure(mainJavaPath.resolve("interface/controller/web"));
createDirectoryStructure(mainJavaPath.resolve("interface/controller/rpc"));
}
/**
* 写入单模块POM文件
*/
private void writeSingleModulePom(Path projectPath, String groupId, String artifactId, String version, String javaVersion, boolean addDependencies) {
// 根据Java版本选择合适的依赖版本
String lombokVersion;
String hutoolVersion;
String logbackVersion;
String slf4jVersion;
String swaggerVersion;
String junitVersion;
int javaVersionNum = Integer.parseInt(javaVersion);
// 根据不同Java版本选择兼容的依赖版本
if (javaVersionNum == 11) {
// Java 11 兼容版本
lombokVersion = "1.18.24"; // 兼容Java 8-17
hutoolVersion = "5.8.16"; // 兼容Java 8-17
logbackVersion = "1.2.13"; // 兼容Java 11
slf4jVersion = "1.7.36"; // 兼容Java 8-17
swaggerVersion = "3.0.0"; // SpringFox 3.0.0 兼容Java 11
junitVersion = "5.9.3"; // JUnit 5.9.3 兼容Java 11
} else if (javaVersionNum >= 17 && javaVersionNum < 21) {
// Java 17-20 兼容版本
lombokVersion = "1.18.32"; // 支持Java 17
hutoolVersion = "5.8.25"; // 支持Java 17
logbackVersion = "1.5.6"; // 支持Java 17
slf4jVersion = "2.0.12"; // 支持Java 17
swaggerVersion = "3.0.0"; // SpringFox 3.0.0 兼容Java 17
junitVersion = "5.10.0"; // JUnit 5.10.0 支持Java 17
} else if (javaVersionNum >= 21) {
// Java 21及以上版本
lombokVersion = "1.18.34"; // 最新兼容版本
hutoolVersion = "5.8.25"; // 支持Java 21
logbackVersion = "1.5.6"; // 支持Java 21
slf4jVersion = "2.0.12"; // 支持Java 21
swaggerVersion = "3.0.0"; // SpringFox 3.0.0 兼容Java 21
junitVersion = "5.10.1"; // JUnit 5.10.1 支持Java 21
} else {
// Java 8 兼容版本
lombokVersion = "1.18.24"; // 兼容Java 8-17
hutoolVersion = "5.8.16"; // 兼容Java 8-17
logbackVersion = "1.2.11"; // 兼容Java 8
slf4jVersion = "1.7.36"; // 兼容Java 8
swaggerVersion = "3.0.0"; // SpringFox 3.0.0 兼容Java 8
junitVersion = "5.8.2"; // JUnit 5.8.2 兼容Java 8
}
StringBuilder content = new StringBuilder();
content.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.append("\n<project xmlns=\"http://maven.apache.org/POM/4.0.0\"")
.append("\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"")
.append("\n xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">")
.append("\n <modelVersion>4.0.0</modelVersion>")
.append("\n\n <groupId>").append(groupId).append("</groupId>")
.append("\n <artifactId>").append(artifactId).append("</artifactId>")
.append("\n <version>").append(version).append("</version>")
.append("\n <packaging>jar</packaging>")
.append("\n\n <properties>")
.append("\n <maven.compiler.source>").append(javaVersion).append("</maven.compiler.source>")
.append("\n <maven.compiler.target>").append(javaVersion).append("</maven.compiler.target>")
.append("\n <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>")
.append("\n </properties>")
.append("\n");
if (addDependencies) {
content.append(" <dependencies>")
.append("\n <!-- 代码简化与工具依赖 -->")
.append("\n <dependency>")
.append("\n <groupId>org.projectlombok</groupId>")
.append("\n <artifactId>lombok</artifactId>")
.append("\n <version>").append(lombokVersion).append("</version>")
.append("\n <scope>provided</scope>")
.append("\n </dependency>")
.append("\n <dependency>")
.append("\n <groupId>cn.hutool</groupId>")
.append("\n <artifactId>hutool-all</artifactId>")
.append("\n <version>").append(hutoolVersion).append("</version>")
.append("\n </dependency>")
.append("\n <!-- 日志体系依赖 -->")
.append("\n <!-- SLF4J API - 日志门面接口 -->")
.append("\n <dependency>")
.append("\n <groupId>org.slf4j</groupId>")
.append("\n <artifactId>slf4j-api</artifactId>")
.append("\n <version>").append(slf4jVersion).append("</version>")
.append("\n </dependency>")
.append("\n <!-- Logback - SLF4J 的具体实现 -->")
.append("\n <dependency>")
.append("\n <groupId>ch.qos.logback</groupId>")
.append("\n <artifactId>logback-classic</artifactId>")
.append("\n <version>").append(logbackVersion).append("</version>")
.append("\n </dependency>")
.append("\n <!-- Swagger - API文档生成工具 -->")
.append("\n <dependency>")
.append("\n <groupId>io.springfox</groupId>")
.append("\n <artifactId>springfox-boot-starter</artifactId>")
.append("\n <version>").append(swaggerVersion).append("</version>")
.append("\n </dependency>")
.append("\n <!-- JUnit - 单元测试框架 -->")
.append("\n <dependency>")
.append("\n <groupId>org.junit.jupiter</groupId>")
.append("\n <artifactId>junit-jupiter-api</artifactId>")
.append("\n <version>").append(junitVersion).append("</version>")
.append("\n <scope>test</scope>")
.append("\n </dependency>")
.append("\n <dependency>")
.append("\n <groupId>org.junit.jupiter</groupId>")
.append("\n <artifactId>junit-jupiter-engine</artifactId>")
.append("\n <version>").append(junitVersion).append("</version>")
.append("\n <scope>test</scope>")
.append("\n </dependency>")
.append("\n </dependencies>");
}
content.append("\n</project>");
writeToFile(projectPath.resolve("pom.xml"), content.toString());
}
/**
* 写入DDD架构的README文件
*/
private void writeDddReadme(Path projectPath, String artifactId, String domainName) {
StringBuilder content = new StringBuilder();
content.append("# ").append(artifactId).append(" 单模块DDD架构项目\n\n")
.append("这是一个基于领域驱动设计(DDD)的单模块项目模板。\n\n")
.append("## 项目结构\n\n")
.append("```\n")
.append(artifactId).append("/\n")
.append("├── src/main/java/com/example/").append(domainName).append("\n")
.append("│ ├── application # 应用层(整体流程控制)\n")
.append("│ │ ├── service # 应用服务\n")
.append("│ │ └── dto # 数据传输对象\n")
.append("│ ├── domain # 领域层(核心)\n")
.append("│ │ ├── model # 实体、值对象\n")
.append("│ │ ├── service # 领域服务(逻辑核心)\n")
.append("│ │ ├── event # 领域事件\n")
.append("│ │ └── repository # 仓储接口\n")
.append("│ ├── infrastructure # 基础设施层\n")
.append("│ │ ├── persistence # 数据库实现\n")
.append("│ │ ├── message # 消息队列\n")
.append("│ │ └── external # 外部服务调用\n")
.append("│ └── interface/controller # 用户接口层\n")
.append("│ ├── web # REST API\n")
.append("│ └── rpc # gRPC/Dubbo\n")
.append("└── pom.xml # Maven配置文件\n")
.append("```\n\n")
.append("## DDD架构说明\n\n")
.append("- **应用层(application)**:负责协调领域对象完成用例,处理跨领域模型的操作\n")
.append("- **领域层(domain)**:核心业务逻辑,包含实体、值对象、领域服务等\n")
.append("- **基础设施层(infrastructure)**:提供技术支持,实现仓储接口、消息队列等\n")
.append("- **接口层(interface)**:处理外部请求,转换为应用服务调用\n")
.append("\n")
.append("## 开始使用\n\n")
.append("1. 根据需要修改`pom.xml`文件添加额外依赖\n")
.append("2. 在相应包中添加源代码\n")
.append("3. 使用以下命令构建项目:\n\n")
.append("```bash\n")
.append("mvn clean install\n")
.append("```\n");
writeToFile(projectPath.resolve("README.md"), content.toString());
}
/**
* 写入父POM包含完整的依赖管理
*/

23
src/main/java/com/wd/maven/aggregation/MavenAggregationProjectGenerator.java

@ -51,7 +51,28 @@ public class MavenAggregationProjectGenerator extends WebProjectTemplate<MavenAg
settings.getVersion(),
settings.getModules(),
settings.getJavaVersion(),
settings.isAddDependencies()
settings.isAddDependencies(),
false, // 默认多模块模式
null
);
}
/**
* 生成项目支持单模块/多模块切换
* @param project 项目对象
* @param groupId 组ID
* @param artifactId 构件ID
* @param version 版本号
* @param modules 模块列表
* @param javaVersion Java版本
* @param addDependencies 是否添加依赖
* @param isSingleModule 是否为单模块
* @param domainName 领域名称单模块时使用
*/
public void generateProject(@NotNull Project project, @NotNull String groupId, @NotNull String artifactId, @NotNull String version, @NotNull String[] modules,
@NotNull String javaVersion, boolean addDependencies, boolean isSingleModule, String domainName) {
// 调用核心生成器,支持单模块/多模块模式
AggregationProjectGenerator generator = new AggregationProjectGenerator();
generator.generateProject(project, groupId, artifactId, version, modules, javaVersion, addDependencies, isSingleModule, domainName);
}
}

4
src/main/java/com/wd/maven/aggregation/QuickstartAggregationAction.java

@ -31,10 +31,12 @@ public class QuickstartAggregationAction extends AnAction {
String version = dialog.getVersion();
String[] modules = dialog.getModules();
String javaVersion = dialog.getJavaVersion();
boolean isSingleModule = dialog.isSingleModule();
String domainName = dialog.getDomainName();
// 生成项目
AggregationProjectGenerator generator = new AggregationProjectGenerator();
generator.generateProject(project, groupId, artifactId, version, modules, javaVersion, dialog.shouldAddDependencies());
generator.generateProject(project, groupId, artifactId, version, modules, javaVersion, dialog.isAddDependencies(), isSingleModule, domainName);
// 刷新项目视图
refreshProjectView(project);

Loading…
Cancel
Save