gradle基本使用

12 篇文章 0 订阅

相信使用做安卓开发的对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 depend
on 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
:compileTestJava
D:\gradle\init\src\test\java\HelloWordTest.java:1: 错误: 需要class, interface或enu
m
锘縤mport org.junit.Test;
^
1 个错误
:compileTestJava FAILED

FAILURE: 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 FAILED

FAILURE: 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/repo
rts/tests/test/index.html

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug
option 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值