Giter Site home page Giter Site logo

hugb_hello_world's 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());

hugb_hello_world's People

Contributors

joningis avatar arnlaugsson avatar

Watchers

Eyrún Magnúsdóttir avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.