Gradle

Gradle

CLI / shell command: gradle

自动化构建系统。

Installation & Config

安装

https://docs.gradle.org/current/userguide/installation.html

Windows:

1. install chocolatey package manager (https://chocolatey.org/)

cmd:

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

2. install gradle via chocklatey

choco install gradle

配置

禁用 Gradle daemon (这个傻逼 daemon 总是有问题)

// add org.gradle.daemon=false to the <GRADLE_USER_HOME>/gradle.properties file
// <GRADLE_USER_HOME> defaults to <USER_HOME>/.gradle

Quick way:

Windows cmd:

(if not exist "%USERPROFILE%/.gradle" mkdir "%USERPROFILE%/.gradle") && (echo. >> "%USERPROFILE%/.gradle/gradle.properties" && echo org.gradle.daemon=false >> "%USERPROFILE%/.gradle/gradle.properties")

Linux shell:

mkdir -p ~/.gradle && echo "org.gradle.daemon=false" >> ~/.gradle/gradle.properties

Build Java Quick Start

** 除非迫不得已,强烈不建议使用傻逼 Java 开发任何项目。 **

用 Gradle 构建傻逼 Java 的项目。

Folder structure

<project_root>
|- build.gradle
|- src/main/java/
|- src/main/java/fuck.java/ # <package_name>
|- src/main/java/fuck.java/FuckYouJava.java
|- libs/
|- libs/*.jar

build.gradle

apply plugin: 'java'
apply plugin: 'application'

mainClassName='fuck.java.FuckYouJava'

repositories {
  mavenCentral()
}

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

defaultTasks 'run'

FuckYouJava.java

src/main/java/fuck.java/FuckYouJava.java

package fuck.java;

class FuckYouJava{
  public static void main(String[] args) { // 傻逼 Java 里函数是二等公民,什么东西都要包裹个去你妈的类
    try {
      System.out.println("Fuck You Java!");
    } catch( Exception e) { // 傻逼 Java 默认异常退出时仍然返回 0 exit code,纯属弱智。
      e.printStackTrace();
      System.exit(1);
    }
  }
}

然后在项目根目录运行 gradle 即可。

build fatJar

task fatJar(type: Jar) {
    manifest {
        attributes 'Main-Class': 'com.curso.online.gradle.Main'
    }
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

Sublime Text Integration

Sublime Text menus -> Tools -> Build System -> New Build System

Edit the new file with below contents and save it as "java_gradle.sublime-build"
{
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": ["source.java"],
    "shell_cmd": "gradle -Pmain=\"${file_base_name}\""
}

Usage:

In sublime Text, Ctrl + b shortcut to trigger build system.


Last update: 2018-04-08 08:50:03 UTC