首页 > 代码库 > Gradle 教程说明 用户指南 第7章 快速构建Java工程

Gradle 教程说明 用户指南 第7章 快速构建Java工程

官网地址:http://www.gradle.org/docs/2.1/userguide/tutorial_java_projects.html


Using the Java plugin

A basic Java project  一个基础的java工程

使用java插件在build.gradle:
apply plugin: ‘java‘

Building the project 构建工程

这里使用gradle/samples/java/quickstart  示例工程。shell进该目录。
> gradle build
命令所 运行的任务有 (你可以使用gradle taskName 来单独运行以下的一种任务):
:compileJava
:processResources
:classes
:jar
:assemble
:compileTestJava
:processTestResources
:testClasses
:test
:check
:build

任务后提示 UP-TO-DATE 表示文件无更新,已为最新
任何执行成功后,会在当前目录下生成build目录,build目录下生成的部份目录有:
  libs/                                JAR file in the directory
  src/main/java/                 source code
  src/test/java/                   test source code
  src/main/resources/        will be include in the JAR file
  src/test/resources/          will be include in the classpath used to run the tests


其他一些 有用的任务:
clean 清除build目录
Deletes the build directory, removing all built files.    < gradle clean
assemble 编译打jar包
:compileJava
:processResources
:classes
:jar
:assemble
会调用这五个task。编译,处理资源,生成字节码,生成jar包      < gradle assemble


External dependencies 外部依赖

通常一个项目会引用一些外部的JAR包。那么就需要告诉Gradle在哪里可以找到它们。
在Gradle中,比如JAR文件,位于一个存储仓库(repository)。
存储仓库可以 获取依赖或发布它使之独立(比如jar包,javadoc文档)。
本例使用Maven仓库。
build.gradle:
repositories {
    mavenCentral()
}


本例需要添加一个.java源码编译时的依赖:commons-collections
和一个test源码编译时的依赖:junit
build.gradle:
dependencies {
    compile group: ‘commons-collections‘, name: ‘commons-collections‘, version: ‘3.2‘
    testCompile group: ‘junit‘, name: ‘junit‘, version: ‘4.+‘
}


Customizing the project 定制工程

Java插件添加了许多特性到您的项目。这些属性通常有默认值。如果他们不适合,很容易改变这些值。例子中,将为我们的Java项目指定版本号,以及我们的源代码的编译级别。在JAR-manifest中,我们还添加了一些属性。
build.gradle:
//java compile version
sourceCompatibility = 1.5
//project version
version = ‘1.0‘
jar {
    manifest {
        attributes ‘Implementation-Title‘: ‘Gradle Quickstart‘,
                   ‘Implementation-Version‘: version
    }
}


添加一个test 系统属性
build.gradle:
test {
    systemProperties ‘property‘: ‘value‘
}


Publishing the JAR file  发布JAR包

一般打JAR包需要指定一个位置。就是要告诉Gradle这个位置。例子中打包到本地目录。也可以发布到远程位置或多个位置
build.gradle:
uploadArchives {
    repositories {
       flatDir {
           dirs ‘repos‘  //publish to flat dir : repos
       }
    }
}


Creating an Eclipse project 创建一个Eclipse工程

要创建Eclipse特有的描述符文件,如.project文件,你需要添加另外一个插件到您的构建文件:
build.gradle:
apply plugin: ‘eclipse‘
> gradle eclipse
ls -al 发现有了 .project

同理,发现gradle java 也可以执行:
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:javadoc
结果是编译并生成了javadoc文档

总结
apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.5
version = '1.0'
jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
                   'Implementation-Version': version
    }
}

repositories {
    mavenCentral()
}

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

test {
    systemProperties 'property': 'value'
}

uploadArchives {
    repositories {
       flatDir {
           dirs 'repos'
       }
    }
}






Gradle 教程说明 用户指南 第7章 快速构建Java工程