Loading

Java Gradle Project 应用

Java Gradle Project 应用

大纲

1 gradle 介绍

2 gradle 安装

3 gradle 介绍

4 gradle 项目目录结构介绍

5 groovy 简单语法

6 groovy 中的闭包

7 gradle 配置文件的介绍

8 让 gradle 使用本地 maven 仓库

9 gradle 开发 web 工程

10 gradle 工程拆分与聚合

Gradle 介绍

Grammatically, it is based on the Groovy language (Groovy is an agile development language based on JVM, which can be simply understood as a weakly typed version of the strongly typed language java), and is a project automation construction tool based on the concepts of Ant and Maven in project management .

Gradle is a declarative build tool. During execution, Gradle does not execute the contents of the build.gradle file sequentially at the beginning, but is divided into two stages. The first stage is the configuration stage, and then the actual execution stage.
In the configuration stage, Gradle will read all the contents of all build.gradle files to configure Project and Task, such as setting up prject and task properties, and handling dependencies between tasks, etc.

Fundemental

Gradle 多porject 目录结构

├── app 
│   ├── build.gradle #app build script
├── module 
│   ├── build.gradle #module build script
├── build.gradle #project build script
├── gradle
│   └── wrapper #gradle package. Build with a unified Gradle version 
|       └── gradle-wrapper.jar # excutiable gradle package
|       └── gradle-wrapper.properties # configuration. # specify gradle version
├── gradle.properties #gradle configuration
├── gradlew #gradle wrapper linux shell script
├── gradlew.bat #gradle wrapper windows script
└── settings.gradle #project settings

build.gradle example andorid


android {
    compileSdkVersion COMPILE_SDK_VERSION as int
    buildToolsVersion BUILD_TOOLS_VERSION
    defaultConfig {
        applicationId "com.xtao.simpledemo"
        minSdkVersion MIN_SDK_VERSION as int
        targetSdkVersion TARGET_SDK_VERSION as int
        versionCode VERSION_CODE as int
        versionName VERSION_NAME
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField("int", "TARGET_SDK_VERSION", "${TARGET_SDK_VERSION}")
        }
        debug {
            buildConfigField("int", "TARGET_SDK_VERSION", "${TARGET_SDK_VERSION}")
            resValue("string", "VERSION_NAME", "${VERSION_NAME}")
        }
    }
}

build.gradle example java

plugins {
   id 'org.springframework.boot' version "${SPRING_BOOT_VERSION}"
   id 'io.spring.dependency-management' version "${SPRING_DEPENDENCY_MANAGEMENT_VERSION}"
   id 'java'
}

group = 'com.sirius'

allprojects{
	version = '0.0.1-SNAPSHOT'
	sourceCompatibility = '1.8'
}

repositories {
   mavenCentral()
}

dependencies {
   implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
   implementation 'org.springframework.boot:spring-boot-starter-jdbc'
   implementation 'org.springframework.boot:spring-boot-starter-web'
   developmentOnly 'org.springframework.boot:spring-boot-devtools'
   testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
   useJUnitPlatform()
}

gradle-wrapper.properties example

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

gradle.properties

Specially used to configure global key-value pair data. Can be used to store sensitive data. Exclude it from git version control, so that the gradle.properties file can only be kept locally

grale.properties example

SPRING_BOOT_VERSION = 2.4.3
SPRING_DEPENDENCY_MANAGEMENT_VERSION = 1.0.11.RELEASE
COMPILE_SDK_VERSION = 26
BUILD_TOOLS_VERSION = 26.0.0
MIN_SDK_VERSION = 19
TARGET_SDK_VERSION = 26
VERSION_CODE = 1
VERSION_NAME = 1.0

settings.gradle

main project and moudle setings

settings.gradle example

rootProject.name = 'hello_gradle'
include 'sub_project'

Optimizing Gradle Build Performance

Using The Build Cache

Migrating build logic from Groovy to Kotlin

Continuous Integration

Executing Gradle Builds on Jenkins

Executting Gradle Builds on TeamCity

JVM

Creating Multi-project Builds

Building Java Libraries

Building Java Applications

Building Java Modules


Placeholder

#### Consuming JVM Libraries



#### Building Groovy Applications



#### Building Groovy Libraries



### Plugin Development



#### Designing Gradle Plugins



#### Implementing Gradle Plugins



#### Testing Gradle Plugins 



#### Publishing Plugins to Gradle Plugin Portal



#### Using the Worker API


Java中调用 gradle.properties 值

build.gradle中设置buildConfigField("int", "TARGET_SDK_VERSION", "${TARGET_SDK_VERSION}")
依次为:参数类型,参数名,参数值

int targetSDKVersion = BuildConfig.TARGET_SDK_VERSION;

用ResourceBundle获取

ResourceBundle bundle = ResourceBundle.getBundle("gradle");//gradle为properties的文件名
String result = bundle.getString("test_key");//test_key是properties文件中的key值

用Properties 获取

Properties properties = new Properties();
InputStream is = this.getClassLoader().getResourceAsStream("gradle.properties");//path
properties.load(is);
String result= properties.getProperty("test_key");//test_key是properties文件中的key值

XML中调用 gradle.properties 中值

build.gradle中设置resValue("string", "VERSION_NAME", "${VERSION_NAME}")
依次为:参数类型,参数名,参数值

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/VERSION_NAME"/>
posted @ 2021-03-16 16:52  Sirius-Wang  阅读(95)  评论(0编辑  收藏  举报