Gradle实战-2

1. Java和eclipse插件

  • 初始化时创建java项目目录结构
1
2
3
4
5
6
7
$ gradle init --type java-library
:wrapper
:init

BUILD SUCCESSFUL

Total time: 0.666 secs
  • 默认的build.gradle的内容,默认已添加java插件。Java插件可以引入源代码约定,默认情况下到src/main/java下查找代码;并提供buildcompileJava等task
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// build.gradle
/*
* This build file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/3.3/userguide/tutorial_java_projects.html
*/

// Apply the java plugin to add support for Java
apply plugin: 'java'
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}

dependencies {
// The production code uses Guava
compile 'com.google.guava:guava:20.0'

// Use JUnit test framework
testCompile 'junit:junit:4.12'
}
  • 查看当前.classpath
1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry path="bin" kind="output"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
<classpathentry sourcepath="/Users/xyq/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/20.0/9c8493c7991464839b612d7547d6c263adf08f75/guava-20.0-sources.jar" kind="lib" path="/Users/xyq/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/20.0/89507701249388e1ed5ddcf8c41f4ce1be7831ef/guava-20.0.jar"/>
<classpathentry sourcepath="/Users/xyq/.gradle/caches/modules-2/files-2.1/junit/junit/4.12/a6c32b40bf3d76eca54e3c601e5d1470c86fcdfa/junit-4.12-sources.jar" kind="lib" path="/Users/xyq/.gradle/caches/modules-2/files-2.1/junit/junit/4.12/2973d150c0dc1fefe998f834810d68f278ea58ec/junit-4.12.jar"/>
<classpathentry sourcepath="/Users/xyq/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/1dc37250fbc78e23a65a67fbbaf71d2e9cbc3c0b/hamcrest-core-1.3-sources.jar" kind="lib" path="/Users/xyq/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar"/>
</classpath>
  • 添加eclipse插件
1
apply plugin: 'eclipse'
  • 执行IDE支持,如关联Referenced Libraries等
1
2
3
4
5
6
7
8
9
$ gradle eclipse
:eclipseClasspath
:eclipseJdt
:eclipseProject
:eclipse

BUILD SUCCESSFUL

Total time: 0.664 secs

2. 构建项目

  • 新建入口类
1
2
3
4
5
6
7
package com;

public class Main {
public static void main(String[] args) {
System.out.println("hello gradle");
}
}
  • 项目根目录下运行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ gradle build
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build UP-TO-DATE

BUILD SUCCESSFUL

Total time: 0.692 secs

UP-TO-DATE标记了被跳过的任务。Gradle的增量式构建支持自动鉴别不需要被执行的任务,如compileTestJava,因为当前项目里没有添加单元测试类。构建生成的class、jar包、单测结果等文件在build/目录下。

3. 运行项目

1
2
$ java -cp build/classes/main/ com.Main
hello gradle

4. 修改项目和插件属性

build.gradle中添加

1
2
3
4
5
6
7
8
9
10
//定义项目版本,
version = 0.1
//定义java兼容版本
sourceCompatibility = 1.8
//Jar包main方法位置
jar {
manifest {
attributes 'Main-class': 'com.Main'
}
}

运行gradle build后,build/lib/下生成LearnGradle-0.1.jar,接着

1
2
$ java -jar build/libs/LearnGradle-0.1.jar
hello gradle

5. 可自定义源代码目录结构

  • 示例
1
2
3
4
5
6
7
8
9
10
11
12
13
sourceSets {
main {
java {
srcDirs = ['src']
}
}
test {
java {
srcDirs = ['test']
}
}
}
buildDir = 'out'

6. 定义中央仓库

1
2
3
repositories {
mavenCentral()
}

7. 引入外部依赖

gradle中,依赖由configuration分组

1
2
3
4
5
dependencies {
compile group: 'org.apache.commons', name: 'common3-lang3', version: '3.5'
compile 'com.google.guava:guava:20.0'
testCompile 'junit:junit:4.12'
}

默认依赖的jar包在用户目录的.gradle/cache/下。可以通过如下的task将项目依赖的jar包复制到项目目录中

1
2
3
4
5
6
task downloadLib(type: Copy) {
delete fileTree(dir: 'lib')
from configurations.compile
from configurations.testCompile
into "lib"
}

8. 构建简单的Java Web项目

  • build.gradle添加gretty插件
1
2
3
4
5
6
7
apply plugin: 'war'
apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'

//grtty config
gretty {
httpPort = 8080
}
  • 构建web项目目录结构
1
$ mkdir -p src/main/webapp/WEB-INF
  • web页面

webapp/创建一个静态HTML页面index.html,这个页面是之后访问的目标页面

1
2
3
4
5
6
7
8
<html>
<head>
<title>Gradle Java Web Page</title>
</head>
<body>
<p>Hello Gradle Java Web!</p>
</body>
</html>
  • 编译

运行gradle buildbuild/libs/目录下生成了LearnGradle-0.1.war文件,查看该war包内容

1
2
3
4
5
6
7
8
9
10
11
$ jar -tf build/libs/LearnGradle-0.1.war
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/classes/
WEB-INF/classes/com/
WEB-INF/classes/com/Main.class
WEB-INF/lib/
WEB-INF/lib/commons-lang3-3.5.jar
WEB-INF/lib/guava-20.0.jar
index.html
  • 运行
1
2
3
4
5
6
7
8
9
10
11
12
13
$ gradle jettyStart
:prepareInplaceWebAppFolder UP-TO-DATE
:createInplaceWebAppFolder UP-TO-DATE
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:prepareInplaceWebAppClasses UP-TO-DATE
:prepareInplaceWebApp UP-TO-DATE
:jettyStart
00:35:12 INFO Jetty 9.2.15.v20160210 started and listening on port 8080
00:35:12 INFO LearnGradle runs at:
00:35:12 INFO http://localhost:8080/LearnGradle
Run 'gradle appStop' to stop the server.

在浏览器中打开http://localhost:8080/LearnGradle/index.html即可看到Hello Gradle Java Web!

9. 包装器wrapper和跨平台

  • 自动创建

通过gradle init --type java-library初始化的项目已包含以下文件:gradlewgradlew.batgradle/wrapper/gradle-wrapper.jargradle/wrapper/gradle-wrapper.properties。Linix / Windows平台可以在下载代码后通过gradlew / gradlew.bat在命令行操作项目

  • 手动创建

build.gradle添加任务

1
2
3
task wrapper (type: Wrapper) {
gradleVersion = '3.3'
}
1
2
3
4
5
6
$ gradle wrapper
:wrapper

BUILD SUCCESSFUL

Total time: 0.66 secs