vlambda博客
学习文章列表

gradle安装和源码编译

gradle安装和源码编译

安装

下载

从 Gralde 官方网站下载 Gradle 的最新发行包。

https://gradle.org/install/

包的链接

https://services.gradle.org/distributions/

解压

Gradle 发行包是一个 ZIP 文件。完整的发行包包括以下内容(官方发行包有 full 完整版,也有不带源码和文档的版本,可根据需求下载。[Rover12421]译注):Gradle 可执行文件用户手册 (有 PDF 和 HTML 两种版本)DSL 参考指南API 手册(Javadoc 和 Groovydoc)样例,包括用户手册中的例子,一些完整的构建样例和更加复杂的构建脚本源代码。仅供参考使用,如果你想要自己来编译 Gradle 你需要从源代码仓库中检出发行版本源码,具体请查看 Gradle 官方主页。

配置环境变量

运行 gradle 必须将 GRADLE_HOME/bin 加入到你的 PATH 环境变量中。

export PATH=$PATH:/opt/gradle/gradle-6.0.1/bin

测试安装

运行如下命令来检查是否安装成功.该命令会显示当前的 JVM 版本和 Gradle 版本。

gradle -v

JVM 参数配置

Gradle 运行时的 JVM 参数可以通过 GRADLE_OPTS 或 JAVA_OPTS 来设置.这些参数将会同时生效。JAVA_OPTS 设置的参数将会同其它 JAVA 应用共享,一个典型的例子是可以在 JAVA_OPTS 中设置代理和 GRADLE_OPTS 设置内存参数。同时这些参数也可以在 gradle 或者 gradlew 脚本文件的开头进行设置。

构建基础

Gradle 的 build-by-convention 可以让您来具体定义一个 project 到底该做什么。每个 project 都由多个 tasks 组成。每个 task 都代表了构建执行过程中的一个原子性操作。如编译,打包,生成 javadoc,发布到某个仓库等操作。到目前为止,可以发现我们可以在一个 project 中定义一些简单任务,后续章节将会阐述多项目构建和多项目多任务的内容。

你可以通过在命令行运行 gradle 命令来执行构建,gradle 命令会从当前目录下寻找 build.gradle 文件来执行构建。我们称 build.gradle 文件为构建脚本。严格来说这其实是一个构建配置脚本,后面你会了解到这个构建脚本定义了一个 project 和一些默认的 task。

你可以创建如下脚本到 build.gradle 中 To try this out,create the following build script named build.gradle。

第一个构建脚本

build.gradle

task hello {
  doLast {
      println 'Hello world!'
  }
}

然后在该文件所在目录执行 gradle -q hello-q 参数的作用是什么?该参数用来控制 gradle 的日志级别,可以保证只输出我们需要的内容。

执行脚本

Output of gradle -q hello
> gradle -q hello
Hello world

上面的脚本定义了一个叫做 hello 的 task,并且给它添加了一个动作。当执行 gradle hello 的时候, Gralde 便会去调用 hello 这个任务来执行给定操作。这些操作其实就是一个用 groovy 书写的闭包。

快速定义任务

build.gradle
task hello << {
  println 'Hello world!'
}

任务依赖

你可以按如下方式创建任务间的依赖关系在两个任务之间指明依赖关系

build.gradle

task hello << {
  println 'Hello world!'
}
task intro(dependsOn: hello) << {
  println "I'm Gradle"
}

gradle -q intro 的输出结果

Output of gradle -q intro
> gradle -q intro
Hello world!
I'm Gradle

添加依赖 task 也可以不必首先声明被依赖的 task。

Gradle Java 构建入门

较常用的任务有如下几个:

clean

删除 build 目录以及所有构建完成的文件。

assemble

编译并打包 jar 文件,但不会执行单元测试。一些其他插件可能会增强这个任务的功能。例如,如果采用了 War 插件,这个任务便会为你的项目打出 War 包。

check

编译并测试代码。一些其他插件也可能会增强这个任务的功能。例如,如果采用了 Code-quality 插件,这个任务会额外执行 Checkstyle。

Groovy├── build.gradle  1├── gradle│   └── wrapper│       ├── gradle-wrapper.jar  2│       └── gradle-wrapper.properties  3├── gradlew  4├── gradlew.bat  5└── settings.gradle  61、Gradle build script for configuring the current project2、Gradle Wrapper executable JAR3、Gradle Wrapper configuration properties4、Gradle Wrapper script for Unix-based systems5、Gradle Wrapper script for Windows6、Gradle settings script for configuring the Gradle build


源码

下载源码:

https://github.com/gradle/gradle

编译源码

./gradlew assemble

脚本说明“CLASSPATH:gradle-3.1\gradle\wrapper\gradle-wrapper.jar

表示的是gradle源代码里面gralde\wrapper\目录下gradle-wrapper.jar,这个jar也是待会要执行的编译操作要运行的jar。DEFAULT_JVM_OPTS:-Xmx1024m -Dfile.encoding=UTF-8 表示的是Java虚拟机配置JAVA_OPTS:空GRADLE_OPTS:空CMD_LINE_ARGS:assemble 表示要执行的gradle task名字

导出编译完成的文件

./gradlew install -Pgradle_installPath=/any/path

完成后会在指定路径生成gradle 二进制包

如:

./gradlew install -Pgradle_installPath=/home/loongson/data/work/03_sw/gradle/gradle-source-build
./gradlew installAll -Pgradle_installPath=/home/loongson/data/work/03_sw/gradle/gradle-source-build

详细信息可以查看https://github.com/gradle/gradle/blob/master/CONTRIBUTING.md

注意:源码编译只支持 jdk11

编译问题

1、

* What went wrong:
Could not determine the dependencies of task ':reporting:reportResources'.
> Could not resolve all task dependencies for configuration ':reporting:reports'.
  > Could not resolve jquery:jquery.min:1.11.0.
    Required by:
        project :reporting
    > Could not resolve jquery:jquery.min:1.11.0.
        > Could not get resource 'https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'.
          > Could not HEAD 'https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'.
              > Connection reset

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.1-20191219082728+0000/userguide/command_line_interface.html#sec:command_line_warnings

主要是jquery.min:1.11.0无法下载导致的,解决两个思路a)添加代理,在 gradle.properties加入如下内容

#Configuring an HTTP proxy using gradle.properties
systemProp.http.proxyHost=www.somehost.org
systemProp.http.proxyPort=8080
systemProp.http.proxyUser=userid
systemProp.http.proxyPassword=password
systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost
There are separate settings for HTTPS.

#Configuring an HTTPS proxy using gradle.properties
systemProp.https.proxyHost=www.somehost.org
systemProp.https.proxyPort=8080
systemProp.https.proxyUser=userid
systemProp.https.proxyPassword=password
systemProp.https.nonProxyHosts=*.nonproxyrepos.com|localhost

b)直接注释掉,需要修改3个地方如下:

./subprojects/docs/docs.gradle:71:    jquery "jquery:jquery.min:1.8.0@js"
./subprojects/reporting/reporting.gradle.kts:44: add("reports", "jquery:jquery.min:1.11.0@js")
./subprojects/internal-performance-testing/internal-performance-testing.gradle.kts:35: reports("jquery:jquery.min:1.11.0@js")

查找方法

[root@localhost gradle]# grep jquery.min -nr ./
./subprojects/docs/docs.gradle:71: jquery "jquery:jquery.min:1.8.0@js"
./subprojects/javascript/src/integTest/groovy/org/gradle/plugins/javascript/base/JavaScriptBasePluginIntegrationTest.groovy:46: jquery "jquery:jquery.min:1.7.2@js"
./subprojects/javascript/src/integTest/groovy/org/gradle/plugins/javascript/base/JavaScriptBasePluginIntegrationTest.groovy:58: def jquery = file("jquery/jquery.min-1.7.2.js")
./subprojects/javascript/src/integTest/groovy/org/gradle/plugins/javascript/envjs/EnvJsPluginIntegrationTest.groovy:85: jquery "jquery:jquery.min:1.7.2@js"
./subprojects/reporting/reporting.gradle.kts:44: add("reports", "jquery:jquery.min:1.11.0@js")
./subprojects/internal-performance-testing/src/main/groovy/org/gradle/performance/results/report/AbstractReportGenerator.java:67: copyResource("jquery.min-1.11.0.js", outputDirectory);
./subprojects/internal-performance-testing/src/main/groovy/org/gradle/performance/results/report/HtmlPageGenerator.java:67: .src(rootDir + "js/jquery.min-1.11.0.js")
./subprojects/internal-performance-testing/internal-performance-testing.gradle.kts:35: reports("jquery:jquery.min:1.11.0@js")
./subprojects/diagnostics/src/main/java/org/gradle/api/reporting/dependencies/internal/ProjectPageRenderer.java:41: final String jqueryLink = requireClassResource("/org/gradle/reporting/jquery.min-1.11.0.js", builder);
./subprojects/diagnostics/src/integTest/groovy/org/gradle/api/reporting/dependencies/HtmlDependencyReportTaskIntegrationTest.groovy:244: file("build/reports/project/dependencies/js/jquery.min-1.11.0.js").assertExists()

架构问题 unknown os.arch: mips64el

14:46:16.807 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Build operation 'Execute container callback action' completed
14:46:16.808 [INFO] [com.google.gradle.osdetector.OsDetector] ------------------------------------------------------------------------
14:46:16.808 [INFO] [com.google.gradle.osdetector.OsDetector] Detecting the operating system and CPU architecture
14:46:16.808 [INFO] [com.google.gradle.osdetector.OsDetector] ------------------------------------------------------------------------
14:46:16.809 [INFO] [com.google.gradle.osdetector.OsDetector] os.detected.name=linux
14:46:16.809 [INFO] [com.google.gradle.osdetector.OsDetector] os.detected.arch=unknown

……
14:46:16.815 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Daemon worker Thread 10: released lock on root.1
14:46:16.816 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
14:46:16.816 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] FAILURE: Build failed with an exception.
14:46:16.816 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
14:46:16.816 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * What went wrong:
14:46:16.816 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] A problem occurred configuring root project 'java'.
14:46:16.816 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] > unknown os.arch: mips64el
14:46:16.816 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
14:46:16.816 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Try:
14:46:16.817 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Run with --stacktrace option to get the stack trace. Run with --scan to get full insights.
14:46:16.817 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
14:46:16.817 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Get more help at https://help.gradle.org
14:46:16.817 [WARN] [org.gradle.internal.featurelifecycle.LoggingDeprecatedFeatureHandler]
Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.6-20200214074751+0000/userguide/command_line_interface.html#sec:command_line_warnings
14:46:16.817 [ERROR] [org.gradle.internal.buildevents.BuildResultLogger]
14:46:16.818 [ERROR] [org.gradle.internal.buildevents.BuildResultLogger] BUILD FAILED in 3s
14:46:16.818 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Completing Build operation 'Run build'
14:46:16.818 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Build operation 'Run build' completed

使用gradle编译客户提供的demo就会有这个问题,buile.gradle如下,使用了/apply plugin: "com.google.osdetector"是个旧版本,

buildscript {
repositories {
mavenLocal()
maven { url "http://maven.aliyun.com/nexus/content/groups/public" }
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
}
}

plugins {
id "java"
id "com.google.protobuf" version "0.8.0"
id "eclipse"
id "maven-publish"
id "com.google.osdetector" version "1.6.2"
}

archivesBaseName = 'shim-client'
version = '1.0'


sourceSets {
main {
java {
srcDir 'src/main/java'
}
proto {
srcDir 'src/main/proto'
}
}
}

repositories {
mavenLocal()
maven { url "http://maven.aliyun.com/nexus/content/groups/public" }
mavenCentral()
}
println "System os.arch=" + System.getProperty("os.arch")
//apply plugin: "com.google.osdetector"

def tcnative_classifier = osdetector.classifier;
……

是这个插件报出来的google/osdetector-gradle-plugin这个插件又依赖trustin/os-maven-plugin插件获取系统架构,trustin/os-maven-plugin1.6.0版本支持了mips64el架构,如下是release note

released this on 22 Apr 2018 · 11 commits to master since this release

Added support for MIPS and MIPSEL 32/64-bit architecture
mips_32 - if the value is one of: mips, mips32
mips_64 - if the value is mips64
mipsel_32 - if the value is one of: mipsel, mips32el
mipsel_64 - if the value is mips64el

google/osdetector-gradle-plugin插件的版本也需要1.6.0查能对应trustin/os-maven-plugin 1.6.0,但项目中使用的是1.4.0的版本,所以是不支持mips64el的,修改版本后正常了 id "com.google.osdetector" version "1.6.2"