build.gradle文件详解

    技术2022-07-11  100

    建议阅读:https://www.cnblogs.com/mingfeng002/p/11751119.html

    原文链接:https://blog.csdn.net/jiaov/article/details/86557527

    build.gradle - 文件包含项目构建所使用的脚本。settings.gradle - 文件将包含必要的一些设置,例如,任务或项目之间的依懒关系等

     

    1. build.gradle

    //构建脚本(给脚本用的脚本) buildscript { //存储一个属于gradle的变量,整个工程都能用,可通过gradle.ext.springBootVersion使用 ext { springBootVersion = '2.1.2.RELEASE' } /*配置仓库地址,从而找到外部依赖 按照你在文件中(build.gradle)仓库的顺序寻找所需依赖(如jar文件), 如果在某个仓库中找到了,那么将不再其它仓库中寻找 */ repositories { //mavenLocal()本地库,local repository(${user.home}/.m2/repository) mavenCentral()//maven的中央仓库 //阿里云Maven远程仓库 maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } } /*配置springboot插件加载 */ dependencies { // classpath 声明说明了在执行其余的脚本时,ClassLoader 可以使用这些依赖项 classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } //使用以下插件 apply plugin: 'java' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8'//jvm版本要求 // 定义仓库 repositories { maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'} maven{url 'https://mvnrepository.com/'} mavenCentral() } // 定义依赖:声明项目中需要哪些依赖 dependencies { compile 'org.springframework.boot:spring-boot-starter' compile('org.springframework.boot:spring-boot-starter-web')//引入web模块,springmvc compile 'org.springframework.boot:spring-boot-starter-test' } 新配置弃用配置行为作用implementationcompile依赖项在编译时对模块可用,并且仅在运行时对模块的消费者可用。 对于大型多项目构建,使用 implementation 而不是 api/compile 可以显著缩短构建时间,因为它可以减少构建系统需要重新编译的项目量。 大多数应用和测试模块都应使用此配置。implementation 只会暴露给直接依赖的模块,使用此配置,在模块修改以后,只会重新编译直接依赖的模块,间接依赖的模块不需要改动apicompile依赖项在编译时对模块可用,并且在编译时和运行时还对模块的消费者可用。 此配置的行为类似于 compile(现在已弃用),一般情况下,您应当仅在库模块中使用它。 应用模块应使用 implementation,除非您想要将其 API 公开给单独的测试模块。api 会暴露给间接依赖的模块,使用此配置,在模块修改以后,模块的直接依赖和间接依赖的模块都需要重新编译compileOnlyprovided依赖项仅在编译时对模块可用,并且在编译或运行时对其消费者不可用。 此配置的行为类似于 provided(现在已弃用)。只在编译期间依赖模块,打包以后运行时不会依赖,可以用来解决一些库冲突的问题runtimeOnlyapk依赖项仅在运行时对模块及其消费者可用。 此配置的行为类似于 apk(现在已弃用)。只在运行时依赖模块,编译时不依赖 新版本: ---------------------------------- plugins { id 'org.springframework.boot' version '2.3.0.RELEASE' id 'io.spring.dependency-management' version '1.0.9.RELEASE' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { mavenCentral() } ext { set('springCloudVersion', "Hoxton.SR4") } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } } test { useJUnitPlatform() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' implementation group: 'com.github.xiaoymin', name: 'knife4j-spring-boot-starter', version: '2.0.4' testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } } ------------------- 也可以这样 dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:recyclerview-v7:27.1.1' implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.2' implementation 'com.jakewharton:butterknife:5.1.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' } ————————————————

    2.settings.gradle配置

    settings.gradles是模块Module配置文件,大多数setting.gradle的作用是为了配置子模块, 根目录下的setting.gradle脚本文件是针对module的全局配置 settings.gradle用于创建多Project的Gradle项目。Project在IDEA里对应Module模块。 例如配置module名rootProject.name = 'project-root',为指定父模块的名称, include 'hello' 为指定包含哪些子模块 ------------------------ //平台根 rootProject.name = 'project-root' //包含子系统以及模块 include ':project-core' //Hello系统模块的加载 include ':project-hello' //World系统模块的加载 include ':project-world'

    3.gradle wrapper

    gradlew / gradlew.bat 这个文件用来下载特定版本的 gradle 然后执行的,就不需要开发者在本地再安装 gradle 了。这样做有什么好处呢?开发者在本地安装 gradle,会碰到的问题是不同项目使用不同版本的 gradle 怎么处理,用 wrapper 就很好的解决了这个问题,可以在不同项目里使用不同的 gradle 版本。gradle wrapper 一般下载在 GRADLE_CACHE/wrapper/dists 目录下gradle/wrapper/gradle-wrapper.properties 是一些 gradlewrapper 的配置,其中用的比较多的就是 distributionUrl,可以执行 gradle 的下载地址和版本gradle/wrapper/gradle-wrapper.jar 是 gradlewrapper 运行需要的依赖包
    Processed: 0.010, SQL: 9