본문 바로가기

리눅스

[리눅스] 그래들(Gradle)로 스프링 부트 빌드(Build)하기

반응형

그래들(Gradle)로 스프링 부트 빌드(Build)하기

Spring Boot 프로젝트 생성

https://scbyun.com/1241

그래들(Gradle)로 스프링 부트 빌드(Build)하기

설정

  • build.gradle 파일

plugins {
	id 'org.springframework.boot' version '2.7.0'
	id 'io.spring.dependency-management' version '1.0.11.RELEASE'
	id 'java'
	id 'war'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	compileOnly 'org.projectlombok:lombok'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	annotationProcessor 'org.projectlombok:lombok'
	providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
	useJUnitPlatform()
}

프로젝트 경로로 이동합니다.

  • 프로젝트 폴더
ls -l
total 48
-rw-r--r--  1 staff  staff  1150 12 25 01:55 HELP.md
drwxr-xr-x  4 staff  staff   128 12 25 01:55 bin
-rw-r--r--  1 staff  staff   736 12 25 01:55 build.gradle
drwxr-xr-x  3 staff  staff    96 12 25 01:55 gradle
-rwxr-xr-x  1 staff  staff  8070 12 25 01:55 gradlew
-rw-r--r--  1 staff  staff  2763 12 25 01:55 gradlew.bat
-rw-r--r--  1 staff  staff    26 12 25 01:55 settings.gradle
drwxr-xr-x  4 staff  staff   128 12 25 01:55 src

gradlew tasks

  • Application tasks 
  • Build tasks
  • Build Setup tasks
  • Documentation tasks
  • Help tasks
  • Jib tasks
  • Verification tasks
  • Rules 
./gradlew tasks
$ ./gradlew tasks
Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project 'demo'
------------------------------------------------------------

Application tasks
-----------------
bootRun - Runs this project as a Spring Boot application.

Build tasks
-----------
assemble - Assembles the outputs of this project.
bootBuildImage - Builds an OCI image of the application using the output of the bootJar task
bootJar - Assembles an executable jar archive containing the main classes and their dependencies.
bootJarMainClassName - Resolves the name of the application's main class for the bootJar task.
bootRunMainClassName - Resolves the name of the application's main class for the bootRun task.
bootWar - Assembles an executable war archive containing webapp content, and the main classes and their dependencies.
bootWarMainClassName - Resolves the name of the application's main class for the bootWar task.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.
war - Generates a war archive with all the compiled classes, the web-app content and the libraries.

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'demo'.
dependencies - Displays all dependencies declared in root project 'demo'.
dependencyInsight - Displays the insight into a specific dependency in root project 'demo'.
dependencyManagement - Displays the dependency management declared in root project 'demo'.
help - Displays a help message.
javaToolchains - Displays the detected java toolchains.
outgoingVariants - Displays the outgoing variants of root project 'demo'.
projects - Displays the sub-projects of root project 'demo'.
properties - Displays the properties of root project 'demo'.
tasks - Displays the tasks runnable from root project 'demo'.

Verification tasks
------------------
check - Runs all checks.
test - Runs the test suite.

Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.

To see all tasks and more detail, run gradlew tasks --all

To see more detail about a task, run gradlew help --task <task>

BUILD SUCCESSFUL in 2s
1 actionable task: 1 executed
728x90

빌드

  • gradlew build 명령 실행
./gradlew build
$ ./gradlew build

BUILD SUCCESSFUL in 18s
7 actionable tasks: 7 executed
  • 빌드(gradlew build) 명령을 실행하면 build/libs/ 폴더에 빌드된 파일(.jar, .war)을 확인 할 수 있습니다.
$ pwd
./demo/build/libs

$ ls -l
total 58008
-rw-r--r--  1 staff  staff  12137042 12 25 13:15 demo-0.0.1-SNAPSHOT-plain.war
-rw-r--r--  1 staff  staff  17559349 12 25 13:15 demo-0.0.1-SNAPSHOT.war
$ tree -L 3
.
├── HELP.md
├── bin
│   ├── main
│   │   ├── application.properties
│   │   ├── com
│   │   └── templates
│   └── test
│       └── com
├── build
│   ├── bootWarMainClassName
│   ├── classes
│   │   └── java
│   ├── generated
│   │   └── sources
│   ├── libs
│   │   ├── demo-0.0.1-SNAPSHOT-plain.war
│   │   └── demo-0.0.1-SNAPSHOT.war
│   ├── reports
│   │   └── tests
│   ├── resources
│   │   └── main
│   ├── test-results
│   │   └── test
│   └── tmp
│       ├── bootWar
│       ├── compileJava
│       ├── compileTestJava
│       ├── test
│       └── war
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   └── resources
    └── test
        └── java

32 directories, 11 files

DemoApplication.java 파일 편집

  • "Hello World!" 출력을 위해 DemoApplication.java 파일 편집
ls -l ./src/main/java/com/example/demo/DemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class DemoApplication {

	@RequestMapping("/")
	String home() {
		return "Hello World!";
	}
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

실행

빌드된 파일을 java -jar 명령으로 실행합니다.

  • 빌드된 파일이 있는 폴더에서 실행
$ ls -l build/libs/demo-0.0.1-SNAPSHOT.war
-rw-r--r--  1 staff  staff  17613442  5 21 20:37 build/libs/demo-0.0.1-SNAPSHOT.war
  • 프로젝트 폴더에서 실행
java -jar ./build/libs/demo-0.0.1-SNAPSHOT.war

실행 확인

http://localhost:8080
$ curl -Ss http://localhost:8080
Hello World!%

rebuilding

  • 다시 빌드할 때는 gradlew init 명령을 실행 후 빌드합니다.
./gradlew init

 

728x90
반응형