Detailed gradle of build.gradle

A project in place only one build.gradle, build.gradle can be understood as a Project Framework, it provides a basic configuration of a project.

common project:

Attributes:

  1. group
  2. name(artifact)
  3. version project version (global)
  4. sourceCompatibility = 1.8 // jdk version of the source used 
  5. When using targetCompatibility = 1.8 // jdk compiled or updated version of java virtual machine compatible
  6. compileJava.options.encoding = 'UTF-8'        
  7. compileTestJava.options.encoding = 'UTF-8'    

method: 

  1. apply application plug-ins
  2. Add rely dependencies
  3. Add warehouse repositories
  4. task defined task
  5. buildscript
  6. allprojects
  7. subprojects
  8. configurations

 

gradle in repositories 

//repositories是project一个方法,闭包作为参数
repositories {
    mavenCentral()
}

This is used to specify what the library, it can exist buildScript block, allprojects block, root-level blocks.

buildScript block repositories primarily to Gradle script execution itself, to get the script dependent on plug-ins.

Root level repositories for the current project primarily to provide the required dependencies, such as log4j, spring-core can be obtained from other dependencies mavenCentral warehouse.

allprojects block repositories for constructing multiple projects, provide a common dependencies required for all items. The subprojects can configure their own repositories for their own independence required dependencies.

gradle in common property can be written in the gradle.properties

A gradle file attributes are many, such as jdk version, encoding type, dependency version. If you are placed in build.gradle certainly not good oh management (experience oh), then the default gradle provides a  gradle.properties  file. Use this file to manage all the properties in appropriate enough

Gradle plugin (Plugins)

Gradle can also declare plug used in the following way:

// build.gradle
plugins {
    id 'com.example.plugin', version '1.0'
}

In fact, from Gradle official plugin repository  https://plugins.gradle.org/m2/  download.

However, the well-known reasons, can not connect in some areas, resulting in less need to download plug-ins, such as the following error:

* What went wrong:
A problem occurred configuring root project 'MyApp'.
> Could not resolve all files for configuration ':classpath'.
   > Could not download jimfs.jar (com.google.jimfs:jimfs:1.1)
      > Could not get resource 'https://plugins.gradle.org/m2/com/google/jimfs/jimfs/1.1/jimfs-1.1.jar'.
         > Could not HEAD 'https://plugins.gradle.org/m2/com/google/jimfs/jimfs/1.1/jimfs-1.1.jar'.
            > Connect to d29vzk4ow07wi7.cloudfront.net:443 [d29vzk4ow07wi7.cloudfront.net/54.192.84.6, d29vzk4ow07wi7.cloudfront.net/54.192.84.168, d29vzk4ow07wi7.cloudfront.net/54.192.84.128, d29vzk4ow07wi7.cloudfront.net/54.192.84.173] failed: Read timed out

Or, plug-in is not external, the existence of a private warehouse, how to modify or add additional private warehouse address?

Direct modification  settings.gradle to add additional warehouse:

// settings.gradle
pluginManagement {
    repositories {
        maven {
            url 'http://examle.com/maven-repo'
        }
        gradlePluginPortal()
    }
}

Of course, you can also use the old way of application plug-ins:

buildscript {
    repositories {
        maven {
            url 'http://examle.com/maven-repo'
        }
    }
    dependencies {
        classpath 'com.example.plugin:gradle-plugin:1.0'
    }
}

apply plugin: 'com.example.plugin'

dependencies

For example root-dependent configuration dependent single project:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

 

Construction of multi-project

settings.gradle role is to build a multi-project, generally like this:

rootProject.name = 'ant'
include 'framework-eureka'
include 'framework-gateway'

include 'module-common'
include 'module-test'


 

 

Guess you like

Origin blog.csdn.net/qq_36850813/article/details/93996333