gradle基本使用

来源:互联网 发布:linux kill -9怎么用 编辑:程序博客网 时间:2024/05/23 17:57

相信使用做安卓开发的对gradle并不陌生,gradle是个帮我们build的工具。他与make和ant不同的是它是基于一种语言,而不是配置。这也就使得它拥有更大的灵活性。闲话少说,下面说说基本用法。
1下载gradle 并解压,然后在环境变量中加入它bin文件夹地址。
2 在命令行中输入gradle测试是否可用。如果不可用检查环境变量和是否装了jdk,gradle是依赖jdk的。
3 在当前目录创建一个java项目,命令行输入gradle init –type=java-helloWord。
4 输入gradle tasks看可用运行的命令,你会发现好多命令及解释,当然也包括上面的init.

Build tasks-----------assemble - Assembles the outputs of this project.build - Assembles and tests this project.buildDependents - Assembles and tests this project and all projects that dependon it.buildNeeded - Assembles and tests this project and all projects it depends on.classes - Assembles main classes.clean - Deletes the build directory.jar - Assembles a jar archive containing the main classes.testClasses - Assembles test classes.Build Setup tasks-----------------init - Initializes a new Gradle build. [incubating]wrapper - Generates Gradle wrapper files. [incubating]

5 运行其中的gradle test 会出现成功,如果遇到:锘縤mport org.junit.Test;这是因为Windows平台下Unicode文件(UTF-8等)头部插入BOM首字符,只要把文件另存为ansi格式就会成功了。

:classes:compileTestJavaD:\gradle\init\src\test\java\HelloWordTest.java:1: 错误: 需要class, interface或enum锘縤mport org.junit.Test;^1 个错误:compileTestJava FAILEDFAILURE: Build failed with an exception.

6 我们改下HelloWord.java中为someLibraryMethod方法为return false后运行,则会报错。

/* * This Java source file was auto generated by running 'gradle buildInit --type java-helloWord' * by 'Administrator' at '16-12-17 下午2:41' with Gradle 3.2.1 * * @author Administrator, @date 16-12-17 下午2:41 */public class HelloWord {    public boolean someLibraryMethod() {        return false;    }}

报错如下,错误信息在build/reports/tests/test中的html文档,非常容易查看。

1 test completed, 1 failed:test FAILEDFAILURE: Build failed with an exception.* What went wrong:Execution failed for task ':test'.> There were failing tests. See the report at: file:///D:/gradle/init/build/reports/tests/test/index.html* Try:Run with --stacktrace option to get the stack trace. Run with --info or --debugoption to get more log output.BUILD FAILED

7.因为工具是编译,我们要生成最后当然是生成jar包。在build.gradle中加入如下代码

jar {    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }    manifest {        attributes 'Main-Class': 'helloGradle.HelloGradle'    }}

8.运行gradle assemble 后会在build\libs生成当前工程的jar包。
最后提供深入学习连接

1 0