05 - Gradle构建任务

2017-01-12  本文已影响54人  半个王国

前面几篇介绍了CI基础环境的部署,但是缺少灵魂,这里的灵魂,我们选用Gradle

基础框架


基础概念

Gradle 插件

参考资料:https://docs.gradle.org/2.13/userguide/plugins.html

依赖管理

参考资料:
https://docs.gradle.org/2.13/userguide/artifact_dependencies_tutorial.html

Gradle常用命令

新建项目


开始了哟


Paste_Image.png
Paste_Image.png
Paste_Image.png

Gradle的文件结构和Maven基本一致:

编译任务


前面提到,只需引入 Java 插件即可实现Java项目的clean、build、test、Jar等工作,下面我们看一下具体的任务有哪些:

>gradlew tasks
:tasks                                     

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build tasks           
-----------           
assemble - Assembles the outputs of this project.
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.

Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'gradle-demo'.
components - Displays the components produced by root project 'gradle-demo'. [incubating]
dependencies - Displays all dependencies declared in root project 'gradle-demo'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradle-demo'.
help - Displays a help message.
model - Displays the configuration model of root project 'gradle-demo'. [incubating]
projects - Displays the sub-projects of root project 'gradle-demo'.
properties - Displays the properties of root project 'gradle-demo'.
tasks - Displays the tasks runnable from root project 'gradle-demo'.

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

Scope,引入的Java插件包含以下几个Scope:

不同于Maven中固定的Scope,Gradle中的Scope是动态的,引入不同的插件可引入新的Scope,如引入Groovy插件,即进入了Groovy Scope

查看依赖情况

$ gradle dependencies
archives - Configuration for the default artifacts.
No dependencies

compile - Classpath for compiling the sources.
+--- mule:mule-extras-groovy:1.1.1 [default]
\--- commons-beanutils:commons-beanutils:1.8.3 [default]
  \--- commons-logging:commons-logging:1.1.1 [compile,master,runtime]

default - Configuration for the default artifacts and their dependencies.
+--- mule:mule-extras-groovy:1.1.1 [default]
\--- commons-beanutils:commons-beanutils:1.8.3 [default]
  \--- commons-logging:commons-logging:1.1.1 [compile,master,runtime]

groovy - The groovy libraries to be used for this Groovy project.
\--- mule:mule-extras-groovy:1.1.1 [default]

runtime - Classpath for running the compiled sources.
+--- mule:mule-extras-groovy:1.1.1 [default]
\--- commons-beanutils:commons-beanutils:1.8.3 [default]
  \--- commons-logging:commons-logging:1.1.1 [compile,master,runtime]

单元测试


编译单元测试

$ gradle testClasses
:compileJava
:processResources
:classes
:compileTestJava
:processTestResources
:testClasses

BUILD SUCCESSFUL

执行单元测试

$ gradle test
:compileJava
:processResources
:classes
:compileTestJava
:processTestResources
:testClasses
:test

执行后,在 build/report/test 下面有测试报告


Paste_Image.png

多源码路径

如下语句添加了源码路径:srcAdditional/main/java

apply plugin: 'java'

sourceSets.main.java.srcDirs =
  ["src/main/java", "srcAdditional/main/java"]

定义默认任务

apply plugin: 'java'

defaultTasks 'clean', 'build'

Maven 插件

当我们要发布Jar包供其他人使用,尤其是使用Maven构建的项目,此时我们需要引入 maven 插件生产 pom.xml 文件,如下所示 archivesBaseName 原来是Java插件使用的,现在也可以被Maven插件使用,用来生产 pom.xml 文件中的 artifactId:

构建脚本

apply plugin: 'java'
apply plugin: 'maven'

group = 'com.gradleware.samples'
// archivesBaseName 如果没有设置,默认使用 project.name (缺点,不能更改)
archivesBaseName = 'gradle-demo'
version = '1.0-SNAPSHOT'
description ="A sample project that uses the Maven plug-in and defines many attributes."

产生 pom.xml 文件

$ gradle install

:compileJava
:processResources
:classes
:jar
:install
产生的 pom 文件

构件发布:uploadArchives

build.gradle 文件中声明构件属性

// 构件属性
group 'com.itc.demo'
archivesBaseName = 'gradle-demo'
version = '1.0-SNAPSHOT'
description = "A sample project that uses the Maven plug-in and defines many attributes."

紧接着声明发布属性

// 发布构件
uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: repositoriesUploadReleasesUrl) {
                authentication(userName: nexusDeployUsername, password: nexusDeployPassword)
            }
            snapshotRepository(url: repositoriesUploadSnapshotsUrlDev) {
                authentication(userName: nexusDeployUsername, password: nexusDeployPassword)
                snapshotTimeout = 0
            }
            uniqueVersion = false
        }
    }
}

代码审查

先来看看Gradle构建脚本的配置:

gradle.properties中声明SonarQube属性

# SonarQube服务地址
systemProp.sonar.host.url=http://sonar.inspur.com:9000
#----- Security (when 'sonar.forceAuthentication' is set to 'true')
# SonarQube 中用户的Token
systemProp.sonar.login=1df1b3143013f43640b036ef160724935edd3c27

build.gradle 中引入SonarQube插件

// 代码审查
plugins {    
  id "org.sonarqube" version "2.2.1"
}

执行代码审查

gradlew sonarqube

任务结束后,审查后的数据就到了SonarQube的WebConsol里面


Paste_Image.png
上一篇 下一篇

猜你喜欢

热点阅读