首先将项目关系添加到根项目的settings.gradle文件中,代码如下:

rootProject.name = 'multiproject'
include 'api'
include 'core'
include 'login'
在根项目的build.gradle文件中添加常用配置项,代码如下:
// 定义了group
group 'com.bill'
version '1.0-SNAPSHOT'
// 为所有工程定义公共配置
allprojects {
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
// 设置 JDK 版本
sourceCompatibility = 1.8
targetCompatibility = 1.8
// 设置编译使用utf-8编码
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
tasks.withType(Javadoc) {
options.encoding = 'UTF-8'
}
tasks.withType(Test) {
systemProperty "file.encoding", "UTF-8"
}
// 使用远程仓库,
repositories {
mavenCentral()
}
// 使用了自定义的仓库,比如公司的nexus私服
//repositories {
// maven {
// url "http://192.168.99.90:8081/nexus/content/groups/public/"
// }
//}
// 定义了公共的依赖库
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
}
login子项目中的build.gradle配置,该登录项目是spring boot项目,其代码配置如下:
// buildscript { } 块来使类添加到编译脚本的classpath中
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
// 使用spring boot插件
apply plugin: 'org.springframework.boot'
// 自定义springCloudVersion版本属性
ext {
springCloudVersion = 'Dalston.SR4'
}
// 添加login子工程的依赖库
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("mysql:mysql-connector-java:5.1.38")
compile("commons-codec:commons-codec:1.10")
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
// 定义manifest
jar {
manifest {
attributes 'Implementation-Title': '登陆服务器', 'Implementation-Version': rootProject.version
attributes 'Main-Class': 'com.bill.LoginApplication'
}
}
要使用buildscript代码块,请查看“第6课Gradle自定义任务实现”. 在jar任务中,我们使用了像rootProject.version这样的配置,因此其配置取决于根项目的version属性.
最后,我们在根项目中执行gradle build命令,我们将看到api,core和login子项目都已编译到jar包中,如下图所示:
至此,我们的编译已完成,那么如何将jar文件发布到指定位置?
我们的存档文件(例如jar包)可以在本地发布或上传到maven.
首先,发布到本地目录,这可以通过在根目录中添加以下配置项来实现:
// 为所有的子工程添加uploadArchives任务
subprojects{
uploadArchives {
repositories {
flatDir {
dirs rootDir.absolutePath + "/release_dir"
}
}
}
}
执行$ gradle uploadArchives命令后gradle实战,将jar包释放到项目根目录下的release_dir子目录中,如下所示:
然后,使用maven-publish插件发布到maven私有服务器. 如果我们的jar希望与其他项目共享,则可以将jar包上传到其他项目的公共Maven存储库. 具体代码如下: <
apply plugin: 'maven-publish'
publishing {
publications {
api(MavenPublication) {
// 定义maven中的坐标,groupId、artifactId和version的值
groupId 'com.game.game-service-api'
artifactId 'game-service-api'
version _version
from components.java
}
}
}
publishing {
// 定义maven私服地址和帐号密码
repositories {
maven {
url "http://192.168.99.90:8081/nexus/content/repositories/thirdparty/"
credentials {
username 'admin'
password 'admin123'
}
}
}
}
在本章中,我们完成了Gradle项目的编译和打包以及发布功能,并通过实践练习回顾了以前的基本理论知识. 在下一章中,我们将继续介绍Gradle和Docker的结合使用. 如果您想了解更多,可以关注我的新书“分布式服务体系结构: 原理,设计和实际战斗”. 有很多共同的工作经验可供分享.
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/ruanjian/article-205630-2.html
正解