gradle

김가빈·2023년 7월 27일
0

CI/CD

목록 보기
1/1

java plugin

  1. package into jar file

    • adds a task jar(./gradlew jar)
    • adds compiled classes and resources into jar achieve
    • name of jar : project-name-version.jar
  2. easily run tests

    • adds a task test(./gradlew test)
    • compiles tests, processes resources, run tests
    • creates a test report!
  3. define dependencies

    • use the dependencies section of build.gradle


    • dependency configuration

      implementation
      testImplementation

    • dependency configuration used to generate classpath


Gradle Java project layout

  • src/main/java

    • build/classes/java/main
  • src/main/resources

    • build/resources/main
  • src/test/java

    • build/classes/java/test
  • src/test/resources

    • build/resources/test
  • everythings is configurable



example Of gradle with java

  • eclipse -> new -> file -> gradle project


  • structure of gradle project

  • example source code

    package com.tomgregory.languageapp;
    
     import java.io.BufferedReader;
     import java.io.IOException;
     import java.io.InputStream;
     import java.io.InputStreamReader;
     import java.nio.charset.StandardCharsets;
    
     public class SayHello {
       public static void main(String[] args) throws IOException {
         String language = args[0];
         
         InputStream resourceStream = SayHello.class.getClassLoader().getResourceAsStream(language + ".txt");
         assert resourceStream != null;
         BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(resourceStream, StandardCharsets.UTF_8));
         
         System.out.println(bufferedInputStream.readLine());
       }
       
     }
  • cd to project folder

      C:\workspace\scheduler\get-going-with-gradle
  • show gradlew tasks

        gradlew tasks
        gradlew tasks --all
  • compileing java project

        gradlew compileJava
  • build resources files

       gradlew processResources
  • make jar acheive

      gradlew jar
  • cd to jar path

      cd C:\workspace\scheduler\get-going-with-gradle\lib\build\libs
  • run jar

      java -jar lib.jar en
      java -jar lib.jar es


example gradle test in java

  • gradle make easy to test

  • cd to project path

      cd C:\workspace\scheduler\get-going-with-gradle
  • start test

      gradlew test


groovy essentials for gradle

  1. you can write java in groovy
  • groovy is a scripting language running in the JVM
  • groovy extends java, making development quicker
  • gradle uses groovy language features in build.gradle
  • BUT can write Java code in Groovy(and build.gradle)

  1. brackets are optional
  • if the function has one more more argument

  • println 'Bracket-free' same as println('Bracket-free')

  • Gradle build script makes use of the Groovy feature

        dependencies {
            testImplementation 'org.junit.jupiter:junit-jupiter:5.6.3'
        }
  • it's same to this

      void dependencies(Closure var1);
  • and this

      dependencies ({
        testImplementation 'org.junit.jupiter:junit-jupiter:5.6.3'
      })
  • dependencies is method


  1. supports closures
  • function as a variable
    def amazingCalculator = {a, b -> a + b }
    println amazingCalculator(2, 3) // print5


the task graph

  • tasks can depend other tasks

  • build aggregates assemble and check tasks
  • you only need remember to run a single task, build

  1. java plugin task graph
  • it's just like tree graph

  • can choose specific task to save time
    • check(just need to test project)
    • assemble(just need to build project)
  • example
gradlew clean 
gradlew assemble
gradlew check
gradlew clean
gradlew build
profile
신입 웹개발자입니다.

0개의 댓글