Dry goods|SpringBoot-Maven and Gradle multi-module construction

The more familiar model is the Springboot+Maven multi-module organization method. Since Gradle has been very powerful recently, according to the data of the five pressure test scenarios given by Gradle's official website, Gradle's processing performance is indeed faster than Maven, so let's experience it. Let's see how Gradle does multi-module construction and the introduction and viewing of various dependencies.

If you are interested in the comparison between Maven and Gradle provided by Gradle official website, you can click: https://gradle.org/maven-vs-gradle/
insert image description here

Material:

  • SpringBoot 2.3.12.RELEASE
  • IDEA
  • Maven 3.6.0
  • Gadle 7.4

Quick View:

1. Maven multi-module in IDEA

insert image description here
Official website direct hit: https://maven.apache.org/

General construction process

  • Create a new project, you can use Spring Initializer to initialize, choose maven project
  • Delete the src directory, leaving only the external pom.xml
  • Right-click the parent project, New->Module->Maven project, fill in the project name to generate a maven sub-project
  • In the parent project, pom.xml automatically writes the submodule name, and the submodule pom.xml automatically writes the parent project name
  • Unified definition of versions and unified declaration of dependencies can be done in the parent project

Maven General Instructions

General Packing Directives

#跳过单测打包
mvn clean package -Dmaven.test.skip=true
#跳过单测打包,并把打好的包上传到本地仓库
mvn clean install -Dmaven.test.skip=true
#跳过单测打包,并把打好的包上传到远程仓库
mvn clean deploy -Dmaven.test.skip=true

other instructions

#查看版本 
mvn -v 

#创建 Maven 项目 
mvn archetype:create 

#编译源代码 
mvn compile

#编译测试代码 
mvn test-compile 

#运行应用程序中的单元测试 
mvn test 

#生成项目相关信息的网站 
mvn site 

#依据项目生成 jar 文件 
mvn package 

#在本地 Repository 中安装 jar
mvn install  

#忽略测试文档编译
mvn -Dmaven.test.skip=true  

#清除目标目录中的生成结果
mvn clean  

#将.java类编译为.class文件
mvn clean compile  

#进行打包
mvn clean package  

#执行单元测试
mvn clean test 

#部署到版本仓库 
mvn clean deploy 

#使其他项目使用这个jar,会安装到maven本地仓库中 
mvn clean install 

#创建项目架构
mvn archetype:generate  

#查看已解析依赖 
mvn dependency:list 

#【*常用*】看到依赖树
mvn dependency:tree com.xx.xxx  

#查看依赖的工具 
mvn dependency:analyze 

#从中央仓库下载文件至本地仓库
mvn help:system  

#查看当前激活的profiles 
mvn help:active-profiles 

#查看所有profiles 
mvn help:all-profiles 

#查看完整的pom信息
mvn help:effective -pom 

2. Gradle multi-module in IDEA

insert image description here
Official website direct hit: https://gradle.org/

build process

The basic process is the same. Compared with the Maven configuration, there is a subprojects configuration in the gradle parent project, which needs to be adjusted manually.

  • Create a new project, you can use [Spring Initializer] to initialize, select the gradle project
  • Delete the src directory and keep the external [build.gradle/gradlew/gradlew.bat/setting.gradle]
  • The gradle directory will be automatically created under the top-level directory of the project, [gradle->wrapper->gradle-wrapper.jar/gradle-wrapper.preperties], if it is a self-built project, you need to pay attention to the introduction (here the project maven project will have more files )
  • Right-click the parent project, [New->Module->Gradle] project, fill in the project name to generate a gradle sub-project. The submodule has only one [build.gradle] file
  • The submodule name [include "submodule"] is automatically written in the parent project setting.gradle. Unlike maven, the inclusion relationship of the submodule is written in another file. There is no trace of the parent project in the submodule.
  • In the parent project, you can make a unified definition of the version [ext], and a unified declaration of dependencies in subprojects{dependencies{} dependencyManagement{}}
  • The difference between the gradle project is that the dependencies of the submodules need to be written separately in the parent project
plugins {
    
    
    id 'org.springframework.boot' version '2.3.12.RELEASE'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.learning'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    
    
    compileOnly {
    
    
        extendsFrom annotationProcessor
    }
}

repositories {
    
    
    mavenCentral()
}

ext {
    
    
    set('springCloudVersion', "Hoxton.SR12")
}

subprojects {
    
    

    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'  //引入外部定义的插件
    apply plugin: 'io.spring.dependency-management' //引入外部定义的插件

    ext {
    
    
        springVersion = "2.3.12.RELEASE" //统一定义版本,后面引用
        compileJava.options.encoding = 'UTF-8'
        compileTestJava.options.encoding = 'UTF-8'
        springCloudVersion = "Hoxton.SR12"
    }

    sourceCompatibility = 1.11
    targetCompatibility = 1.11

    tasks.withType(JavaCompile) {
    
    
        options.encoding = 'UTF-8'
    }

    //配置依赖
    dependencies {
    
    
        implementation 'org.springframework.boot:spring-boot-starter' //一次性定义子项目包含的依赖
        implementation 'com.alibaba:fastjson:2.0.33' //指定版本
        compileOnly 'org.projectlombok:lombok'
        annotationProcessor 'org.projectlombok:lombok'
        testImplementation('org.springframework.boot:spring-boot-starter-test') {
    
    
            exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        }
    }

    dependencyManagement {
    
    
        imports {
    
    
            mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
        }
    }

    tasks.named('test') {
    
    
        useJUnitPlatform()
    }
}

Gradle Common Commands

  • Gradle is a configuration language developed by Google based on the groovy language, which is used to replace the ant build
  • Projects built based on gradle have a gradle local agent called gradle wrapper, which is the content in the gradle subdirectory under the project root directory in the previous steps
# 查看构建版本
./gradlew -v
# 清除build文件夹
./gradlew clean
a@b:~/files/gitFile/springcloudall$ ./gradlew  clean

BUILD SUCCESSFUL in 827ms
3 actionable tasks: 2 executed, 1 up-to-date

# 检查依赖并编译打包
./gradlew build

./gradlew dependencies

a@b:~/files/gitFile/springcloudall/eurekaclient$ ../gradlew dependencies

> Task :eurekaclient:dependencies

------------------------------------------------------------
Project ':eurekaclient'
------------------------------------------------------------

annotationProcessor - Annotation processors and their dependencies for source set 'main'.
\--- org.projectlombok:lombok -> 1.18.20

apiElements - API elements for main. (n)
No dependencies

archives - Configuration for archive artifacts. (n)
No dependencies

bootArchives - Configuration for Spring Boot archive artifacts.
No dependencies

compileClasspath - Compile classpath for source set 'main'.
+--- org.projectlombok:lombok -> 1.18.20
+--- org.springframework.boot:spring-boot-starter -> 2.3.12.RELEASE
|    +--- org.springframework.boot:spring-boot:2.3.12.RELEASE
|    |    +--- org.springframework:spring-core:5.2.15.RELEASE
|    |    |    \--- org.springframework:spring-jcl:5.2.15.RELEASE
|    |    \--- org.springframework:spring-context:5.2.15.RELEASE
|    |         +--- org.springframework:spring-aop:5.2.15.RELEASE
|    |         |    +--- org.springframework:spring-beans:5.2.15.RELEASE
|    |         |    |    \--- org.springframework:spring-core:5.2.15.RELEASE (*)
|    |         |    \--- org.springframework:spring-core:5.2.15.RELEASE (*)
|    |         +--- org.springframework:spring-beans:5.2.15.RELEASE (*)
|    |         +--- org.springframework:spring-core:5.2.15.RELEASE (*)
|    |         \--- org.springframework:spring-expression:5.2.15.RELEASE
|    |              \--- org.springframework:spring-core:5.2.15.RELEASE (*)
|    +--- org.springframework.boot:spring-boot-autoconfigure:2.3.12.RELEASE
|    |    \--- org.springframework.boot:spring-boot:2.3.12.RELEASE (*)
|    +--- org.springframework.boot:spring-boot-starter-logging:2.3.12.RELEASE
|    |    +--- ch.qos.logback:logback-classic:1.2.3
|    |    |    +--- ch.qos.logback:logback-core:1.2.3
|    |    |    \--- org.slf4j:slf4j-api:1.7.25 -> 1.7.30
|    |    +--- org.apache.logging.log4j:log4j-to-slf4j:2.13.3
|    |    |    +--- org.slf4j:slf4j-api:1.7.25 -> 1.7.30
|    |    |    \--- org.apache.logging.log4j:log4j-api:2.13.3
|    |    \--- org.slf4j:jul-to-slf4j:1.7.30
|    |         \--- org.slf4j:slf4j-api:1.7.30
|    +--- jakarta.annotation:jakarta.annotation-api:1.3.5
|    +--- org.springframework:spring-core:5.2.15.RELEASE (*)

# 或者模组的 依赖
./gradlew app:dependencies
# 检索依赖库
./gradlew app:dependencies | grep CompileClasspath
# windows 没有 grep 命令
./gradlew app:dependencies | findstr "CompileClasspath"

Guess you like

Origin blog.csdn.net/c_zyer/article/details/131124066