『阿男的Gradle学习笔记』 *02*使用Gradle编译Java项目
Java代码目录结构默认遵循maven的规范:
$ tree
.
├── build.gradle
└── src
└── main
└── java
└── org
└── weli
└── HelloWorld.java
5 directories, 2 files
HelloWorld.java
代码的内容:
package org.weli;
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello, world!");
}
}
`build.gradle的内容:
apply plugin: 'java'
'java' plugin会自动引入一些默认task支持maven目录规范。使用下面的命令编译项目:
$ gradle build
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
BUILD SUCCESSFUL
Total time: 3.639 secs
编译完成后代码目录内容如下:
$ tree
.
├── build
│ ├── classes
│ │ └── main
│ │ └── org
│ │ └── weli
│ │ └── HelloWorld.class
│ ├── dependency-cache
│ ├── libs
│ │ └── 02.jar
│ └── tmp
│ ├── compileJava
│ └── jar
│ └── MANIFEST.MF
├── build.gradle
└── src
└── main
└── java
└── org
└── weli
└── HelloWorld.java
15 directories, 5 files
执行编译后的代码:
$ java -cp build/classes/main/ org.weli.HelloWorld
Hello, world!