Ubuntu18.04 运行 Gradle 4.9

简介

Gradle是一个基于Apache Ant和Apache Maven的项目自动化构建工具, 使用一种基于Groovy的特定领域语言(domain-specific language DSL)来声明构建设置. 支持多种语言项目的构建, 包含 Android, C++, Groovy, Java, Javascript, Kotlin, Scala.

官网 https://gradle.org/. 下载地址 https://gradle.org/releases/ .

安装

和maven一样, 解压缩即可, 放至 /opt/gradle

运行

生成Java项目结构:

mkdir java-demo
cd java-demo/
/opt/gradle/latest/bin/gradle init --type java-application
tree

#生成的目录结构
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   └── java
    │       └── App.java
    └── test
        └── java
            └── AppTest.java

构建

直接执行

java-demo$ ./gradlew build
Downloading https://services.gradle.org/distributions/gradle-4.9-bin.zip
................^C

这个下载会很慢, 因为gradle-4.9-bin.zip实际已经下载过了, 但是也不能直接放到.gradle目录下去, 因为目录路径中有hash串

~/.gradle$ find ./ -name gradle-*.zip
./wrapper/dists/gradle-4.4-all/9br9xq1tocpiv8o6njlyu5op1/gradle-4.4-all.zip
./wrapper/dists/gradle-4.6-bin/4jp4stjndanmxuerzfseyb6wo/gradle-4.6-bin.zip
./wrapper/dists/gradle-4.9-bin/dv6yjbd5p4c085k06brhv9r3f/gradle-4.9-bin.zip
./wrapper/dists/gradle-4.1-all/bzyivzo6n839fup2jbap0tjew/gradle-4.1-all.zip

可以将文件放到项目的gradle/wrapper目录下, 并修改gradle-wrapper.properties, 修改下面这行

# distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-bin.zip
distributionUrl=gradle-4.9-bin.zip

然后就会直接从本地下载了

java-demo$ ./gradlew build
Downloading file:/home/milton/IdeaProjects/untitled3/java-demo/gradle/wrapper/gradle-4.9-bin.zip
........................................................................
Download https://jcenter.bintray.com/com/google/guava/guava/23.0/guava-23.0.pom
Download https://jcenter.bintray.com/com/google/guava/guava-parent/23.0/guava-parent-23.0.pom
Download https://jcenter.bintray.com/com/google/guava/guava/23.0/guava-23.0.jar

BUILD SUCCESSFUL in 39s
7 actionable tasks: 7 executed

Update: 这样处理之后, 如果再把gradle-wrapper.properties改回来, 再次运行时还会进行下载, 可以用另一种方法:

# 查看目录下的hash目录
~/.gradle/wrapper/dists/gradle-4.9-bin
~/.gradle/wrapper/dists/gradle-4.9-bin$ ll
total 12
drwxr-xr-x 2 milton milton 4096 Jul 21 00:04 cuv0xpj2z1lkier1i57jl72tr/
drwxr-xr-x 3 milton milton 4096 Jul 20 23:43 dv6yjbd5p4c085k06brhv9r3f/
drwxr-xr-x 2 milton milton 4096 Jul 21 00:04 e9cinqnqvph59rr7g70qubb4t/
# 子目录下能看到未完成的下载
~/.gradle/wrapper/dists/gradle-4.9-bin$ ll cuv0xpj2z1lkier1i57jl72tr/
total 0
-rw-r--r-- 1 milton milton 0 Jul 20 23:42 gradle-4.9-bin.zip.lck
-rw-r--r-- 1 milton milton 0 Jul 20 23:42 gradle-4.9-bin.zip.part
# 将.part文件删除, 将完整的zip文件cp进来就可以了
...
# 运行完后再查看目录, 会变成这样的结构
~/.gradle/wrapper/dists/gradle-4.9-bin$ ll cuv0xpj2z11kier1i57j172tr/
total 74552
drwxr-xr-x 6 milton milton     4096 Jul 21 00:04 gradle-4.9/
-rw-r--r-- 1 milton milton 76334709 Jul 21 00:04 gradle-4.9-bin.zip
-rw-r--r-- 1 milton milton        0 Jul 20 23:06 gradle-4.9-bin.zip.lck
-rw-r--r-- 1 milton milton        0 Jul 21 00:04 gradle-4.9-bin.zip.ok

gradle的库文件, 都存放在 .gradle/caches/modules-2/files-2.1/ 目录下

还可以运行的其他命令

# 查看有那些构建方式
./gradlew tasks

# 运行, Runs this project as a JVM application
./gradlew run

# test - Runs the unit tests
./gradlew test

 

加载单独的jar包

在项目底下添加libs目录,将jar包仍进libs目录

build.gradle配置如下:

dependencies { compile files('lib/ojdbc-14.jar')}

加载整个目录的jar包

在自己的 Gradle 项目里建立一个名为 libs (这个名字可以自己定义,不一定非要叫这个名字)的文件夹,把自己本地的 jar 包拷贝到这个文件夹中。

build.gradle配置如下:

dependencies { compile fileTree(dir:'libs',include:['*.jar'])}

.

.

posted on 2018-07-20 23:51  Milton  阅读(615)  评论(0编辑  收藏  举报

导航