顯示具有 gradle 標籤的文章。 顯示所有文章
顯示具有 gradle 標籤的文章。 顯示所有文章

Gradle - application

Run No arg main class
Java 
package examples;
public class TestMain {
    public static void main(String[] params) {
        System.out.println("hello");
    }
}
build.gradle
apply plugin: 'java'
apply plugin: 'application'
mainClassName= 'examples.TestMain'

repositories {
    mavenCentral()
}

dependencies {
    testCompile 'junit:junit:4.12'
}
Run
$ gradle run

> Task :run
hello

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.3/userguide/command_line_interface.html#sec:command_line_warnings


BUILD SUCCESSFUL in 952ms
3 actionable tasks: 3 executed

Run main class with hard code arg
Java
package examples;
public class TestMain {
    public static void main(String[] params) {
        System.out.println("hello " + params[0]);
    }
}
build.gradle
apply plugin: 'java'
apply plugin: 'application'
mainClassName= ‘examples.TestMain'

repositories {
    mavenCentral()
}

dependencies {
    testCompile 'junit:junit:4.12'
}

run.args = ['abc']
Run
apply plugin: 'java'
apply plugin: 'application'
mainClassName= 'examples.TestMain'

repositories {
    mavenCentral()
}

dependencies {
    testCompile 'junit:junit:4.12'
}

run.args = ['abc']

Run main class with command line arg
Java
package examples;
public class TestMain {
    public static void main(String[] params) {
        System.out.println("hello " + params[0]);
    }
}
build.gradle
apply plugin: 'java'
apply plugin: 'application'
mainClassName= 'examples.TestMain'


repositories {
    mavenCentral()
}


dependencies {
    testCompile 'junit:junit:4.12'
}
run.args = [project.args] //simply args can work as well
Run
$ gradle clean run -Pargs=ABC

> Task :run
hello ABC

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.3/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 760ms
4 actionable tasks: 4 executed







Gradle java project - code and test

source code
Hello.java
package examples;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class Hello {


    public String hello() throws IOException {
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("hello.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder s = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
            s.append(line);
        }
        return s.toString();
    }
    
}

prepare build.gradle and apply java plugin
apply plugin: ‘java'

compile java
$ gradle compileJava

BUILD SUCCESSFUL in 620ms
1 actionable task: 1 executed

Clean classes
$ gradle clean

BUILD SUCCESSFUL in 564ms
1 actionable task: 1 executed

Build code and run test
Add junit dependency
apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    testCompile 'junit:junit:4.12'
}

Add test code
package examples;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.junit.Assert;
import org.junit.Test;

public class HelloTest {

    @Test
    public void hello() throws IOException, URISyntaxException {
        Hello h = new Hello();
        String hello = h.hello();
        String hellotest = new String(Files.readAllBytes(Paths.get(Thread.currentThread().getContextClassLoader().getResource("hellotest.txt").toURI())));
        Assert.assertEquals(hellotest, hello);
    }
   
}

Compile and Test
$ gradle build


Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.3/userguide/command_line_interface.html#sec:command_line_warnings


BUILD SUCCESSFUL in 1s
6 actionable tasks: 6 executed

Test Report
Open in browser: build/reports/tests/test/index.html 

Gradle helloworld task


Edit build.gradle
task helloworld {
    println "Hello world"
}

List tasks and find helloworld
$gradle tasks --all

...
Other tasks
-----------
helloworld
prepareKotlinBuildScriptModel

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed

Run task
$ gradle helloworld
> Configure project :
Hello world

BUILD SUCCESSFUL in 931ms


Gradle Helloworld - Main Class

Reference
Thanks 良葛格, his explanation is so simple and good to read and practice!!

Class
package examples;
public class HelloMain {
    public static void main(String[] params) {
        System.out.println("Hello world:" + params[0]);
    }
}

build.gradle
apply plugin:'java'
apply plugin:'application'

mainClassName='examples.HelloMain'

run {
    args username
}

run

$ gradle run -Pusername=Isaac

> Task :run
Hello world:Isaac

BUILD SUCCESSFUL in 721ms
3 actionable tasks: 1 executed, 2 up-to-date

別名演算法 Alias Method

 題目 每個伺服器支援不同的 TPM (transaction per minute) 當 request 來的時候, 系統需要馬上根據 TPM 的能力隨機找到一個適合的 server. 雖然稱為 "隨機", 但還是需要有 TPM 作為權重. 解法 別名演算法...