使用Gradle进行项目构建管理
使用Gradle进行项目构建管理
安装配置Gradle
Gradle的安装与配置
创建项目
使用gradle init命令创建root项目
E:\code\myself\china-unicorn>gradle init Starting a Gradle Daemon, 1 busy Daemon could not be reused, use --status for detailsSelect type of project to generate:1: basic2: application3: library4: Gradle plugin Enter selection (default: basic) [1..4] 1Select build script DSL:1: Groovy2: Kotlin Enter selection (default: Groovy) [1..2] 1Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]Project name (default: china-unicorn):> Task :init Get more help with your project: Learn more about Gradle by exploring our samples at https://docs.gradle.org/7.5.1/samplesBUILD SUCCESSFUL in 4m 2 actionable tasks: 2 executed E:\code\myself\china-unicorn>
修改
gradle-wrapper.properties
,配置本地distributionUrl。distributionUrl=file:///E:/gradle/gradle-8.0.2-all.zip
在根目录下增加root级别的
build.gradle
文件// 这是根目录build.gradle // 应用于所有的project allprojects {apply plugin: 'java'apply plugin: 'maven-publish'sourceCompatibility = '1.8'group 'org.donny.bigdata'version '1.0.0-SNAPSHOT'/* java编译的时候缺省状态下会因为中文字符而失败 */[compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'buildscript {repositories {maven {url 'https://maven.aliyun.com/repository/public'}}} }// 所有子项目的通用配置 subprojects {//版本定义ext {log4jVersion = '2.20.0'sparkVersion = '2.3.3'scalaMajorMinorVersion = '2.11'// 打包时使用的预发布路径preDestinationDirectory = "$rootProject.buildDir\\preDist\\$rootProject.name-$rootProject.version"}// 添加通用依赖dependencies {testImplementation group: 'junit', name: 'junit', version: '4.12'testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'}// 仓库配置repositories {mavenLocal()def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'all {ArtifactRepository repo ->if (repo instanceof MavenArtifactRepository) {def url = repo.url.toString()if (url.startsWith('https://artifacts.elastic.co/maven')|| url.startsWith('https://repo1.maven.org/maven2')|| url.startsWith('https://repo.maven.org/maven2')|| url.startsWith('https://jcenter.bintray.com/')|| url.startsWith('https://repo.maven.apache.org/maven2')) {project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."remove repo}}}maven {url REPOSITORY_URL}} }
用idea,打开项目
图形化创建子模块server和子模块spark-on-yarn-monitor。
子模块可以使用父模块定义的变量,在ext中定义版本和一些子模块需要使用的变量
// 这是根目录build.gradle // 应用于所有的project allprojects {apply plugin: 'java'apply plugin: 'maven-publish'sourceCompatibility = '1.8'group 'org.donny.bigdata'version '1.0.0-SNAPSHOT'/* java编译的时候缺省状态下会因为中文字符而失败 */[compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'buildscript {repositories {maven {url 'https://maven.aliyun.com/repository/public'}}} }// 所有子项目的通用配置 subprojects {//版本定义ext {log4jVersion = '2.20.0'sparkVersion = '2.3.3'scalaMajorMinorVersion = '2.11'// 打包时使用的预发布路径preDestinationDirectory = "$rootProject.buildDir\\preDist\\$rootProject.name-$rootProject.version"}// 添加通用依赖dependencies {testImplementation group: 'junit', name: 'junit', version: '4.12'testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'}// 仓库配置repositories {mavenLocal()def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'all {ArtifactRepository repo ->if (repo instanceof MavenArtifactRepository) {def url = repo.url.toString()if (url.startsWith('https://artifacts.elastic.co/maven')|| url.startsWith('https://repo1.maven.org/maven2')|| url.startsWith('https://repo.maven.org/maven2')|| url.startsWith('https://jcenter.bintray.com/')|| url.startsWith('https://repo.maven.apache.org/maven2')) {project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."remove repo}}}maven {url REPOSITORY_URL}} }
server子模块配置如下,可以看到子模块可以使用父模块定义的变量
import org.gradle.util.GradleVersionarchivesBaseName = "Chineseunicorn"ext.mailVersion = '2.0.1' ext.nettyVersion = '4.1.90.Final'dependencies {// 日志依赖implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: "${log4jVersion}"implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: "${log4jVersion}"// jakarta邮件依赖implementation "com.sun.mail:jakarta.mail:${mailVersion}"implementation group: 'jakarta.mail', name: 'jakarta.mail-api', version: "${mailVersion}"// nettyimplementation group: 'io.netty', name: 'netty-all', version: "${nettyVersion}" }test {useJUnitPlatform() }
11
改写父子模块的jar任务
root模块,依赖子模块的jar关系
jar {dependsOn clean// 依赖子模块spark-on-yarn-monitor的jar任务dependsOn ':spark-on-yarn-monitor:jar'// 依赖子模块server的jar任务dependsOn ':server:jar' }
server子模块
- 将运行时依赖的jar打入一个单独的lib文件夹
- 依赖其他子模块的任务
dependsOn ':spark-on-yarn-monitor:jar'
- manifest文件编写
// 重写jar jar {dependsOn ':spark-on-yarn-monitor:jar'dependsOn 'clearPreDist'dependsOn 'copyBin'dependsOn 'copyJar'dependsOn 'copyConf'from sourceSets.main.outputdependsOn configurations.runtimeClasspathString someString = ''configurations.runtimeClasspath.each { someString = someString + " lib//" + it.name }manifest {attributes 'Implementation-Title': archivesBaseNameattributes 'Implementation-Vendor-Id': 'com.donny'attributes 'Implementation-Version': archiveVersionattributes 'Main-Class': 'com.donny.bigdata.surveillance.ChineseUnicorn'attributes 'Created-By': 'Gradle ' + GradleVersion.current()attributes 'Build-Jdk': JavaVersion.current()attributes 'Class-Path': someString}// 控制jar的输出位置destinationDirectory = file("${preDestinationDirectory}")// 开启zip64压缩输出zip64 true } // 打包 task clearPreDist(type: Delete) {delete "${preDestinationDirectory}" } // 将依赖包复制到lib目录 task copyJar(type: Copy) {into "${preDestinationDirectory}" + "\\lib"from configurations.runtimeClasspath } // 将配置文件复制到conf目录 task copyConf(type: Copy) {into "${preDestinationDirectory}" + "\\conf"from "$rootProject.rootDir" + "\\conf" } // 将脚本复制到bin目录 task copyBin(type: Copy) {into "${preDestinationDirectory}" + "\\bin"from "$rootProject.rootDir" + "\\bin" }
spark-on-yarn-monitor子模块
// 改写 jar {// 将所有implementation依赖一起打包到jar,需要对冲突定义规则,我这里采用DuplicatesStrategy.EXCLUDE策略configurations.implementation.canBeResolved = trueduplicatesStrategy = DuplicatesStrategy.EXCLUDEfrom { configurations.implementation.collect { it.isDirectory() ? it : zipTree(it) } }manifest {attributes 'Main-Class': 'com.donny.bigdata.surveillance.spark.SparkOnYarnMonitor'}// 开启zip64压缩输出zip64 true// 控制jar的输出位置-父工程的plugins文件夹中destinationDirectory = file("${preDestinationDirectory}" + "\\plugins") }
打包成zip或者tar
root模块的配置增加任务,最终发布包在文件夹distributions下。
task Zip(type: Zip) {dependsOn jar//要压缩的文件夹from("$buildDir\\preDist\\") }task buildTar(type: Tar) {dependsOn jarcompression = Compression.GZIParchiveExtension = "tar.gz"//要压缩的文件夹from("$buildDir\\preDist\\") }
使用Gradle进行项目构建管理相关推荐
- maven项目构建管理工具一站式全部搞定
内容目录 一.maven的概述 二.maven环境搭建 三.maven常用命令 四.maven的坐标和仓库 五.创建maven项目 六.创建maven web项目 概述: maven是基于项目对象模型 ...
- 如何在Gradle多项目构建中管理依赖项
从很早以前我就一直喜欢Gradle构建工具. 它的潜力甚至在1.0版本之前就已经很明显了,那时变化经常被打破. 如今,升级很少会引起意外. 该工具已经成熟并且运行良好. Gradle包括一个功能强大的 ...
- Gradle多项目构建–类似父pom的结构
当您来自Maven背景时,很可能已经习惯了父pom结构. 现在,在进行gradle时,情况有所不同. 想象一下有一个包含接口和各种其他实现的项目的场景. 这将是我们的项目结构. multi-proje ...
- Java项目构建管理Maven的安装与配置
1.下载安装 首先安装配置好jdk[指路:jdk的安装和配置],然后下载安装maven(现在多数使用maven3,版本号3.x.x,可以去官网下载https://maven.apache.org/do ...
- 《Gradle权威指南》--Android Gradle多项目构建
No1: Android多项目设置 目录结构: MyProject/ setting.gradle app/ build.gradle libraries/ lib1/ build.gradle li ...
- gradle文件利用java解析_使用文件读取Gradle Multi项目构建
嗨,我有2个项目的文件如下: project1 \- build.gradle project2 \- build.gradle \- build.properties project1: build ...
- 暴力突破 Gradle 自动化项目构建(八)- Android 对 Gradle 的扩展:Variants、Transform
一.前言 上一篇学习了如何自定义 Gradle 插件,本篇我们来学习下 Android 对 Gradle 的扩展:Variants(变体)以及 Transform.通过扩展可以让我们在自定义 Grad ...
- gradle普通项目构建外部依赖jar的终极方法gradle瘦身
示例build.gradle如下 加载的包可以随意主要是下面的部分 plugins {id 'maven-publish' }dependencies {testCompile group: 'jun ...
- gradle的多项目构建(九)
目录 1. 概念 2. settings 3. task 4. 属性继承 1. 概念 在Android Studio中,一个模块和一个项目是有区别的.一个项目包含多个模块module.而Android ...
最新文章
- bootstrap的使用
- c语言的0变成奇怪的图形,Word打开后图片中的文字变成了奇怪的符号
- 客户资产管理(Custom Asset Management)
- C++中的const数据成员
- Asp.Net Core安全防护-客户端IP白名单限制
- 微信公众嵌套页面里再嵌入其他页面的一些问题
- SpringBoot+Vue 完整的外卖系统,手机端和后台管理
- 不写代码,带你徒手开发一个健康打卡应用
- 我的内核学习笔记:环境
- java eml解析_javamail 收邮件 解析eml文件
- centos7 如何安装部署k8s_架构运维篇(五):Centos7/Linux中安装部署RocketMQ
- VS Code 如何运行.html文件(VS Code 如何安装插件)(open with live serve和open in default browser)
- batch script learn
- php高德行政区边界,请教 高德地图绘制行政区划边界
- dvwa页面打不开的原因_细看网页打不开的解决办法以及原因细看
- java中laber字体颜色设置,重写jxl中可用的颜色实现自定义颜色
- sharing-jdbc分库分表方案设计
- OVP保护芯片首选ETA7008,耐压36V,过压保护点可调
- 嘿嘿各位兄弟姐妹们,赚钱是一个男人最大的底气和尊严!
- Hbuildx打包ios自定义基座