본문 바로가기

리눅스

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

728x90

Spring Boot 프로젝트 생성

https://4wxyz.tistory.com/148

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

설정

build.gradle 파일

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

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

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'
}

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

> 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.
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.

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'.

Jib tasks
---------
jib - Builds a container image to a registry.
jibBuildTar - Builds a container image to a tarball.
jibDockerBuild - Builds a container image to a Docker daemon.

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>

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.3.2/userguide/command_line_interface.html#sec:command_line_warnings

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

빌드

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

실행

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

###빌드된 파일이 있는 폴더에서 실행
$ cd ./build/libs/
$ java -jar demo-0.0.1-SNAPSHOT.war


###프로젝트 폴더에서 실행
$ java -jar ./build/libs/demo-0.0.1-SNAPSHOT.war

실행 확인

http://localhost:8080

rebuilding

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

./gradlew init

 

728x90