gradle插件和依赖相关配置

gradle版本

假设你的本地gradle已经安装配置完成。没有安装配置的,可以参考 gradle安装

C:\Users\yueling.DANGDANG>gradle -v

------------------------------------------------------------
Gradle 4.5.1
------------------------------------------------------------

概述

gradle构建脚本定义了构建项目的过程; 每个项目包含一些依赖项和一些发布项。依赖项指支持构建项目的组件或文件,如所需JAR文件以。发布指项目的输出结果,如测试类文件和构建文件,war、jar等。

常用插件介绍

在配置开始之前先需要了解依赖如何配置,又均代表什么含义

不同插件有不同的标准配置,如常用的java插件、war插件

Java插件标准的配置

java插件-依赖配置

名称 扩展 被使用时运行的任务 含义
compile - compileJava 用来编译项目源代码的依赖
runtime compile - 在运行时被生成的类使用的依赖. 默认的, 也包含了编译时的依赖
testCompile compile compileTestJava 编译测试代码的依赖. 默认的, 包含生成的类运行所需的依赖和编译源代码的依赖
testRuntime runtime test 运行测试所需要的依赖. 默认的, 包含上面三个依赖
archives - uploadArchives 项目产生的信息单元(如:jar包)
default runtime - 使用其他项目的默认依赖项,包括该项目产生的信息单元以及依赖

Java插件-资源设置依赖关系配置

名称 扩展 被使用时运行的任务 含义
sourceSetCompile - compileSourceSetJava 编译时给定资源设置的依赖
sourceSetRuntime - - 运行时给定资源设置的依赖

Java插件-目录属性

属性名称 类型 默认值 描述
reportsDirName String reports 在构建目录的生成报告的文件夹名
reportsDir File (read-only) buildDir/reportsDirName 该目录下会生成报告
testResultsDirName String test-results 在构建目录的测试结果的result.xml的存放目录名
testResultsDir File (read-only) buildDir/testResultsDirName 测试结果的 result.xml 文件会存放在该文件夹中
testReportDirName String tests 在构建目录的测试报告的文件夹名
testReportDir File (read-only) reportsDir/testReportDirName 测试的测试报告会存放在该目录下
libsDirName String libs 在构建目录下的类库文件夹名
libsDir File (read-only) buildDir/libsDirName 该目录下存放类库
distsDirName String distributions 在构建目录下的distributions文件夹名
distsDir File (read-only) buildDir/distsDirName 该目录下存放生成的distributions
docsDirName String 在构建目录下的doc文件夹名
docsDir File (read-only) buildDir/docsDirName 该目录下存放生成的文档
dependencyCacheDirName String dependency-cache 在构建目录下的依赖缓存文件夹名
dependencyCacheDir File (read-only) buildDir/dependencyCacheDirName 该目录用来缓存源依赖信息。

参考 JavaPluginConvention
参考 BasePluginConvention

Java插件-其他配置

属性名称 类型 默认值 描述
sourceSets SourceSetContainer Not null 包含项目的资源设置
sourceCompatibility JavaVersion.也可以使用String类型或Number类型,如’1.5’ 或 1.5 当前使用的JVM版本 编译Java源码时所使用的Java兼容版本
targetCompatibility JavaVersion.也可以使用String类型或Number类型,如’1.5’ 或 1.5 sourceCompatibility 生成class文件的Java版本
archivesBaseName String projectName 用于.jar文件或者.zip存档的基本名称
manifest Mainfest an empty manifest 该清单中包括所有的JAR文件

war插件标准的配置

扩展自java插件,新增两个依赖管理
providedCompile和providedRuntime

参考 The War Plugin

声明依赖

java插件例子

先来个demo

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

此构建脚本做了什么呢~?

  • 启用java插件
  • 构建脚本声明 commons-collections 3.2.被用来编译项目的源代码. 在运行阶段同样也需要commons-collections 3.2和它的依赖.
  • 构建脚本同样声明了需要 junit >= 4.0 的版本来编译项目测试.
  • 构建脚本告诉 gradle 到 Maven 中央仓库里找任何需要的依赖.

引用一个外部依赖需要使用 group, name 和 version 属性. 根据你想要使用的库, group 和 version 可能会有所差别.有一种简写形式, 只使用一串字符串 “group:name:version” 如junit:junit:4.+

默认地, gradle 不提前定义任何仓库. 在使用外部依赖之前, 你需要自己至少定义一个库.一个项目可以有多个库. gradle 会根据依赖定义的顺序在各个库里寻找它们, 找到后对应文件后会忽略其他的仓库,终止查找。

//Maven central 仓库
repositories {
    mavenCentral()
}
//使用远程maven仓库
repositories {
    maven {
        url "http://repo.mycompany.com/maven2"
    }
}
//使用ivy本地仓库
repositories {
    ivy {
        // URL can refer to a local directory
        url "../local-repo"
    }
}
//使用远程ivy仓库
repositories {
    ivy {
        url "http://repo.mycompany.com/repo"
    }
}

相反:可以将输出文件上传至仓库

//运行uploadArchives 发布ivy库
uploadArchives {
    repositories {
        ivy {
            credentials {
                username "username"
                password "pw"
            }
            url "http://repo.mycompany.com"
        }
    }
}
//运行uploadArchives发布maven库
apply plugin: 'maven'
uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "file://localhost/tmp/myRepo/")
        }
    }
}

war插件例子

apply plugin: 'war'

version = "1.0"  

repositories{  
    mavenCentral()  
}  
[compileJava,compileTestJava,javadoc]*.options*.encoding = "UTF-8"  
dependencies{  
    providedCompile "javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1"  
    testCompile "junit:junit:4.12"  
}  
task testjar(type: Jar) {  
    baseName project.name  
    from("$buildDir/classes/main")  
}  
war{  
    dependsOn testjar  
    from("$projectDir/src/main/resources") {  
        include "*.properties"  
        into("WEB-INF/classes")  
    }  
    classpath=classpath - sourceSets.main.output  
    classpath fileTree(dir:libsDir, include:"${project.name}-${version}.jar")  
} 

构建脚本中的引用的变量在java插件章节都有介绍,自行对照~~

猜你喜欢

转载自blog.csdn.net/yue530tomtom/article/details/79392154