IT/삽질

[JAVA배포] JAR파일 vs WAR파일

_하늘여우_ 2024. 6. 16. 23:57

출처 : unsplash.com

 

흔히 java 배포 시 war배포, jar배포 방식을 혼용해서 말하는 경우가 있다.

이번 기회에 둘 간의 차이점을 알아 본다.

 

JAR파일과 WAR파일의 차이점

  • JAR (Java Archive) 파일: 독립 실행 가능한 자바 애플리케이션 배포 시 사용됨. JAR 파일은 라이브러리나 유틸리티 패키지 포함 가능하며, java -jar 명령어로 실행 가능함. 흔히 스프링 프로젝트 생성 시 "External Libraries" 에 등록되는 .jar 형태가 그것이다.

  • WAR (Web Application Archive) 파일: 웹 애플리케이션을 배포하는데 사용됨. 보통 WAS (서블릿 컨테이너, 예: 톰캣, 제우스..) 를 통해 배포되는 경우를 일컫음.

 

JAR 파일 예제

1. SpringBoot 애플리케이션 코드

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @RestController
    class HelloController {
        @GetMapping("/")
        public String hello() {
            return "Hello World!";
        }
    }
}

 

2. 빌드 및 실행

- 내장 was 통해서 서비스 수행 가능

$ ./gradlew clean build
$ java -jar ./build/libs/demo-0.0.1-SNAPSHOT.jar

skyfox@DESKTOP-HLRO4FU:/mnt/c/study/javaStudy/demo$ java -jar ./build/libs/demo-0.0.1-SNAPSHOT.jar 

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.4)

2024-06-16 23:54:52.380  INFO 1146 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 11.0.19 on DESKTOP-HLRO4FU with PID 1146 (/m
nt/c/study/javaStudy/demo/build/libs/demo-0.0.1-SNAPSHOT.jar started by skyfox in /mnt/c/study/javaStudy/demo)
2024-06-16 23:54:52.398  INFO 1146 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2024-06-16 23:54:56.705  INFO 1146 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2024-06-16 23:54:56.758  INFO 1146 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-06-16 23:54:56.760  INFO 1146 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.65]
2024-06-16 23:54:57.054  INFO 1146 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-06-16 23:54:57.055  INFO 1146 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 4379 ms
2024-06-16 23:54:58.585  INFO 1146 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2024-06-16 23:54:58.632  INFO 1146 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 8.463 seconds (JVM running for 10.127)

 

 

결론

일반적으로 톰캣이나 JBoss 같은 WAS 통해서 자바 애플리케이션 배포 및 서비스 진행하는 경우 WAR 파일 방식으로 배포하며, 그 외 독립적으로 실행 가능한 파일 생성 시 JAR 파일 방식으로 배포한다!