본문 바로가기

개발/Framework

[Spring] 메이븐(Maven) 개념 / 사용법

 

Spring을 사용하기 위해서는 많은 라이브러리가 필요

라이브러리를 관리하기 위한 도구로 Maven을 사용!

 

 

< Maven >

 

- 라이브러리 정보들을 통합하여 관리하는 프레임워크

- 자바 프로젝트들을 위한 빌드 자동화 도구

- 장점 : 라이브러리간의 의존성*을 파악하여 자동으로 관리해줌

 

* 라이브러리간의 의존성(Dependency)

C를 사용하고 싶다 - B라는 라이브러리가 있어야 함
B를 사용하고 싶다 - A라는 라이브러리가 있어야 함

이러한 의존관계에 따라 필요한 라이브러리를 모두 설치해야함 + 호환성도 맞춰야함

=> 이를 Maven이 대신 관리해줌

 

 

 

< Maven 다운로드 >

 

https://maven.apache.org/download.cgi

 

Maven – Download Apache Maven

Downloading Apache Maven 3.8.6 Apache Maven 3.8.6 is the latest release and recommended version for all users. System Requirements Java Development Kit (JDK) Maven 3.3+ require JDK 1.7 or above to execute - they still allow you to build against 1.3 and oth

maven.apache.org

 

20221225 현재 Maven 3.8.6

- 메이븐 3.3이상부터는 JDK 1.7 이상이 요구됨

 

 

 

< IDE에서 Maven 추가 >

 

Window > Preference > Maven > User Settings > 다운로드한 Maven의 settings.xml 경로 지정 (conf폴더 내)

settings.xml내에 지정한 경로로 변경됨. (외부 라이브러리 저장 경로 지정)

 

 

 

 

< pom.xml >

 

[ 참고 ]

https://maven.apache.org/pom.html

 

Maven – POM Reference

POM Reference POM stands for "Project Object Model". It is an XML representation of a Maven project held in a file named pom.xml. When in the presence of Maven folks, speaking of a project is speaking in the philosophical sense, beyond a mere collection of

maven.apache.org

 

구성

project{프로젝트의 정보 + properties + [repositories] + dependencies + build}

 

 

- 기본사항 : 프로젝트에 대한 정보

POM에는 프로젝트에 대한 모든 필수 정보와 빌드 프로세스 중에 사용할 플러그인 구성이 포함되어 있습니다. 
The POM contains all necessary information about a project, as well as configurations of plugins to be used during the build process. 
<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>
 
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>my-project</artifactId>
  <packaging>war</packaging>
</project>

> groupId : 2번째 레벨까지의 패키지 구조

> artifactId : 3번째 레벨의 패키지명(즉, 프로그램명)
> packaging : 배포할 때 압축형식 war(jar는 그냥 프로그램, web이라서 war) - 확장자 

 

- dependencies : 프로젝트에서 사용할 라이브러리의 정보들을 담는 곳

POM의 초석은 종속성 목록입니다. 대부분의 프로젝트는 올바르게 빌드하고 실행하기 위해 다른 프로젝트에 의존합니다.
The cornerstone of the POM is its dependency list. Most projects depend on others to build and run correctly. If all Maven does for you is manage this list, you have gained a lot.
<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">
  ...
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <type>jar</type>
      <scope>test</scope>
      <optional>true</optional>
    </dependency>
    ...
  </dependencies>
  ...
</project>

작성 시, Maven repository 참고

https://mvnrepository.com/

 

- properties : 이 문서에서 사용할 환경설정 내용들을 담은 변수. (재사용 용도로 지정)

   => EL구문으로 태그명을 호출(${태그명})해 해당 값을 불러올 수 있음

Properties는 POM 기본 사항을 이해하는 데 마지막으로 필요한 부분입니다. 해당 값은 표기법을 사용하여 POM 내 어디에서나 액세스할 수 있습니다 . ${X} 여기서 X는 속성입니다.
Properties are the last required piece to understand POM basics. Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property.
<properties>
    <java-version>1.8</java-version>
    <org.springframework-version>5.3.22</org.springframework-version>
    <org.aspectj-version>1.6.10</org.aspectj-version>
    <org.slf4j-version>1.6.6</org.slf4j-version>
</properties>

 

- repository : 라이브러리를 다운로드 받을 위치 지정 / 기본저장소에서 다운받지 못하는 경우, 직접 다운받고자 하는 사설 저장소를 등록

repositories 요소는 POM에서 Maven이 현재 프로젝트에서 사용할 원격 아티팩트를 다운로드할 수 있는 위치와 방식을 지정하는 반면 distributionManagement는 이 프로젝트가 배포될 때 원격 저장소에 도달하는 위치(및 방법)를 지정합니다.
Whereas the repositories element specifies in the POM the location and manner in which Maven may download remote artifacts for use by the current project, distributionManagement specifies where (and how) this project will get to a remote repository when it is deployed. The repository elements will be used for snapshot distribution if the snapshotRepository is not defined.
저장소는 아티팩트를 수집하고 저장하는 장소로 존재합니다. 프로젝트가 아티팩트에 종속될 때마다 Maven은 먼저 지정된 아티팩트의 로컬 복사본을 사용하려고 시도합니다. 해당 아티팩트가 로컬 리포지토리에 없으면 원격 리포지토리에서 다운로드를 시도합니다. POM 내의 저장소 요소는 검색할 대체 저장소를 지정합니다.
저장소는 Maven 커뮤니티의 가장 강력한 기능 중 하나입니다. 기본적으로 Maven은 
https://repo.maven.apache.org/maven2/ 에서 중앙 저장소를 검색합니다 . 
추가 리포지토리는 pom.xml `repositories` 요소에서 구성할 수 있습니다.
Additional repositories can be configured in the pom.xml `repositories` element.
<repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
    <repository> <!-- 추가적으로 사설 저장소 등록 -->
      <id>Datanucleus</id>
      <url>http://www.datanucleus.org/downloads/maven2/</url>
    </repository>
</repositories>

 

- plugins : 플러그인 관련 태그

<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">
  <build>
    ...
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <extensions>false</extensions>
        <inherited>true</inherited>
        <configuration>
          <classifier>test</classifier>
        </configuration>
        <dependencies>...</dependencies>
        <executions>...</executions>
      </plugin>
    </plugins>
  </build>
</project>

 

 

 

 

* 추가

https://spring.io/guides/gs/maven/

 

Building Java Projects with Maven

this guide is designed to get you productive as quickly as possible and using the latest Spring project releases and techniques as recommended by the Spring team

spring.io

스프링에서 메이븐을 사용하는 방법은 스프링 공식페이지에 잘 나와있다.

pom.xml 작성법은 이쪽이 더 실제와 비슷한 예제이다.