SpringBoot 시작하기 2.gradle에서 실행 가능한 war 파일 만들기
                                            
                                                
                                                
                                                
                                                
                                                
                                                 9177 단어  SpringBoot자바
                    
소개
 지난번 , SpringMVC에서 HelloWorld REST API를 만들었기 때문에
이것을 실행 가능한 war에 아카이브해, 실제로 실행해 보는 곳까지 해 본다.
 환경
소프트웨어
버전
OS
Windows10 Pro
자바
JDK12
이클립스
2020-12 M1
SpringBoot
2.4.0
Spring Tool Suite (STS)
4.8.1
 구현
 0. 마지막으로 만든 HelloWorld REST API 앱
HelloController.java@RestController
public class HelloController {
    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }
    @RequestMapping("/sb")
    public String helloSp() {
        return "Hello SpringBoot!";
    }
}
HelloSpringApplication.javapackage com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloSpringApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloSpringApplication.class, args);
    }
}
war를 시작하고 여기의 엔드포인트에 액세스하는 것이 목표.
 1. Gradle에 war 플러그인을 추가하고 bootWar 작업 실행
실행 가능한 war를 만들려면 gradle에 war 플러그인을 추가하면 된다.
다음에 생성되는 war 파일명을 지정한다.bootWarタスク 에 archiveName 로 지정할 수 있다.
build.gradleplugins {
    id 'org.springframework.boot' version '2.4.0'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
    id 'eclipse'
    id 'war' //★ここを追加★
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '12'
repositories {
    mavenCentral()
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
    useJUnitPlatform()
}
// ★warファイル名を指定★
bootWar {
  archiveName 'HelloSpringApp.war'
}
war 플러그인을 추가하고 gradle tasks 에서 작업을 확인하면bootWar 가 추가되기 때문에 그것을 실행.
 
bootWar 실행M:\develop\repository\git\practice2\HelloSpring>gradle bootWar
BUILD SUCCESSFUL in 41s
4 actionable tasks: 4 executed
성공하면 프로젝트의 build/libs 부하에 war 파일이 생성된다.
 
 2. war 파일 시작
작성한 war 파일을 java -jar {warファイル名} 로 기동할 수 있다.
M:\develop\repository\git\practice2\HelloSpring\build\libs>java -jar HelloSpringApp.war
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.0)
2020-11-25 23:19:40.149  INFO 13388 --- [           main] com.example.demo.HelloSpringApplication  : Starting HelloSpringApplication using Java 12.0.2 on xxxxx with PID 13388 (M:\develop\repository\git\practice2\HelloSpring\build\libs\HelloSpringApp.war started by xxxx in M:\develop\repository\git\practice2\HelloSpring\build\libs)
2020-11-25 23:19:40.151  INFO 13388 --- [           main] com.example.demo.HelloSpringApplication  : No active profile set, falling back to default profiles: default
2020-11-25 23:19:41.212  INFO 13388 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-11-25 23:19:41.223  INFO 13388 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-11-25 23:19:41.223  INFO 13388 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.39]
2020-11-25 23:19:41.684  INFO 13388 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-11-25 23:19:41.685  INFO 13388 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1489 ms
2020-11-25 23:19:41.829  INFO 13388 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-11-25 23:19:41.968  INFO 13388 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-11-25 23:19:41.976  INFO 13388 --- [           main] com.example.demo.HelloSpringApplication  : Started HelloSpringApplication in 2.181 seconds (JVM running for 2.504)
이 상태에서 다음에 액세스하십시오.
소프트웨어
버전
OS
Windows10 Pro
자바
JDK12
이클립스
2020-12 M1
SpringBoot
2.4.0
Spring Tool Suite (STS)
4.8.1
구현
 0. 마지막으로 만든 HelloWorld REST API 앱
HelloController.java@RestController
public class HelloController {
    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }
    @RequestMapping("/sb")
    public String helloSp() {
        return "Hello SpringBoot!";
    }
}
HelloSpringApplication.javapackage com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloSpringApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloSpringApplication.class, args);
    }
}
war를 시작하고 여기의 엔드포인트에 액세스하는 것이 목표.
 1. Gradle에 war 플러그인을 추가하고 bootWar 작업 실행
실행 가능한 war를 만들려면 gradle에 war 플러그인을 추가하면 된다.
다음에 생성되는 war 파일명을 지정한다.bootWarタスク 에 archiveName 로 지정할 수 있다.
build.gradleplugins {
    id 'org.springframework.boot' version '2.4.0'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
    id 'eclipse'
    id 'war' //★ここを追加★
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '12'
repositories {
    mavenCentral()
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
    useJUnitPlatform()
}
// ★warファイル名を指定★
bootWar {
  archiveName 'HelloSpringApp.war'
}
war 플러그인을 추가하고 gradle tasks 에서 작업을 확인하면bootWar 가 추가되기 때문에 그것을 실행.
 
bootWar 실행M:\develop\repository\git\practice2\HelloSpring>gradle bootWar
BUILD SUCCESSFUL in 41s
4 actionable tasks: 4 executed
성공하면 프로젝트의 build/libs 부하에 war 파일이 생성된다.
 
 2. war 파일 시작
작성한 war 파일을 java -jar {warファイル名} 로 기동할 수 있다.
M:\develop\repository\git\practice2\HelloSpring\build\libs>java -jar HelloSpringApp.war
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.0)
2020-11-25 23:19:40.149  INFO 13388 --- [           main] com.example.demo.HelloSpringApplication  : Starting HelloSpringApplication using Java 12.0.2 on xxxxx with PID 13388 (M:\develop\repository\git\practice2\HelloSpring\build\libs\HelloSpringApp.war started by xxxx in M:\develop\repository\git\practice2\HelloSpring\build\libs)
2020-11-25 23:19:40.151  INFO 13388 --- [           main] com.example.demo.HelloSpringApplication  : No active profile set, falling back to default profiles: default
2020-11-25 23:19:41.212  INFO 13388 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-11-25 23:19:41.223  INFO 13388 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-11-25 23:19:41.223  INFO 13388 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.39]
2020-11-25 23:19:41.684  INFO 13388 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-11-25 23:19:41.685  INFO 13388 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1489 ms
2020-11-25 23:19:41.829  INFO 13388 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-11-25 23:19:41.968  INFO 13388 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-11-25 23:19:41.976  INFO 13388 --- [           main] com.example.demo.HelloSpringApplication  : Started HelloSpringApplication in 2.181 seconds (JVM running for 2.504)
이 상태에서 다음에 액세스하십시오.
@RestController
public class HelloController {
    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }
    @RequestMapping("/sb")
    public String helloSp() {
        return "Hello SpringBoot!";
    }
}
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloSpringApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloSpringApplication.class, args);
    }
}
plugins {
    id 'org.springframework.boot' version '2.4.0'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
    id 'eclipse'
    id 'war' //★ここを追加★
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '12'
repositories {
    mavenCentral()
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
    useJUnitPlatform()
}
// ★warファイル名を指定★
bootWar {
  archiveName 'HelloSpringApp.war'
}
M:\develop\repository\git\practice2\HelloSpring>gradle bootWar
BUILD SUCCESSFUL in 41s
4 actionable tasks: 4 executed
M:\develop\repository\git\practice2\HelloSpring\build\libs>java -jar HelloSpringApp.war
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.0)
2020-11-25 23:19:40.149  INFO 13388 --- [           main] com.example.demo.HelloSpringApplication  : Starting HelloSpringApplication using Java 12.0.2 on xxxxx with PID 13388 (M:\develop\repository\git\practice2\HelloSpring\build\libs\HelloSpringApp.war started by xxxx in M:\develop\repository\git\practice2\HelloSpring\build\libs)
2020-11-25 23:19:40.151  INFO 13388 --- [           main] com.example.demo.HelloSpringApplication  : No active profile set, falling back to default profiles: default
2020-11-25 23:19:41.212  INFO 13388 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-11-25 23:19:41.223  INFO 13388 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-11-25 23:19:41.223  INFO 13388 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.39]
2020-11-25 23:19:41.684  INFO 13388 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-11-25 23:19:41.685  INFO 13388 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1489 ms
2020-11-25 23:19:41.829  INFO 13388 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-11-25 23:19:41.968  INFO 13388 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-11-25 23:19:41.976  INFO 13388 --- [           main] com.example.demo.HelloSpringApplication  : Started HelloSpringApplication in 2.181 seconds (JVM running for 2.504)
http://localhost:8080/  
 http://localhost:8080/sb  
 했어.

tomcat을 내포하고 있으므로이 war 아카이브를 거기에 서버에 배포하면
그대로 웹 앱으로 공개할 수 있다.
(이런 간단한 앱은 없겠지만)
main 클래스를 지정하는 manifest는 gradle에 쓰지 않았지만
좋아.
요약
우선 아카이브화의 방법은 취득 완료.
뒤의 CI/CD의 CD 부분에서 도움이 된다・・・라고 좋구나.
 참고
 Spring Boot + Gradle에서 war 파일을 만드는 방법
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(SpringBoot 시작하기 2.gradle에서 실행 가능한 war 파일 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/suganury/items/6e4f1a7fd4e37608a5cc
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                 우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
Spring Boot + Gradle에서 war 파일을 만드는 방법
Reference
이 문제에 관하여(SpringBoot 시작하기 2.gradle에서 실행 가능한 war 파일 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/suganury/items/6e4f1a7fd4e37608a5cc텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)