gradle安装使用

1.下载Gradle 3.2

2.Gradle配置到环境变量中GRADLE_HOME

3.eclipse插件安装,搜索gradle,然后安装Buildship Gradle Integration

4.windows—>Perferences下看到Gradle选项,然后在输入框里选择你的gradle的根目录

5.附件01

6.task hello << {

  println "hello gradle"

task表示定义一个任务,hello表示任务名, << 表示任务添加到任务列表的最后,大括号内是所要执行的任务的具体内容。

7.右键 - run as - run configuration-new可新建一个gradle任务执行

8.gradle新建一个简单项目 new gradle project,手动创建resources文件夹:build path-add folder-create new folder,java的源码目录:src/main/java

资源文件目录:src/main/resources

java的测试代码目录:src/test/java

测试的资源目录:src/test/resources

9.gradle构建web项目,建一个简单项目,右键项目-project facets-勾选Dynamic web module-further configure available-webapp+勾选

10.gradle构建多项目

构建一个java项目gradle-parent:然后在build.gradle文件中写入subprojects {repositories {dependencies{}}}

gradle-parent 根目录下添加两个文件夹 sub-project1 sub-project2 ,然后分别在这两个文件夹下添加空文件 build.gradle, settings.gradle 添加如下代码包,包含两个子工程
include 'sub-project1'
include 'sub-project2'
,刷新gradle-parent,即可生成两个子工程sub-project1sub-project2

11.settings.gradle在构建单项目时该配置文件可以省略,但是在构建多项目时必须加上该配置文件,该文件的作用是指定项目的包含关系include 'sub1',include 'sub2',rootProject.name = 'parent'

12.build.gradlesubprojects {} :一般配置在多项目中工程的根项目上,用来指定一些共有的配置和依赖,例如指定使用java插件,指定maven仓库的地址,制定一些所有项目都会用到的jar

dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
    testCompile group:'junit', name: 'junit', version '4.11'

}

13.子项目之间的依赖

对于多模块的项目,项目中的某些模块需要依赖于其他模块,在初始化阶段,Gradle为每个模块都创建了一个Project对象,并且可以通过模块的名字引用到该对象。在配置模块之间的依赖时,使用这种方式可以告诉Gradle当前模块依赖了哪些子模块。例如,在我们的项目中,sub-project1会依赖sub-project2子项目,就可以在sub-project1的构建脚本中加上如下代码

dependencies {
    compile(
             project(':sub-project2')
       )
}
14.发布项目到maven私服
apply plugin: 'maven'			  
repositories {
    maven {
	    url "http://localhost:8081/repository/maven-public/"
    }
}
uploadArchives {
    repositories {
	    mavenDeployer {
		   repository (url: "http://localhost:8081/repository/maven-snapshots") {
			    authentication(userName: nexusUsername, password: nexusPassword)
		    }
		    pom.version = "1.0-SNAPSHOT"
			pom.artifactId = "simple-project"
		    pom.groupId = "com.example"
	    }
    }
}
15.Gradle对依赖的配置比较简洁

dependencies {

    compile('org.springframework:spring-core:2.5.6')

    compile('org.springframework:spring-beans:2.5.6')

    compile('org.springframework:spring-context:2.5.6')

    compile('com.google.code.kaptcha:kaptcha:2.3:jdk15')

    testCompile('junit:junit:4.7')

}

16.Gradle也支持多模块构建。而在parentbuild.gradle中可以使用allprojectssubprojects代码块来分别定义里面的配置是应用于所有项目还是子项目。对于子模块的定义是放置在setttings.gradle文件中的。在gradle的设计当中,每个模块都是Project的对象实例。而在parent build.gradle中通过allprojectssubprojects可以对这些对象进行各种操作

17.

maven项目如何转换成gradle项目

进入win命令行窗口,进入到maven项目对应的跟目录下,然后执行以下命令:

gradle init --type pom 。执行完该命令之后 gradle 会根据 pom 文件生成对应的 build.gradle 文件

 

18.如何生成一个可执行的jar包
//清除上次的编译过的文件
task clear(type:Delete){
   delete 'build','target'
}
 
//定义main方法所在类的路径信息,为执行所需的jar包添加classpath
jar {  
    manifest {  
        attributes('Implementation-Title': 'Gradle',  
                'Implementation-Version': version,  
                'Created-By': 'jin',  
                'Main-Class': 'com.huawei.gradle.demo.consumer.DemoConsumer',  
                'Class-Path': configurations.compile.collect { 'lib/' + it.getName() }.join(' ')  
        )  
    }  
}  
 
//把JAR复制到目标目录
task release(type: Copy, dependsOn: [clear,jar]) {
from(configurations.runtime) {  
        into 'lib'  
    }  
    from('build/resources/main') {  
        into 'conf'  
    }  
    from('build/libs') {
        include '*.jar'
    }
    into ('target')
}

 

 

猜你喜欢

转载自wangelectronic.iteye.com/blog/2374796