JHipster 是一个开发平台,可以快速生成,开发和部署现代 Web 应用程序+微服务架构。

JHipster 或者称 Java Hipster,是一个应用代码产生器,能够创建 Spring Boot/Spring Cloud + React/VueJs/AngularJS 的应用。使用 JHipster,首先你要配置好 Java 、Git 以及 Maven 或者 Gradle 的环境,然后通过 NodeJs 管理工具安装 JHipster 。

介绍

JHipster 官网: https://www.jhipster.tech/ ,截止本文发布时,最新版本为 8.0.0 。

JHipster 客户端使用到的技术栈有:

jhipster-client-side-options

服务端使用的技术栈有:

jhipster-server-side-options

支持以下部署方式:

jhipster-deploy-options

JHipster 提供了一个 CLI 工具 generator-jhipster 和在线网站 https://start.jhipster.tech/ ,来创建应用。generator-jhipster 支持本地安装和 docker 安装两种方式。另外,还有一个轻量级的定制工具 jhipseter-lite 可以更细力度的定制。

JHipster Pro 是一个 JHipster 国内落地方案,符合国情的代码生成器解决方案,支持 MyBatis、SMS、OSS。当前最新版本对应 JHipster 官方版本:v7.1.0,网站最近一次跟新是在 2021 年 8 月份。

安装 generator-jhipster

前提条件安装 Java 和 NodeJs,然后通过 npm 或者 yarn 安装 generator-jhipster。以下是通过 npm 安装:

1
npm install -g generator-jhipster

查看版本:

1
2
$ npx jhipster --version
8.0.0-rc.1

另外,可以在 docker 里面安装:

1
2
3
4
5
docker image pull jhipster/jhipster

docker container run --name jhipster -v ~/jhipster:/home/jhipster/app -v ~/.m2:/home/jhipster/.m2 -p 8080:8080 -p 9000:9000 -p 3001:3001 -d -t jhipster/jhipster

docker container exec -it jhipster bash

创建应用

为了方便研究代码,可以使用不同技术分多次创建应用。

首先,创建一个最小化的简单应用:

1
mkdir jhipster-simple-demo

转到该目录:

1
cd jhipster-simple-demo/

要生成您的应用程序,请输入:

1
jhipster

接下来根据你的需求来进行选择,比如,我需要创建一个最小化的单体应用,则选择如下:

  • 单体应用
  • 使用 Gatling 测试框架
  • 不使用 Spring WebFlux
  • 使用 HTTP Session Authentication 认证
  • 不使用数据库
  • 不使用缓存
  • 使用 Maven
  • 使用 OpenAPI-generator
  • 不生成前端客户端
  • 使用国际化

对应使用 maven 的项目,如下方式启动应用:

1
2
3
4
./mvnw

#或者
./mvnw spring-boot:run

不出意外的话,就可以看到项目顺利的跑起来了。可以在浏览器中输入 http://127.0.0.1:8080 查看应用的运行情况。 默认的登录用户名和密码都是 admin。

如果你选择了数据库或者缓存,则需要使用 docker 启动容器。

比如,启动 mysql:

1
docker compose -f src/main/docker/mysql.yml up -d

启动 redis:

1
docker compose -f src/main/docker/redis.yml up -d

Maven 插件

1、Maven 离线编译方法

参考文章 https://xdcsy.github.io/Text/Section0038.xhtml

1
./mvnw dependency:go-offline

2、jib-maven-plugin

jib-maven-plugin 是一个用于构建和推送 Docker 镜像的 Maven 插件。它可以简化将应用程序打包为 Docker 镜像并将其推送到容器注册表的过程。

相关示例,可以参考 https://github.com/GoogleContainerTools/jib/tree/master/examples

3、spring-boot-maven-plugin

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring-boot.version}</version>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>${start-class}</mainClass>
        <!--
        Enable the line below to have remote debugging of your application on port 5005
        <jvmArguments>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005</jvmArguments>
        -->
    </configuration>
</plugin>

运行命令:

1
./mvnw -Dspring-boot.run.jvmArguments="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000"

4、maven-javadoc-plugin

maven-javadoc-plugin 是一个用于生成 Java 代码的 Javadoc 文档的 Maven 插件。它可以根据代码中的注释自动生成详细的 API 文档,并支持定制化配置。

批处理模式下生成 Java 文档:

1
./mvnw  javadoc:javadoc -B

解释一下每个部分的含义:

  • ./mvnw:这是 Maven Wrapper 的脚本,它允许你在没有全局 Maven 安装的情况下运行 Maven 命令。./mvnw是用于 Unix/Linux 系统的脚本,如果你在 Windows 系统上运行,请使用mvnw(无前缀的点和斜杠)。
  • -ntp:这是--no-transfer-progress的缩写,它禁用传输进度的显示。这样做可以减少输出,使命令执行过程更加简洁。
  • javadoc:javadoc:这是 Maven 插件的目标,用于生成 Javadoc 文档。通过执行此目标,Maven 将处理项目源代码并生成相应的 Javadoc 文档。
  • --batch-mode:这是指定 Maven 在批处理模式下运行的选项。批处理模式禁用交互式模式,使得 Maven 命令不会要求用户输入。

5、maven-enforcer-plugin

maven-enforcer-plugin 是一个用于强制执行 Maven 构建规则的插件。它可以帮助团队确保项目的构建符合特定的要求和规范。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>3.4.1</version>
    <executions>
        <execution>
            <id>enforce-versions</id>
            <goals>
                <goal>enforce</goal>
            </goals>
        </execution>
        <execution>
            <id>enforce-dependencyConvergence</id>
            <configuration>
                <rules>
                    <DependencyConvergence/>
                </rules>
                <fail>false</fail>
            </configuration>
            <goals>
                <goal>enforce</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <rules>
            <requireMavenVersion>
                <message>You are running an older version of Maven. JHipster requires at least Maven 3.2.5</message>
                <version>[3.2.5,)</version>
            </requireMavenVersion>
            <requireJavaVersion>
                <message>You are running an incompatible version of Java. JHipster supports JDK 17 to 21.</message>
                <version>[17,18),[18,19),[19,20),[20,21),[21,22)</version>
            </requireJavaVersion>
        </rules>
    </configuration>
</plugin>

6、spotless-maven-plugin

spotless-maven-plugin 是一个用于在 Maven 构建过程中执行代码格式化的插件。它可以帮助团队保持一致的代码风格,并自动修复格式错误。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<plugin>
    <groupId>com.diffplug.spotless</groupId>
    <artifactId>spotless-maven-plugin</artifactId>
    <version>2.40.0</version>
    <configuration>
        <java>
            <!-- <removeUnusedImports/> -->
        </java>
    </configuration>
    <executions>
        <execution>
            <id>spotless</id>
            <phase>process-sources</phase>
            <goals>
                <goal>apply</goal>
            </goals>
        </execution>
    </executions>
</plugin>

7、modernizer-maven-plugin

modernizer-maven-plugin 是一个用于在 Maven 构建过程中执行代码现代化分析的插件。它可以帮助团队识别过时的代码和使用不推荐的 API,以便进行更新和修复。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<plugin>
    <groupId>org.gaul</groupId>
    <artifactId>modernizer-maven-plugin</artifactId>
    <version>2.7.0</version>
    <executions>
        <execution>
            <id>modernizer</id>
            <phase>package</phase>
            <goals>
                <goal>modernizer</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <javaVersion>17</javaVersion>
    </configuration>
</plugin>

8、sonar-maven-plugin

sonar-maven-plugin 是一个用于与 SonarQube 集成的 Maven 插件。SonarQube 是一个用于静态代码分析、代码质量管理和持续集成的开源平台。sonar-maven-plugin 可以将 Maven 项目的代码和分析结果上传到 SonarQube 服务器,并生成详细的代码质量报告。

1
2
3
4
5
<plugin>
      <groupId>org.sonarsource.scanner.maven</groupId>
      <artifactId>sonar-maven-plugin</artifactId>
      <version>3.10.0.2594</version>
  </plugin>

启动 docker 容器:

1
docker compose -f src/main/docker/sonar.yml up -d

运行:

1
./mvnw -Pprod clean verify sonar:sonar -Dsonar.login=admin -Dsonar.password=admin

再次运行:

1
./mvnw initialize sonar:sonar -Dsonar.login=admin -Dsonar.password=admin

Sonar 的配置文件 sonar-project.properties 是通过 maven 插件加载的。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>${properties-maven-plugin.version}</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>read-project-properties</goal>
            </goals>
            <configuration>
                <files>
                    <file>sonar-project.properties</file>
                </files>
            </configuration>
        </execution>
    </executions>
</plugin>

9、jacoco-maven-plugin

jacoco-maven-plugin 是一个用于在 Maven 项目中生成代码覆盖率报告的插件。它使用 JaCoCo(Java Code Coverage)工具来分析项目的测试覆盖率,并生成详细的报告,帮助开发团队评估测试的有效性和代码质量。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>${jacoco-maven-plugin.version}</version>
      <executions>
          <execution>
              <id>pre-unit-tests</id>
              <goals>
                  <goal>prepare-agent</goal>
              </goals>
          </execution>
          <execution>
              <!-- Ensures that the code coverage report for unit tests is created after unit tests have been run -->
              <id>post-unit-test</id>
              <phase>test</phase>
              <goals>
                  <goal>report</goal>
              </goals>
          </execution>
          <execution>
              <id>pre-integration-tests</id>
              <goals>
                  <goal>prepare-agent-integration</goal>
              </goals>
          </execution>
          <execution>
              <!-- Ensures that the code coverage report for integration tests is created after integration tests have been run -->
              <id>post-integration-tests</id>
              <phase>post-integration-test</phase>
              <goals>
                  <goal>report-integration</goal>
              </goals>
          </execution>
      </executions>
  </plugin>

10、openapi-generator-maven-plugin

用于根据 OpenAPI 规范(以前称为 Swagger 规范)生成客户端代码或服务器端框架。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<plugin>
    <!--
        Plugin that provides API-first development using openapi-generator-cli to
        generate Spring-MVC endpoint stubs at compile time from an OpenAPI definition file
    -->
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>${openapi-generator-maven-plugin.version}</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/swagger/api.yml</inputSpec>
                <generatorName>spring</generatorName>
                <apiPackage>com.mycompany.myapp.web.api</apiPackage>
                <modelPackage>com.mycompany.myapp.service.api.dto</modelPackage>
                <supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
                <skipValidateSpec>false</skipValidateSpec>
                <configOptions>
                    <delegatePattern>true</delegatePattern>
                    <title>jhipster-sample-application</title>
                    <useSpringBoot3>true</useSpringBoot3>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

11、frontend-maven-plugin

frontend-maven-plugin 是一个 Maven 插件,用于在构建过程中执行前端相关的任务,例如安装依赖、运行构建脚本、打包前端资源等。它可以将前端开发与后端构建过程无缝集成,简化了多模块项目的构建和部署。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <version>${frontend-maven-plugin.version}</version>
    <configuration>
        <installDirectory>target</installDirectory>
        <nodeVersion>${node.version}</nodeVersion>
        <npmVersion>${npm.version}</npmVersion>
    </configuration>
</plugin>

12、git-commit-id-maven-plugin

git-commit-id-maven-plugin 是一个 Maven 插件,用于从 Git 代码库中获取当前构建的 Git 提交信息,并将其注入到构建过程中,例如生成的类文件或资源文件中。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<plugin>
    <groupId>io.github.git-commit-id</groupId>
    <artifactId>git-commit-id-maven-plugin</artifactId>
    <version>${git-commit-id-maven-plugin.version}</version>
    <executions>
        <execution>
            <goals>
                <goal>revision</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <failOnNoGitDirectory>false</failOnNoGitDirectory>
        <failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
        <generateGitPropertiesFile>true</generateGitPropertiesFile>
        <includeOnlyProperties>
            <includeOnlyProperty>^git.commit.id.abbrev$</includeOnlyProperty>
            <includeOnlyProperty>^git.commit.id.describe$</includeOnlyProperty>
            <includeOnlyProperty>^git.branch$</includeOnlyProperty>
        </includeOnlyProperties>
    </configuration>
</plugin>

spring boot 加载 git.properties 文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
@Configuration
class GitInfoConfiguration {

  @Bean
  public PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
    PropertySourcesPlaceholderConfigurer propsConfig = new PropertySourcesPlaceholderConfigurer();
    propsConfig.setLocation(new ClassPathResource("git.properties"));
    propsConfig.setIgnoreResourceNotFound(true);
    propsConfig.setIgnoreUnresolvablePlaceholders(true);
    return propsConfig;
  }
}

13、maven-checkstyle-plugin

maven-checkstyle-plugin 是一个用于在 Maven 项目中执行代码风格检查的插件。它可以帮助团队在开发过程中维持一致的代码风格,并提供静态代码分析的功能。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>${maven-checkstyle-plugin.version}</version>
  <configuration>
    <configLocation>checkstyle.xml</configLocation>
    <includeTestSourceDirectory>true</includeTestSourceDirectory>
    <consoleOutput>true</consoleOutput>
    <failsOnError>true</failsOnError>
  </configuration>
  <executions>
    <execution>
      <id>validate</id>
      <phase>validate</phase>
      <goals>
        <goal>check</goal>
      </goals>
    </execution>
  </executions>
</plugin>

生成 site:

1
2
3
4
5
6
7
8
<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
        </plugin>
    </plugins>
</reporting>

13、maven-failsafe-plugin

maven-failsafe-plugin 是一个用于在 Maven 项目中运行集成测试的插件。它提供了一种便捷的方式来执行和验证项目中的集成测试。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>${failsafe-plugin.version}</version>
  <configuration>
    <!-- Due to spring-boot repackage, without adding this property test classes are not found
         See https://github.com/spring-projects/spring-boot/issues/6254 -->
    <classesDirectory>${project.build.outputDirectory}</classesDirectory>
    <!-- Force alphabetical order to have a reproducible build -->
    <runOrder>alphabetical</runOrder>
    <includes>
      <include>**/*IT*</include>
      <include>**/*CucumberTest*</include>
    </includes>
  </configuration>
  <executions>
    <execution>
      <id>integration-test</id>
      <goals>
        <goal>integration-test</goal>
      </goals>
    </execution>
    <execution>
      <id>verify</id>
      <goals>
        <goal>verify</goal>
      </goals>
    </execution>
  </executions>
</plugin>

14、maven-surefire-plugin

maven-surefire-plugin 是一个用于在 Maven 项目中运行单元测试的插件。它提供了执行和管理单元测试的功能,并生成测试报告。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>${surefire-plugin.version}</version>
  <configuration>
    <!-- Force alphabetical order to have a reproducible build -->
    <runOrder>alphabetical</runOrder>
    <excludes>
      <exclude>**/*IT*</exclude>
      <exclude>**/*CucumberTest*</exclude>
    </excludes>
  </configuration>
</plugin>

Docker

src/main/docker 中有一些常见应用的 docker-compoe 编排文件。这些文件有以下特点:

  • 有的提供了健康检查
  • 容器端口只映射到 127.0.0.1

mysql

mysql.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# This configuration is intended for development purpose, it's **your** responsibility to harden it for production
name: jhipstersampleapplication
services:
  mysql:
    image: mysql:8.1.0
    volumes:
      - ./config/mysql:/etc/mysql/conf.d
    #   - ~/volumes/jhipster/jhipsterSampleApplication/mysql/:/var/lib/mysql/
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
      - MYSQL_DATABASE=jhipstersampleapplication
    # If you want to expose these ports outside your dev PC,
    # remove the "127.0.0.1:" prefix
    ports:
      - 127.0.0.1:3306:3306
    command: mysqld --lower_case_table_names=1 --skip-ssl --character_set_server=utf8mb4 --explicit_defaults_for_timestamp
    healthcheck:
      test: ["CMD", "mysql", "-e", "SHOW DATABASES;"]
      interval: 5s
      timeout: 5s
      retries: 10

postgresql

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# This configuration is intended for development purpose, it's **your** responsibility to harden it for production
name: jhipstersampleapplication
services:
  postgresql:
    image: postgres:16.0
    # volumes:
    #   - ~/volumes/jhipster/jhipsterSampleApplication/postgresql/:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=jhipsterSampleApplication
      - POSTGRES_PASSWORD=
      - POSTGRES_HOST_AUTH_METHOD=trust
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER}"]
      interval: 5s
      timeout: 5s
      retries: 10
    # If you want to expose these ports outside your dev PC,
    # remove the "127.0.0.1:" prefix
    ports:
      - 127.0.0.1:5432:5432

redis

redis.yml

1
2
3
4
5
6
7
8
9
# This configuration is intended for development purpose, it's **your** responsibility to harden it for production
name: jhipstersampleapplication
services:
  redis:
    image: redis:7.2.1
    # If you want to expose these ports outside your dev PC,
    # remove the "127.0.0.1:" prefix
    ports:
      - 127.0.0.1:6379:6379

sonar

sonar.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# This configuration is intended for development purpose, it's **your** responsibility to harden it for production
name: jhipstersampleapplication
services:
  sonar:
    container_name: sonarqube
    image: sonarqube:10.2.1-community
    platform: linux/x86_64
    # Forced authentication redirect for UI is turned off for out of the box experience while trying out SonarQube
    # For real use cases delete SONAR_FORCEAUTHENTICATION variable or set SONAR_FORCEAUTHENTICATION=true
    environment:
      - SONAR_FORCEAUTHENTICATION=false
    # If you want to expose these ports outside your dev PC,
    # remove the "127.0.0.1:" prefix
    ports:
      - 127.0.0.1:9001:9000
      - 127.0.0.1:9000:9000

swagger-editor

1
2
3
4
5
6
7
# This configuration is intended for development purpose, it's **your** responsibility to harden it for production
name: jhipstersampleapplication
services:
  swagger-editor:
    image: swaggerapi/swagger-editor:latest
    ports:
      - 127.0.0.1:7742:8080