• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

joningis/hugb_hello_world: Sample program used to show students Gradle in action ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

joningis/hugb_hello_world

开源软件地址(OpenSource Url):

https://github.com/joningis/hugb_hello_world

开源编程语言(OpenSource Language):

Java 95.6%

开源软件介绍(OpenSource Introduction):

hugb_hello_world

(T-303-HUGB, Hugbúnaðarfræði, 2016-3)

Sample program used to show students Gradle in action.

What was done

####NOTE: I used gradle 3.1 in the lecture.

Create project

  • Git clone hugb_hello_world project
  • Run gradle init --type java-library to create basic folder structure.
  • Run gradle check to check if everything is working as it should.

Project content

  • build.gradle (The gradle build file that we use)
  • gradle (Folder containing the gradle wrapper)
  • gradlew (Linux/Unix script to run the gradle wrapper)
  • gradlew.bat (Windows script to run the gradle wrapper)
  • settings.gradle (Settings file for gradle, not used in our example, can for example contain definitions if we have multiple projects)
  • src (Source folder for our code)

Show basic gradle tasks

  • Checkout list of gradle tasks (./gradlew tasks if we use the wrapper, gradle tasks if using the gradle install on the machine)

Create basic code

  • Navigate to src/main/java and delete Library.java (just a demo file that we do not use)
  • Navigate to scr/test/java and delete LibraryTest.java.
  • Create folders for code under src/main/java (mkdir -p com/bangsapabbi/helloworld)
  • Navigate to the new folder and create HelloWorld.java
package com.bangsapabbi.helloworld;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello world");
    }
}
  • Check if everything builds.
  • Add the application plugin* to run the program.
apply plugin: 'application'

mainClassName = "com.bangsapabbi.helloworld.HelloWorld"
  • Extract the "Hello World!" message to World class to be able to show basic unit tests.
package com.bangsapabbi.helloworld;

public class HelloWorld {
    public static void main(String[] args) {
        World world = new World();
        System.out.println(world.greet());
    }
}
package com.bangsapabbi.helloworld;

public class World {
    public String greet() {
        return "Hello world!";
    }
}

Testing

  • Navigate to src/test/java
  • Create folder structure (mkdir -p com/bangsapabbi/helloworld)
  • Add WorldTest.java file.
package com.bangsapabbi.helloworld;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class WorldTest {

    @Test
    public void greetResultsInHello() {
        World world = new World();
        assertEquals("Hello world!", world.greet());
    }

}
  • Run tests using gradle test
  • Change test and see the build fail.
  • Open build/reports/test to view detailed test report.

bin folder

  • Create bin folder
  • Create clean script
  • chmod +x on the clean file.
#!/bin/bash

./gradlew clean

Gson (Not part of the project in git)

  • Goto http://search.maven.org/
  • Search for gson
  • Click the version number
  • Here we can find the dependency string that we need.
compile 'com.google.code.gson:gson:2.3'

Refactoring World class to include gson output.

package com.bangsapabbi.helloworld;

import com.google.gson.Gson;

public class World {

    private String greeting = "Hello world!";

    public String greet() {
        return greeting;
    }

    public String jsonGreet() {
        return new Gson().toJson(this);
    }
}
  • Add the following line to HelloWorld.java to output json.
System.out.println(world.jsonGreet());




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap