GradleAndroid Studio

Gradle构建Java应用程序

2017-07-21  本文已影响13人  Android那些事儿

文章摘要
1、运行init,创建
2、执行创建
3、运行应用程序


一、运行init任务

要运行该init任务,请从命令提示符运行以下命令:

$ gradle init --type <name>

name来自如下:

$ mkdir java-demo
$ cd java-demo

2、运行Init任务

$ gradle init --type java-application
Starting a Gradle Daemon (subsequent builds will be faster)
:wrapper
:init

BUILD SUCCESSFUL

该init任务运行的wrapper任务首先,其产生gradlew和gradlew.bat包装脚本。然后它创建具有以下结构的新项目:

查看工程生成文件:

.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   └── java
    │       └── App.java
    └── test
        └── java
            └── AppTest.java

7 directories, 8 files

3、查看配置生成的文件

settings.gradle,其中就一行:

rootProject.name = 'java-demo'

这将分配根项目的名称java-demo,这是默认值。

IFEI:java-demo ifei$ cat 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/4.0.1/userguide/tutorial_java_projects.html
 */

// Apply the java plugin to add support for Java
apply plugin: 'java'

// Apply the application plugin to add support for building an application
apply plugin: 'application'

// 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 {
    // This dependency is found on compile classpath of this component and consumers.
    compile 'com.google.guava:guava:21.0'

    // Use JUnit test framework
    testCompile 'junit:junit:4.12'
}

// Define the main class for the application
mainClassName = 'App'

构建文件添加java和application插件。前者支持Java项目。后者允许您将一个类指定为具有main方法,该方法可以由命令行中的构建执行。在演示中,main类的名称是App。

该文件src/main/java/App.java显示在此处:

示例2.生成的App.java类

IFEI:java-demo ifei$ cat src/main/java/App.java 
/*
 * This Java source file was generated by the Gradle 'init' task.
 */
public class App {
    public String getGreeting() {
        return "Hello world.";
    }

    public static void main(String[] args) {
        System.out.println(new App().getGreeting());
    }
}

测试类,src/test/java/AppTest.java如下所示:

示例3. JUnit测试,AppTest:

IFEI:java-demo ifei$ cat src/test/java/AppTest.java 
/*
 * This Java source file was generated by the Gradle 'init' task.
 */
import org.junit.Test;
import static org.junit.Assert.*;

public class AppTest {
    @Test public void testAppHasAGreeting() {
        App classUnderTest = new App();
        assertNotNull("app should have a greeting", classUnderTest.getGreeting());
    }
}

生成的测试类有一个使用JUnit @Test注释注释的测试。该测试实例化App该类,调用该getGreeting方法,并检查返回的值不为null。


4、执行构建

要构建项目,请运行build命令。您可以使用常规gradle命令,但是当项目包含一个包装器脚本时,它被认为是使用它的好形式。

$ ./gradlew build
:compileJava
// Download of Guava if not already cached...
:processResources UP-TO-DATE
:classes
:jar
:startScripts
:distTar
:distZip
:assemble
:compileTestJava
// Download of JUnit if not already cached...
:processTestResources UP-TO-DATE
:testClasses
:test
:check
:build

BUILD SUCCESSFUL

第一次运行包装器脚本时gradlew,可能会有一个延迟,而该版本gradle被下载并存储在您的~/.gradle/wrapper/dists文件夹中。

第一次运行构建时,Gradle会检查您的缓存中是否已经有Guava和JUnit库~/.gradle。如果没有,库将被下载并存储在那里。下次运行构建时,将使用缓存的版本。该build任务编译的类,运行测试,并生成测试报告。

您可以通过打开位于的HTML输出文件来查看测试报告build/reports/tests/test/index.html。

示例报告如下所示:

image.png

5、运行应用程序

因为Gradle构建使用了Application plugin,所以可以从命令行运行应用程序。首先,使用tasks任务来看看插件添加了哪些任务。

$ ./gradlew tasks
:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Application tasks
-----------------
run - Runs this project as a JVM application

// ... many other tasks ...

该run任务告诉Gradle main在分配给该mainClassName属性的类中执行该:

$ ./gradlew run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run
Hello world.

BUILD SUCCESSFUL
上一篇 下一篇

猜你喜欢

热点阅读