Gradle构建Java项目

时间:2023-01-13 07:26:36

前提:已经安装好Gradle

一.创建目录结构

在项目主目录下,创建以下子目录;在*nix系统下可以使用命令:

mkdir -p src/main/java/hello
vim src/main/java/hello/HelloWorld.java
vim src/main/java/hello/Greeter.java

src/main/java/hello/HelloWorld.java的源代码:

package hello;

public class HelloWorld {
  public static void main(String[] args) {
    Greeter greeter = new Greeter();
    System.out.println(greeter.sayHello());
  }
}

src/main/java/hello/Greeter.java的源代码:

package hello;

public class Greeter {
  public String sayHello() {
    return "Hello world!";
  }
}

编写build.gradle(使用了gradle进行构建打包,依赖maven中心仓库,gradle wrapper等构建要素)

apply plugin: 'java'
repositories {
    mavenLocal()
    mavenCentral()
}
dependencies {
    compile "joda-time:joda-time:2.2"
}
jar {
    baseName = 'gs-gradle'
    version =  '0.1.0'
}
task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}
apply plugin: 'application'
mainClassName = 'hello.HelloWorld'

构建:

gradle

参考:

http://www.importnew.com/15881.html