gradle 是一个让构建自动化的工具,类似于maven,ant的功能.
使用gradle可以给java项目编译,单元测试,打包,或者生成可执行的jar包等
gradle依赖java环境,所以使用gradle前需要安装jdk 或jre
gradle的构建依赖于task, task可以指定与其他task之间的依赖关系 比如,有两个task,walk 和bike,如果指定walk依赖bike,那么 执行walk前会先执行bike.
task的来源有两种:
下面是一些常用的基本功能
<strong>task walk(description:'walk') { doLast { copy { into 'demo' exclude '**/.svn/**' from('README.md') } } }</strong>
<strong>task walk(description:'walk') { doLast { println 'walk...' project.delete { delete 'README.md' followSymlinks = true } } }</strong>
gradle <任务名>
./gradlew <任务名>
compile project.fileTree(dir:'/Users/whuanghkl/code/mygit/myproject/target',include:['io0007-0.0.1.jar'])
参考 https://docs.gradle.org/current/userguide/userguide.html
gradle中依赖的仓库有多种:
参考: https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.RepositoryHandler.html
我们可以选择 maven仓库:
repositories { mavenCentral() }
那么查询依赖就和maven一样了.
我的项目是spring boot,所以需要引入插件'org.springframework.boot'
id 'org.springframework.boot' version '2.0.3.RELEASE'
需要在build.gradle 文件中 指定可执行jar的main class :
jar { manifest { attributes 'Main-Class': 'com.kunlunsoft.Application' } }
执行任务bootJar 就可以生成可执行的jar包
plugins { id 'java' id 'base' // id 'application' id 'org.springframework.boot' version '2.0.3.RELEASE' } //mainClassName = "com.kunlunsoft.Application" 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...' } } 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://my.oschina.net/huangweiindex/blog/1844872
https://my.oschina.net/huangweiindex/blog/1842459