这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
本文是一篇笔记,通过实战将Gradle构建多模块的步骤记录下来,为今后新建的项目准备一个脚手架,该脚手架由两个子模块组成:二方库、SpringBoot应用,本文由以下内容组成:
名称 | 链接 | 备注 |
---|---|---|
项目主页 | 该项目在GitHub上的主页 | |
git仓库地址(https) | 该项目源码的仓库地址,https协议 | |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
// gradle自身会用到的相关设置
buildscript {
// 仓库
repositories {
// 本地
mavenLocal()
// 阿里云
maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/'
}
// 中央仓库
mavenCentral()
// grandle插件
maven {
url 'https://plugins.gradle.org/m2/'
}
}
// 子模块会用到的变量
ext {
springBootVersion = '2.4.4'
}
}
// 插件
plugins {
id 'java'
id 'java-library'
// 有这个声明,子模块可以使用org.springframework.boot插件而无需指定版本,但是apply=false表示当前模块不使用此插件
id 'org.springframework.boot' version "${springBootVersion}" apply false
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}
// gradle wrapper指定版本
wrapper {
gradleVersion = '6.8.3'
}
// 取当前时间
def buildTimeAndDate = OffsetDateTime.now()
// 根据时间生成字符串变量
ext {
projectVersion = project.version
buildDate = DateTimeFormatter.ISO_LOCAL_DATE.format(buildTimeAndDate)
buildTime = DateTimeFormatter.ofPattern('HH:mm:ss.SSSZ').format(buildTimeAndDate)
}
// 针对所有project的配置,包含根项目
allprojects {
group 'com.bolingcavalry'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'io.spring.dependency-management'
// 编译相关参数
compileJava {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
options.encoding = 'UTF-8'
options.compilerArgs = [
'-Xlint:all', '-Xlint:-processing'
]
}
// Copy LICENSE
tasks.withType(Jar) {
from(project.rootDir) {
include 'LICENSE'
into 'META-INF'
}
}
// 生成jar文件时,MANIFEST.MF的内容如下
jar {
manifest {
attributes(
'Created-By': "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})".toString(),
'Built-By': 'travis',
'Build-Date': buildDate,
'Build-Time': buildTime,
'Built-OS': "${System.properties['os.name']}",
'Specification-Title': project.name,
'Specification-Version': project.version,
'Specification-Vendor': 'Will Zhao',
'Implementation-Title': project.name,
'Implementation-Version': project.version,
'Implementation-Vendor': 'Will Zhao'
)
}
}
// 仓库
repositories {
// 本地
mavenLocal()
// 如果有私服就在此配置,如果没有请注释掉
maven {
url 'http://192.168.50.43:8081/repository/aliyun-proxy/'
}
// 阿里云
maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/'
}
// 中央仓库
mavenCentral()
// grandle插件
maven {
url "https://plugins.gradle.org/m2/"
}
}
}
// 类似maven的dependencyManagement,这里将所有jar的版本指定好,子模块在依赖时可以不用指定版本
allprojects { project ->
buildscript {
dependencyManagement {
imports {
mavenBom "org.springframework.boot:spring-boot-starter-parent:${springBootVersion}"
mavenBom "org.junit:junit-bom:5.7.0"
}
dependencies {
dependency 'org.projectlombok:lombok:1.16.16'
dependency 'org.apache.commons:commons-lang3:3.11'
dependency 'commons-collections:commons-collections:3.2.2'
dependency 'net.devh:grpc-server-spring-boot-starter:2.11.0.RELEASE'
}
}
}
}
// 构建二方库的插件
plugins {
id 'java-library'
}
// 这里可以指定构建出来的jar的文件名
archivesBaseName = 'demo-client'
// 子模块自己的依赖
dependencies {
// 使用api,将依赖传递给使用democlient的模块
api 'org.projectlombok:lombok'
// annotationProcessor不会传递,使用了lombok生成代码的模块,需要自己声明annotationProcessor
annotationProcessor 'org.projectlombok:lombok'
}
package com.bolingcavalry.dto;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
@Builder
public class Student {
String name;
int age;
}
接下来要创建的是一个SpringBoot应用,并且使用了democlient模块中的Student类;
plugins {
id 'org.springframework.boot'
}
// 用了插件org.springframework.boot之后,jar task会失效,可用bootJar取代
bootJar {
archiveBaseName = project.name
archiveVersion = project.version
manifest {
attributes(
'Created-By': "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})".toString(),
'Built-By': 'travis',
'Build-Date': buildDate,
'Build-Time': buildTime,
'Built-OS': "${System.properties['os.name']}",
'Specification-Title': project.name,
'Specification-Version': projectVersion,
'Specification-Vendor': 'Will Zhao',
'Implementation-Title': project.name,
'Implementation-Version': projectVersion,
'Implementation-Vendor': 'Will Zhao'
)
}
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
// 子模块自己的依赖
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
// 二方库依赖
implementation project(':democlient')
// annotationProcessor不会传递,使用了lombok生成代码的模块,需要自己声明annotationProcessor
annotationProcessor 'org.projectlombok:lombok'
}
test {
useJUnitPlatform()
}
package com.bolingcavalry;
import com.bolingcavalry.dto.Student;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@SpringBootApplication
@RestController
public class DemoApplication {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
return "Hello "
+ new Date()
+ "--"
+ new Student("Tom", 11);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。