需要准备
- 一个熟悉的 IDE 开发工具
- JDK 7及以上
- Gradle 3.2以上
项目的前期准备
1.新建工程
首先我们创建根工程为multiproject
的项目,该项目包含三个子工程,分别为:api、core 和 login。其结构如下:
multiproject/
build.gradle
settings.gradle
api/
core/
login/
Gradle 中多工程的详细介绍,请复习“第10课 Gradle 实现多工程的编译打包”的内容,接下来我将使用 IntelliJ IDEA 工具一步一步的带着大家完成多工程的创建。
首先打开 IDEA 工具,选择新建工程,如下图:
enter image description here
然后选择 Gradle 项,如下图:
enter image description here
在这里输入我们的 GroupId 和 ArtifactId,其中 ArtifactId 会默认作为我们工程的名称,如下图:
enter image description here
这一步保持默认值,继续下一步:
enter image description here
在这里可以更改工程名称,指定工程的位置,如下图:
enter image description here
点击 Finish 完成创建,现在的工程结果如下:
enter image description here
我们再继续创建子工程,在 IDEA 中对应的是 Module,具体操作参照以下四图:
enter image description here
enter image description here
enter image description here
enter image description here
创建好 api 子工程的结构如下图:
enter image description here
其它子工程创建步聚同 api 一样,只是 login 工程我们使用的是 spring boot 实现的一个简单的登陆项目,具体如何使用 spring boot 请自行学习,不在本次的内容范围之中,最终的目录结构如下:
enter image description here
同时在子项目 api、core 和 login 工程下创建了 Java 插件约定的目录结构(具体可复习“第12课 Gradle 中常用插件的介绍-下”的内容),具体结构如下:
src/
main/
java/ # java源文件
resources/ # 项目资源文件
test/
java/ # 测试源文件
resources/ # 测试资源文件
2.常用命令
- gradle projects:列举当前目录下的所有项目,代码示例如下:
$ gradle projects
:projects
------------------------------------------------------------
Root project
------------------------------------------------------------
Root project 'multiproject'
+--- Project ':api'
+--- Project ':core'
\--- Project ':login'
To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :api:tasks
BUILD SUCCESSFUL
- gradle tasks:列举当前目录下的所有任务,代码示例如下:
$ gradle tasks
:tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Application tasks
-----------------
bootRun - Run the project with support for auto-detecting main class and reloading static resources
Build tasks
-----------
assemble - Assembles the outputs of this project.
bootRepackage - Repackage existing JAR and WAR archives so that they can be executed from the command line using 'java -jar'
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.
...省略....
- gradle properties:列举当前目录下的所有的属性,代码示例如下:
$ gradle properties
:properties
------------------------------------------------------------
Root project
------------------------------------------------------------
allprojects: [root project 'multiproject', project ':api', project ':core', project ':login']
ant: org.gradle.api.internal.project.DefaultAntBuilder@3796aca5
antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@42ca9b32
archivesBaseName: multiproject
artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@33097a0a
asDynamicObject: DynamicObject for root project 'multiproject'
assemble: task ':assemble'
...省略....
- gradle help:显示帮助信息,示例代码如下:
$ gradle help
:help
Welcome to Gradle 3.2.
To run a build, run gradle <task> ...
To see a list of available tasks, run gradle tasks
To see a list of command-line options, run gradle --help
To see more detail about a task, run gradle help --task <task>
BUILD SUCCESSFUL
- gradle dependencies:举出项目中的依赖信息,:
$ gradle dependencies
:dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
...省略....
testCompile - Dependencies for source set 'test'.
\--- junit:junit:4.12
\--- org.hamcrest:hamcrest-core:1.3
testCompileClasspath - Compile classpath for source set 'test'.
\--- junit:junit:4.12
\--- org.hamcrest:hamcrest-core:1.3
testCompileOnly - Compile dependencies for source set 'test'.
\--- junit:junit:4.12
\--- org.hamcrest:hamcrest-core:1.3
...省略....
3.依赖管理
我们通常会在项目中添加一些第三方的依赖库,具体的添加方法请复习“第03课 Gradle 基础脚本的实现”的内容,这里主要介绍下具体的几种依赖配置,如下表:
依赖范围 | 描述 |
---|---|
compile | 该依赖是为编译项目的源码 |
runtime | 该依赖为项目运行时的classes,默认情况下也是为了编译 |
testCompile | 该依赖是为编译项目测试的源码,默认情况下包括compile和runtime |
testRuntime | 该依赖是为执行测试时的用例。默认情况下包括compile、runtime和testCompile |
示例代码如下:
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')
}
4.自定义属性
http://www.cnblogs.com/davenkin/p/gradle-learning-5.html
我们在介绍使用 Java 插件时,介绍了很多插件会向工程中添加额外的属性,而在 Gradle 中设置和读取工程的属性是非常重要的一个功能,我们在 Gradle 和 Jenkins 结合时还会具体用到,接下来我将简单介绍下几种常用的自定义属性的方法。
首先,Gradle 在默认情况下已经为工程定义了很多属性的,我们可以使用之前介绍的gradle properties
命令来查看,其中比较常用的有以下几个属性:
- project:工程本身
- name:工程的名字
- path:工程的绝对路径
- description:工程的描述信息
- buildDir:工程构建结果存放目录
- version:工程的版本号
两种常用的自定义属性的方法:
- 在 build.gradle 文件中定义属性
- 通过命令行参数定义属性
- 通过 JVM 系统参数定义属性
示例代码如下:
// 第一种 在build.gradle文件中定义属性
ext.gitbranch = "develop"
// 或者使用闭包方法
ext {
gitbranch = "develop"
}
// 第二种 通过命令行参数定义属性
task customProperties << {
println gitbranch
}
gradle -Pgitbranch="develop" customProperties
// 第三种 通过JVM系统参数定义属性
// 我们可以通过-D参数定义JVM的系统参数,
// 然后在代码中可以可以通过System.getProperty()进行获取
task customProperties << {
println System.properties['gitbranch']
}
gradle -Dgitbranch="develop" customProperties
通过上面工程创建、常用命令、依赖配置和自定义属性等的介绍,我们已经做好了项目构建的前期工作,接下来让我们开始完成我们的项目构建实战部分内容吧。
项目构建
1.在根工程添加公共配置项
首先在根工程的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'
}
}
2.在子工程中实现特殊的配置
login 子工程中的build.gradle
配置,login 工程为 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 代码块的使用,请复习“第06课 Gradle 中自定义任务的实现”的内容。 在 jar 任务中,我们使用了rootProject.version
这样的配置,所以它的配置依赖了根工程的 version 属性。
最后我们在根工程中执行命令gradle build
,会看到 api、core 和 login 子工程都被编译好 jar 包了,如下图所示:
enter image description here
到这里我们的编译工作就完成了,那么接下来如何把我们的 jar 文件发布到指定的位置呢?
3.发布文件
我们的归档文件,比如 jar 包,可以发布到本地,也可以上传到 maven 中。
首先,发布到本地目录,可以通过在根目录下添加如下配置项实现:
// 为所有的子工程添加uploadArchives任务
subprojects{
uploadArchives {
repositories {
flatDir {
dirs rootDir.absolutePath + "/release_dir"
}
}
}
}
执行命令$ gradle uploadArchives
后,在工程的根目下的 release_dir 子目录中发布了 jar 包,如下图:
enter image description here
然后,使用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.jianshu.com/p/98afc49ece75
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。