您现在的位置是:首页 > 文章详情

使用gradle构建java项目(1)

日期:2020-04-08点击:715

gradle 是什么

gradle 是一个让构建自动化的工具,类似于maven,ant的功能.
使用gradle可以给java项目编译,单元测试,打包,或者生成可执行的jar包等

gradle的依赖环境

gradle依赖java环境,所以使用gradle前需要安装jdk 或jre

gradle 构建项目的流程

gradle的构建依赖于task,
task可以指定与其他task之间的依赖关系
比如,有两个task,walk 和bike,如果指定walk依赖bike,那么
执行walk前会先执行bike.

task的来源有两种:

  1. 插件提供,gradle有很多现成的插件;

8a4823973d0541d2eed6ed26b84561bdbbf.jpg

  1. 自定义:在build.gradle 文件中声明task

e6c1f7ace96348dfba7f27da91b7fef95f0.jpg

生命周期

maven 的构建是通过生命周期来排列的,而 gradle 不是,它没有生命周期,它是通过任务的依赖关系来组织顺序的。

build.gradle 的执行流程

Lifecycle
There is a one-to-one relationship between a Project and a build.gradle file. During build initialisation, Gradle assembles a Project object for each project which is to participate in the build, as follows:

  1. Create a Settings instance for the build.
  2. Evaluate the settings.gradle script, if present, against the Settings object to configure it.
  3. Use the configured Settings object to create the hierarchy of Project instances.
  4. Finally, evaluate each Project by executing its build.gradle file, if present, against the project. The projects are evaluated in breadth-wise order, such that a project is evaluated before its child projects. This order can be overridden by calling Project.evaluationDependsOnChildren() or by adding an explicit evaluation dependency using Project.evaluationDependsOn(java.lang.String).

如何执行任务

方式一:使用全局命令

gradle <任务名>
113bccf7ad0c12859e52d947270487db401.jpg

方式二:使用项目中的脚本

./gradlew <任务名>
c5b538b2f4e34c680243a4055ba4c90086c.jpg

task 能实现哪些功能

下面是一些常用的基本功能

复制

task walk(description:'walk') {
    doLast {
        println 'walk...'
        println myName
        copy {
            into 'demo'
            exclude '**/.svn/**'
            from('README.md')
        }
    }
}

删除

task walk(description:'walk') {
    doLast {
        println 'walk...'
        println myName
        project.delete {
            delete 'README.md'
            followSymlinks = true
        }
        
    }
}

生成源码 jar 包

task sourcesJar(type: Jar) {
        classifier = 'sources'
        from sourceSets.main.allSource
        doFirst {
            manifest = defaultManifest([
                    project      : subproj,
                    projectVendor: project_vendor
            ])
        }
    }

设置/添加依赖

使用project 下面的dependencies
d18e9729da31def6889fc71d77a6d9be9ee.jpg

see /Users/whuanghkl/.gradle/wrapper/dists/gradle-4.4-all/9br9xq1tocpiv8o6njlyu5op1/gradle-4.4/src/core-api/org/gradle/api/Project.java

 void dependencies(Closure configureClosure);

dependencies 方法的参数是一个配置闭包,那么这个闭包有哪些配置呢?
5b926d1d98df5034b9f6e24055096aeb813.jpg

maven 中的

 <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
            <scope>compile</scope>
        </dependency>

等价于

compile 'commons-lang:commons-lang:2.6'

参考:https://docs.gradle.org/current/userguide/java_plugin.html#tab:configurations
https://docs.gradle.org/current/userguide/managing_dependency_configurations.html

引入本地jar包

 compile project.fileTree(dir:'/Users/whuanghkl/code/mygit/myproject/target',include:['io0007-0.0.1.jar'])

依赖指定文件夹

 compile project.fileTree(dir:'/Users/xx/Documents/mygit/demo/io0007/target',include:['*.jar'])
    compile project.fileTree(dir:'/Users/xx/Documents/mygit/demo/idea_plugin/intellij-aa-editor/lib',include:['*.jar'])

如何查询依赖的版本

直接使用 maven 仓库,那么就可以直接查询maven 仓库
https://mvnrepository.com/search?q=common-lang

gradle中依赖的仓库有多种:
8c277fb28a9985cf3e694833ea531cf46c8.jpg

参考:https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.RepositoryHandler.html

我们可以选择 maven仓库:

repositories {
    mavenCentral()
}

也可以使用本地的 maven 仓库:

repositories {
    mavenLocal()
}

2019-02-21_15-38-10.png
甚至可以直接指定依赖的 jar 包目录:

 maven {
        url "lib"
    }

那么 查询依赖就和maven一样了 .

gradle的插件有哪些

参考 https://docs.gradle.org/current/userguide/userguide.html

e10c1b93a97ae566bcc402852974f61fbdc.jpg

springboot 项目生成可执行的jar包

我的项目是spring boot,所以需要引入插件'org.springframework.boot'
id 'org.springframework.boot' version '2.0.4.RELEASE'
需要在build.gradle 文件中 指定可执行jar的main class :

jar {
    manifest {
        attributes 'Main-Class': 'com.kunlunsoft.Application'
    }
}

执行任务bootJar 就可以生成可执行的jar包
6de57783a20b55af0908f5cfe442310e2fd.jpg

cc53f5ef57729601e84096dd3f9d2ad232e.jpg

gradle 与maven相比有哪些优势

  1. 功能更强大,可以很方便的自定义任务;
  2. 添加依赖更简洁方便;
  3. 任务的执行流程更灵活,不像maven的生命周期那么固定.
  4. 可以编写 groovy 代码,我们知道 groovy 是非常灵活的。

参考

https://my.oschina.net/huangweiindex/blog/1844872
https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.DependencyHandler.html
https://docs.gradle.org/current/userguide/java_plugin.html#tab:configurations

我项目中完整的build.gradle 文件如下:

plugins {
    id 'java'
    id 'base'
    id 'org.springframework.boot' version '2.0.3.RELEASE'
}


group 'com.kunlunsoft'
version '1.0.0-SNAPSHOT'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

jar {
    manifest {
        attributes 'Main-Class': 'com.kunlunsoft.Application'
    }
}
task walk(description:'walk') {
    doLast {
        println 'walk...'
    }
}


repositories {
    maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile("org.springframework.boot:spring-boot-starter-test")
    //数据源
    compile 'org.springframework.boot:spring-boot-starter:1.5.14.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-web:1.5.14.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-data-redis:1.5.14.RELEASE'
    compile 'mysql:mysql-connector-java:5.1.38'

    compile project.fileTree(dir:'/Users/whuanghkl/code/myproject/target',include:['io0007-0.0.1-SNAPSHOT.jar'])
    compile 'com.google.guava:guava:23.0-rc1'
    compile 'org.apache.commons:commons-email:1.5'
    compile 'org.codehaus.jackson:jackson-mapper-lgpl:1.9.12'
    //redis
//    compile 'org.springframework.data:spring-data-redis:1.8.13.RELEASE'
    compile 'redis.clients:jedis:2.9.0'
    compile 'org.springframework.statemachine:spring-statemachine-core:1.2.0.RELEASE'
    compile 'com.alibaba:fastjson:1.2.47'

//配置mybatis
    compile "org.mybatis.spring.boot:mybatis-spring-boot-starter:1.1.1"

    compile 'org.springframework.boot:spring-boot-gradle-plugin:1.5.14.RELEASE'
//    compile 'org.springframework:springloaded:1.5.14.RELEASE'
}
原文链接:https://yq.aliyun.com/articles/754130
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章