gradle学习与实战(Gradle learning and Practice)

gradle 安装与配置1. 官网https://gradle.org/install/#manually 下载

(注意发布提示中的版本支持,v2.0+ ==> jdk8.0;v5.0+ ==> jdk11;v6.0+ ==>jdk13)2. 解压缩后配置环境变量,windows10 中”我的电脑”==> 右击”属性”==> 选择”高级系统配置”,添加系统属性变量名: GRADLE_HOME变量值: 解压缩的文件路径(D:\tools\gradle-7.3)变量名:GRADLE_USER_HOME变量值:自定义Gradle仓库目录或者Maven的仓库目录(D:\repository)添加变量值变量名:Path变量值:%GRADLE_HOME%\bin3.检查安装是否成功: 在“cmd”命令行中输入“gradle -v” 展示出版本信息即成功;4.配置gradle仓库源在安装目录的init.d文件夹下,创建”.gradle”后缀文件(如: init.gradle)allprojects { repositories { mavenLocal() maven { name “Alibaba” ; url “https://maven.aliyun.com/repository/public” } maven { name “Bsdn” ; url “http://nexus.bsdn.org/content/groups/public/” } mavenCentral() } buildscript { repositories { maven { name “Alibaba” ; url ‘https://maven.aliyun.com/repository/public’ } maven { name “Bsdn” ; url ‘http://nexus.bsdn.org/content/groups/public/’ } maven { name “M2” ; url ‘https://plugins.gradle.org/m2/’ } } }}

其中第一为 maven 的本地仓库;第三第四分别为国内/国外的仓库地址,第五为Apache的仓库地址;

5.springboot+gradle项目使用Spring Initializr快速构建,或者在build.gradle中添加如下配置:dependencies { //web 应用程序 compile(‘org.springframework.boot:spring-boot-starter-web:2.6.0’) compile(‘org.projectlombok:lombok:1.18.14’)}

tasks.withType(JavaCompile) { options.encoding = ‘UTF-8’}

tasks.withType(Javadoc) { options.encoding = ‘UTF-8’}

配置测试接口类:import lombok.extern.slf4j.Slf4j;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;

@Slf4j@RestController@RequestMapping(“/test”)public class HelloController {

@RequestMapping(“hello”) public String sayHello(){ log.info(“Hello world!”); return “Hello world!”; }}配置启动类:import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplicationpublic class MyApplication {

public static void main(String[] args) { SpringApplication.run(MyApplication.class,args); }}

启动启动类,验证接口: http://localhost:8080/test/hello对于已有的maven项目,亦可在工程目录路径执行cmd命令,使用”gralde init”转换成Gradle项目;

————————

Gradle installation and configuration 1 Official website https://gradle.org/install/#manually Download

(note the version support in the release prompt, v2.0 + = = & gt; jdk8.0; V5.0 + = = & gt; jdk11; V6.0 + = = & gt; jdk13) 2 After decompression, configure the environment variables, and “my computer” = = & gt; Right click Properties = = & gt; Select advanced system configuration and add the system attribute variable name: grade_ Home variable value: decompressed file path (D: \ tools \ gradle-7.3) variable name: gradle_ USER_ Home variable value: customize gradle warehouse directory or Maven’s warehouse directory (D: \ repository). Add variable value variable name: path variable value:% grade_ HOME%\bin3. Check whether the installation is successful: enter “gradle – V” in the “CMD” command line to display the version information, which is successful; 4. Configure the gradle warehouse source in init. Of the installation directory D folder, create “. Gradle” suffix file (e.g. init. Gradle) allprojects {repositories {maven local() maven {name “Alibaba”; URL“ https://maven.aliyun.com/repository/public ” } maven { name “Bsdn” ; url ” http://nexus.bsdn.org/content/groups/public/ ” } mavenCentral() } buildscript { repositories { maven { name “Alibaba” ; url ‘ https://maven.aliyun.com/repository/public ‘ } maven { name “Bsdn” ; url ‘ http://nexus.bsdn.org/content/groups/public/ ‘ } maven { name “M2” ; url ‘ https://plugins.gradle.org/m2/ ‘ } } }}

The first is Maven’s local warehouse; The third and fourth are the domestic / foreign warehouse addresses respectively, and the fifth is the Apache warehouse address;

5.springboot+gradle项目使用Spring Initializr快速构建,或者在build.gradle中添加如下配置:dependencies { //web 应用程序 compile(‘org.springframework.boot:spring-boot-starter-web:2.6.0’) compile(‘org.projectlombok:lombok:1.18.14’)}

tasks.withType(JavaCompile) { options.encoding = ‘UTF-8’}

tasks.withType(Javadoc) { options.encoding = ‘UTF-8’}

配置测试接口类:import lombok.extern.slf4j.Slf4j;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;

@Slf4j@RestController@RequestMapping(“/test”)public class HelloController {

@RequestMapping(“hello”) public String sayHello(){ log.info(“Hello world!”); return “Hello world!”; }}配置启动类:import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplicationpublic class MyApplication {

public static void main(String[] args) { SpringApplication.run(MyApplication.class,args); }}

Start the class and verify the interface: http://localhost:8080/test/hello For the existing Maven project, you can also execute the CMD command in the project directory path and use “gradde init” to convert it to gradle project;