Maven이란 무엇인가(2)

겔로그·2022년 7월 2일
0

Spring Boot

목록 보기
10/21

개요

오늘은 이전시간에 봤었던 Maven의 구체적인 사용법에 대해 알아보는 시간을 가질 것이다. pom.xml에는 어떤 정보들이 들어가는지, 어떻게 옵션들을 설정하여 빌드에 옵션을 줄 수 있는지에 대해 알아보고자 한다.

Pom.xml (기본 태그)

이전에도 봤듯이 pom.xml은 Maven에서 빌드 관련 설정 정보 및 프로젝트의 의존성이 담겨있는 파일이다.

앞서, Spring initializr를 통해 프로젝트를 만들 경우, 의존성을 아무것도 선택하지 않으면 다음과 같은 구조를 볼 수 있다.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <description>test</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

pom.xml 내부 태그를 봤을 경우 다양한 태그들이 존재하는 것을 알 수 있다. 이제 각각의 태그가 어떤 정보를 담고 있는지 알아가보자.

pom.xml 태그 종류

project : Maven의 XML 네임스페이스를 지정
modelVersion : Maven의 model Version(현재는 4.0.0, 업그레이드 될 경우 변경)

<modelVersion>4.0.0</modelVersion>

groupId : 해당 프로젝트를 모든 프로젝트 사이에서 고유하게 식별하게 해 주는 id

groupId 명명 규칙

  • pakage 명명 규칙을 따른다.
  • 도메인 명칭을 이용한다.
  • 하위그룹은 아무렇게나 사용 가능.
<groupId>org.springframework.boot</groupId>

artifactId : 버전 정보를 생략한 jar 파일의 이름, 소문자로만 작성하며 특수문자 사용 X

  • 써드파티 jar 파일일 경우, 할당된 이름 사용
<artifactId>spring-boot-starter-parent</artifactId>

version : 자와 점으로 이루어진 일반적인 버전 형태를 사용

<version>2.7.1</version>

relativePath : 보통 다중 pom.xml을 사용할 때 이용, 외부 상속일 경우 빈 값으로 선언

<relativePath/> <!-- lookup parent from repository -->

packaging : 패키징 형식을 지정하는 태그(없을 경우 jar로 가정)

<packaging>jar</packaging>

name : 프로젝트의 이름

<name>test-project</name>

properties : 프로젝트 관련 속성, maven 내부에서 반복적으로 사용될 상수 값을 정의할 때 사용

<java.version>11</java.version>

parent : pom.xml의 상속에 관련된 태그, 빈 값일 경우, 외부 상속

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.7.1</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>

dependencies : 의존성 라이브러리 정보를 포함하며 project 태그 바로 하위에 존재함, 최소 최소 groupId, artifactId, version 정보가 필요

 <dependencies>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
   </dependency>
</dependencies>

scope : 각 의존성 라이브러리가 적용될 범위를 지정할 수 있다. 범위는 5가지 종류로 분류된다.

scope 5가지 종류

  • compile (default) : 아무것도 지정되지 않았을 경우 설정되는 기본 설정값.
    이 값으로 설정하는 경우 모든 클래스 경로에서 사용할 수 있고, 컴파일 및 배포 시 같이 제공된다. 해당 프로젝트에 종속된 다른 프로젝트에도 적용될 수 있다.
  • provided : JDK 혹은 컨테이너가 런타임 시에만 해당 라이브러리를 제공한다. 컴파일 혹은 테스트 경로에서만 사용할 수 있으며, 배포 시에는 빠진다.
  • runtime : 컴파일 시에는 사용되지 않고, 실행될 때만 사용된다. 런타임과 테스트 경로에서는 있지만, 컴파일 클래스 경로에는 존재하지 않는다.
  • test : 해당 scope를 사용하는 경우는, 테스트 시에만 해당 라이브러리를 사용한다. 응용 프로그램의 정상적인 사용 시에는 필요 없다는 의미로 테스트 컴파일과 실행 단계에서만 사용하며, 종속된 다른 프로젝트에는 영향을 미치지 않는다.
  • system : provided와 유사하나, 저장소에서 관리되지 않고 직접 관리하는 JAR를 추가한다.
    해당 범위에 있는 라이브러리는 artifact 단위에서는 항상 사용할 수 있으나 repository에 존재하지 않는다.
    해당 범위를 사용하는 경우에는 dependency 설정에 systemPath를 추가해서 작성해야 한다. (어느 경로에 JAR 파일을 둬야 하는지 지정해줘야 함, ${java.home}/lib 등과 같이 기술)

plugins : 빌드에서 사용할 플러그인

<plugins>
  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
  </plugin>
</plugins>

다음과 같은 다양한 태그 종류들로 의존성을 관리하며, 빌드간 어떤 의존성을 추가할지, 패키징은 어떤 것으로 할 것이며 추가한 의존성이 어느 범위까지 영향을 미치게 할지 설정할 수 있다.

Pom.xml (고급 옵션)

기본적으로 사용했던 pom.xml 태그 외에도 다양한 태그들이 존재한다.
pom.xml으로 구성할 수 있는 추가적인 옵션들은 다음과 같다.

원격 레포지토리 설정

<repositories>
  <repository>
    <id>repository</id>
    <url>http://test.com</url>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
    <releases>
      <enabled>true</enabled>
    </releases>
  </repository>
</repositories>

git commit log

<plugin>
   <groupId>pl.project13.maven</groupId>
   <artifactId>git-commit-id-plugin</artifactId>
   <version>2.2.1</version>
   <configuration>
     <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
     <prefix>git</prefix>
     <dateFormat>dd.MM.yyyy '@' HH:mm:ss z</dateFormat>
     <dateFormatTimeZone>${user.timezone}</dateFormatTimeZone>
     <verbose>false</verbose>
     <generateGitPropertiesFile>true</generateGitPropertiesFile>
     <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties
     </generateGitPropertiesFilename>
     <format>properties</format>
     <skipPoms>true</skipPoms>
     <injectAllReactorProjects>false</injectAllReactorProjects>
     <failOnNoGitDirectory>true</failOnNoGitDirectory>
     <failOnUnableToExtractRepoInfo>true</failOnUnableToExtractRepoInfo>
     <skip>false</skip>
     <runOnlyOnce>false</runOnlyOnce>
     <excludeProperties>
     </excludeProperties>
     <includeOnlyProperties>
     </includeOnlyProperties>
     <useNativeGit>false</useNativeGit>
     <abbrevLength>7</abbrevLength>
     <commitIdGenerationMode>flat</commitIdGenerationMode>
     <gitDescribe>
       <skip>false</skip>
       <always>false</always>
       <abbrev>7</abbrev>
       <dirty>-dirty</dirty>
       <match>*</match>
       <forceLongFormat>false</forceLongFormat>
     </gitDescribe>
   </configuration>
</plugin>

Profile 설정

<profiles>
   <profile>
     <id>local</id>
     <activation>
       <activeByDefault>true</activeByDefault>
     </activation>
     <properties>
       <env>local</env>
     </properties>
   </profile>

   <profile>
     <id>beta</id>
     <activation>
       <activeByDefault>false</activeByDefault>
     </activation>
     <properties>
       <env>beta</env>
     </properties>
   </profile>
</profiles>

빌드간 사용할 resource 설정

<build>
  <finalName>test-project</finalName>
  <resources>
    <resource>
      <directory>src/main/resources-${env}</directory>
    </resource>
  </resources>
  <testResources>
    <testResource>
      <directory>src/test/resources</directory>
    </testResource>
  </testResources>
</build>

이 외에도 다양한 구성을 할 수 있으니 다양한 오픈소스 프로젝트를 참고하여 Pom.xml에서 어떤 설정까지 가능한지 좀 더 알아보는 시간을 가져도 좋을 것 같다.

결론

이번 글에서는 일반적으로 사용되어지는 태그 및 어떤 방식으로 pom.xml을 구성하는지 알아보는 시간을 가져보았다.

보다 더 뛰어난 개발자가 되기 위해서는 내가 사용하는 것들이 적어도 어떤 동작을 통해 하나의 어플리케이션을 실행하는지 이해하고 있어야 된다고 생각한다.

그저 maven이라는 빌드 툴이 있는데 pom.xml에 의존성을 추가하고 빌드하고 실행하면 어플리케이션이 실행된다!라고 단순하게 생각하면서 개발을 하지 말자라는 의미에서  글을 쓴 것이다.

솔직히 저것만 보고 아~ pom.xml에는 저런 태그들이 있고 이럴 때 사용하는구나~ 하는 사람은 없을거라고 확신한다. (아닐 경우 천재..)

뭔 소린지 모르겠지?

솔직히 말하자면 이 말고도 다양한 태그들이 존재한다. 해당 태그들이 무엇을 하는지 하나하나 다 이해하려고 하면 정말 오랜시간이 소요될 것이다.

Maven에 대해 좀 더 자세하게 알고싶은 사람들은 다양한 오픈소스의 pom.xml을 확인해보고 어떤 설정을 했는지만 봐도 지금보다 한 단계 성장할 수 있을 것이다

pom.xml 참고
sample pom.xml code
guava pom.xml
apache pom.xml

한 번씩 읽어보고 앞서 설명한 내용을 다시 리뷰하며 Maven 설정에 대한 이해도를 다 함께 높이는 시간이 됐으면 좋겠다.

Reference

https://wikidocs.net/18341
https://araikuma.tistory.com/447
https://johngrib.github.io/wiki/groupId-artifactId/
https://www.baeldung.com/maven-relativepath
https://www.baeldung.com/maven-packaging-types
https://yongku.tistory.com/entry/%EC%8A%A4%ED%94%84%EB%A7%81Spring-pomxml%EC%9D%98-%EA%B5%AC%EC%A1%B0

profile
Gelog 나쁜 것만 드려요~

0개의 댓글