Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

第 24 章 Gradle 5

目录

24.1. 安装 Gradle
24.1.1. CentOS
24.1.2. Mac
24.1.3. Artifactory 本地仓库
24.2. Example
24.3. gradle 命令
24.3.1. tasks 列出任务
24.4. build.gradle
24.4.1. repositories
24.4.2. dependencies
24.4.3. jar
24.4.4. Task
24.5. gradle.properties
24.5.1. 列出 properties
24.5.2. 自定义 gradle.properties
24.5.3. ext
24.5.4. System.properties

24.1. 安装 Gradle

24.1.1. CentOS

安装 Gradle

			
curl -s https://raw.githubusercontent.com/oscm/shell/38c2d4d307ce7a0760fc88d5d9703eef64d3b81c/lang/java/gradle/gradle-2.9.sh | bash
			
			

安装后运行 gradle --help 验证释放正确安装。

			
neo@MacBook-Pro ~ % gradle --help

Welcome to Gradle 5.1.1!

Here are the highlights of this release:
 - Control which dependencies can be retrieved from which repositories
 - Production-ready configuration avoidance APIs

For more details see https://docs.gradle.org/5.1.1/release-notes.html


USAGE: gradle [option...] [task...]

-?, -h, --help            Shows this help message.
-a, --no-rebuild          Do not rebuild project dependencies.
-b, --build-file          Specify the build file.
--build-cache             Enables the Gradle build cache. Gradle will try to reuse outputs from previous builds.
-c, --settings-file       Specify the settings file.
--configure-on-demand     Configure necessary projects only. Gradle will attempt to reduce configuration time for large multi-project builds. [incubating]
--console                 Specifies which type of console output to generate. Values are 'plain', 'auto' (default), 'rich' or 'verbose'.
--continue                Continue task execution after a task failure.
-D, --system-prop         Set system property of the JVM (e.g. -Dmyprop=myvalue).
-d, --debug               Log in debug mode (includes normal stacktrace).
--daemon                  Uses the Gradle Daemon to run the build. Starts the Daemon if not running.
--foreground              Starts the Gradle Daemon in the foreground.
-g, --gradle-user-home    Specifies the gradle user home directory.
-I, --init-script         Specify an initialization script.
-i, --info                Set log level to info.
--include-build           Include the specified build in the composite.
-m, --dry-run             Run the builds with all task actions disabled.
--max-workers             Configure the number of concurrent workers Gradle is allowed to use.
--no-build-cache          Disables the Gradle build cache.
--no-configure-on-demand  Disables the use of configuration on demand. [incubating]
--no-daemon               Do not use the Gradle daemon to run the build. Useful occasionally if you have configured Gradle to always run with the daemon by default.
--no-parallel             Disables parallel execution to build projects.
--no-scan                 Disables the creation of a build scan. For more information about build scans, please visit https://gradle.com/build-scans.
--offline                 Execute the build without accessing network resources.
-P, --project-prop        Set project property for the build script (e.g. -Pmyprop=myvalue).
-p, --project-dir         Specifies the start directory for Gradle. Defaults to current directory.
--parallel                Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use.
--priority                Specifies the scheduling priority for the Gradle daemon and all processes launched by it. Values are 'normal' (default) or 'low' [incubating]
--profile                 Profile build execution time and generates a report in the <build_dir>/reports/profile directory.
--project-cache-dir       Specify the project-specific cache directory. Defaults to .gradle in the root project directory.
-q, --quiet               Log errors only.
--refresh-dependencies    Refresh the state of dependencies.
--rerun-tasks             Ignore previously cached task results.
-S, --full-stacktrace     Print out the full (very verbose) stacktrace for all exceptions.
-s, --stacktrace          Print out the stacktrace for all exceptions.
--scan                    Creates a build scan. Gradle will emit a warning if the build scan plugin has not been applied. (https://gradle.com/build-scans)
--status                  Shows status of running and recently stopped Gradle Daemon(s).
--stop                    Stops the Gradle Daemon if it is running.
-t, --continuous          Enables continuous build. Gradle does not exit and will re-execute tasks when task file inputs change.
--update-locks            Perform a partial update of the dependency lock, letting passed in module notations change version. [incubating]
-v, --version             Print version info.
-w, --warn                Set log level to warn.
--warning-mode            Specifies which mode of warnings to generate. Values are 'all', 'summary'(default) or 'none'
--write-locks             Persists dependency resolution for locked configurations, ignoring existing locking information if it exists [incubating]
-x, --exclude-task        Specify a task to be excluded from execution.
			
			

24.1.2. Mac

			
neo@MacBook-Pro ~ %  brew install gradle
			
			
			
neo@MacBook-Pro-M2 test % gradle 

ERROR: JAVA_HOME is set to an invalid directory: @@HOMEBREW_JAVA@@

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.

neo@MacBook-Pro-M2 test % export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home  			
			
			

24.1.3. Artifactory 本地仓库

	
	
	
			

24.1.3.1. Artifactory Web UI

通过访问 http://localhost:8081/artifactory/ ,用默认管理员账号密码(用户名:admin,密码:password ) 登录,进入管理界面体验 Artifactory。

24.1.3.2. build.gradle

		
buildscript {
    repositories {

        maven {
            url "http://artifactory.netkiller.cn:8081/artifactory/libs-release-local"
        }

        jcenter()
    }
    dependencies {
        ...
    }
}

allprojects {
    repositories {
        maven {
            url "http://artifactory.netkiller.cn:8081/artifactory/libs-release-local"
        }

        jcenter()
    }
}
		
				

Artifactory 默认将 jecnter 库缓存到私有 maven仓库中。我们可以在项目中配置 jcenter 仓库信息列表,后续编译所需要的包都直接从私服 maven仓库读取,加快项目编译速度。

		
buildscript {
    repositories {
        maven {
            url "http://artifactory.netkiller.cn:8081/artifactory/jcenter" // 配置私服jcenter仓库信息
        }

        maven {
            url "http://artifactory.netkiller.cn:8081/artifactory/libs-release-local"
        }

        jcenter()
    }
    dependencies {
       ...
    }
}

allprojects {
    repositories {
        maven {
            url "http://artifactory.netkiller.cn:8081/artifactory/jcenter" // 配置私服jcenter仓库信息
        }

        maven {
            url "http://artifactory.netkiller.cn:8081/artifactory/libs-release-local"
        }

        jcenter()
    }
}