DEV Community

Weerasak Chongnguluam
Weerasak Chongnguluam

Posted on

2 2

สร้าง java project เล็กๆด้วย Gradle

ถ้าอยากรันโค้ด Java และมี dependencies ต่างๆด้วย ตอนนี้ทำได้ง่ายๆด้วย Gradle ตัวอย่างการสร้างโปรเจ็ค Java ด้วย gradle ง่ายๆทำได้ดังนี้

สร้าง directory ใหม่แล้ว cd เข้าไป

> mkdir demo-java
> cd demo-java
Enter fullscreen mode Exit fullscreen mode

จากนั้นสั่ง gradle init แล้วเลือก option ตามนี้

> gradle init

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 2

Select implementation language:
  1: C++
  2: Groovy
  3: Java
  4: Kotlin
  5: Scala
  6: Swift
Enter selection (default: Java) [1..6] 

Split functionality across multiple subprojects?:
  1: no - only one application project
  2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 

Select test framework:
  1: JUnit 4
  2: TestNG
  3: Spock
  4: JUnit Jupiter
Enter selection (default: JUnit 4) [1..4] 

Project name (default: demo-java): 
Source package (default: demo.java): 

> Task :init
Get more help with your project: https://docs.gradle.org/6.8.3/samples/sample_building_java_applications.html

BUILD SUCCESSFUL in 22s
2 actionable tasks: 2 executed
Enter fullscreen mode Exit fullscreen mode

โครงสร้าง directory และไฟล์ต่างๆจะได้ตามนี้

.
├── app
│   ├── build
│   │   ├── classes
│   │   │   └── java
│   │   │       └── main
│   │   │           └── demo
│   │   │               └── java
│   │   │                   └── App.class
│   │   ├── generated
│   │   │   └── sources
│   │   │       ├── annotationProcessor
│   │   │       │   └── java
│   │   │       │       └── main
│   │   │       └── headers
│   │   │           └── java
│   │   │               └── main
│   │   └── tmp
│   │       └── compileJava
│   │           └── source-classes-mapping.txt
│   ├── build.gradle
│   └── src
│       ├── main
│       │   ├── java
│       │   │   └── demo
│       │   │       └── java
│       │   │           └── App.java
│       │   └── resources
│       └── test
│           ├── java
│           │   └── demo
│           │       └── java
│           │           └── AppTest.java
│           └── resources
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
Enter fullscreen mode Exit fullscreen mode

โค้ดที่เราจะเขียนคือเขียนใน app/src/main/java/demo/java/App.java ซึ่ง gradle จะเจน example ให้แบบนี้

/*
 * This Java source file was generated by the Gradle 'init' task.
 */
package demo.java;

public class App {
    public String getGreeting() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        System.out.println(new App().getGreeting());
    }
}
Enter fullscreen mode Exit fullscreen mode

เราก็แก้โค้ดเล่นในนี้แล้วสั่งรันได้โดย gradle run

> gradle run

> Task :app:run
Hello World!

Enter fullscreen mode Exit fullscreen mode

ทีนี้ถ้าเราจำเป็นต้องใช้ package อื่นที่ไม่ใช้ standard library ของ Java เราสามารถหา dependencies ได้จาก https://mvnrepository.com/ เช่นอยากได้ Apache Commons Codec เพื่อใช้งาน Hex.encodeHexString ก็ค้น Apache Commons Codec ในเว็บ https://mvnrepository.com/ แล้วเลือก version จากนั้นเลือก tab Gradle แล้วก็อปปี้ config ประมาณนี้

// https://mvnrepository.com/artifact/commons-codec/commons-codec
implementation group: 'commons-codec', name: 'commons-codec', version: '1.15'

Enter fullscreen mode Exit fullscreen mode

แล้วเอาไปแก้ config ที่ไฟล์ app/build.gradle โดยเพิ่มใน block dependencies {} แบบนี้

dependencies {
    // Use JUnit test framework.
    testImplementation 'junit:junit:4.13'

    // This dependency is used by the application.
    implementation 'com.google.guava:guava:29.0-jre'

    implementation group: 'commons-codec', name: 'commons-codec', version: '1.15'
}
Enter fullscreen mode Exit fullscreen mode

เท่านี้เราก็สามารถใช้ Hex.encodeHexString ได้แล้วเช่นแก้โค้ดใน App.java เป็นแบบนี้

package demo.java;

import org.apache.commons.codec.binary.Hex;


public class App {
    public static void main(String[] args) {
        System.out.println(Hex.encodeHexString("Hello World".getBytes()));
    }
}
Enter fullscreen mode Exit fullscreen mode

สั่งรันด้วย gradle run ก็จะได้แบบนี้

> gradle run

> Task :app:run
48656c6c6f20576f726c64

BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed
Enter fullscreen mode Exit fullscreen mode

Ref: https://docs.gradle.org/current/samples/sample_building_java_applications.html

ขอฝาก Buy Me a Coffee

สำหรับท่านใดที่อ่านแล้วชอบโพสต์ต่างๆของผมที่นี่ ต้องการสนับสนุนค่ากาแฟเล็กๆน้อยๆ สามารถสนับสนุนผมได้ผ่านทาง Buy Me a Coffee คลิ๊กที่รูปด้านล่างนี้ได้เลยครับ

Buy Me A Coffee

ส่วนท่านใดไม่สะดวกใช้บัตรเครดิต หรือ Paypal สามารถสนับสนุนผมได้ผ่านทาง PromptPay โดยดู QR Code ได้จากโพสต์ที่พินเอาไว้ได้ที่ Page DevDose ครับ https://web.facebook.com/devdoseth

ขอบคุณครับ 🙏

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay