목차

  1. maven 설치
  2. 빈 폴더 생성하기
  3. pom.xml 작성하기
  4. pom.xml이해하기
    1. dependency 추가란?
    2. 메이븐 로컬 리포지토리 & 메이븐 원격 중앙 리포지토리
  5. 컴파일하기 (mvn compile)
  6. 컴파일 실행과정에서 출력되는 메세지 읽어보기
  7. 의존 전이 (Transitive Dependencies)

먼저 maven 을 이해하기 위해서, maven 프로젝트를 직접 만들어보자.

 

1. maven 설치

maven 을 설치한다. https://maven.apache.org/ 사이트에서 zip 파일을 다운로드한 뒤 환경변수 설정을 해준다.

PATH 변수에 "[메이븐 설치 폴더]/bin" 경로를 추가하면 된다.

환경변수 설정

설치가 잘 됐는지 확인하기 위해서는 커맨드라인에서서  mvn -version  을 해보면 된다.

 

2. 빈 폴더 구조 생성

아래와 같은 구조로 새폴더들을 생성하자. project 폴더 안에 src 폴더, src 폴더 안에 main 폴더, main 폴더안에 java 폴더가 있도록 새폴더들을 만들어주면 된다.

projdct 안에 src, src 안에 main, main 안에 java

maven 의 기본 폴더 구조에 맞춰서 폴더들을 생성해준 것이다. (이렇게 해야한다)

 

3. pom.xml 

project 폴더에 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 
		http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>sp5</groupId>
	<artifactId>sp5-chap02</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.0.2.RELEASE</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>utf-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

 

4. pom.xml 이해하기

위 xml 코드에서 <dependency> 부분을 보자.

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.0.2.RELEASE</version>
		</dependency>
	</dependencies>

 

이 부분은

spring-context 라는 identifier를 가진 5.0.2.RELEASE 버전의 artifact 에 대한 의존(dependency)을 추가

한 것이다.

 

artifact 가 뭘까?

maven은 한 개의 모듈을 artifact 라는 단위로 관리한다.. 라고 책에 써있다.

그냥 artifact = module = jar 파일 이라고 이해하면 될 것 같다.

 

dependency 를 추가한다 는 건 뭘까?

일반적인 자바 어플리케이션에서 클래스패스(classpath)에 spring-context 모듈을 추가한다는 것이다.

 

잠깐 딴소리를 해보자. 클래스 패스란 뭘까?


이 그림은 자바 코드가 실행되는 과정을 간단하게 그린 것이다.

여기에서만약

java Filename.class 명령을 실행하는 위치가

Filename.class 파일이 있는 위치와 다르면

해당 명령은 정상동작하지 못할 것이다.

 

그런데 다음과 같은 쉘 스크립트가 있다고 하자.

#!/bin/bash

CLASSPATH=/Users/charlieshin/javatest    # Filename.class 파일 위치

java MyClass

이 쉘 스크립트가 실행되는 위치는

Filename.class 파일의 위치와 관계없이 잘 동작한다. CLASSPATH 변수 덕분에.

 

아무튼 이렇게, CLASSPATH 변수는

클래스와  패키지의 위치를 지정해주는 역할을 한다.

 

이 변수는 Java Compiler 와 JVM 이 사용한다.


다시 돌아와서, pom.xml 의 dependency 부분을 보자. ↓

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>

참고로 각 artifact ( = module = jar 파일 ) 들의 완전한 이름은 "아티팩트이름-버전.jar"이어서,

결국 이 코드는

Java compiler와 JVM이 사용하는 CLASSPATH 변수에 spring-context-5.0.2.RELEASE.jar 파일 경로를 추가한다. 라는 뜻이다.

 

spring-context-5.0.2.RELEASE.jar 이 파일의 경로는 어디일까?

maven 은 기본적으로 [사용자홈폴더]\.m2\repository 폴더를 로컬 리포지토리로 사용한다.

나의 maven local repository 경로는 아래 캡처화면과 같다.

 

LG gram 은 나랑 비슷할것같다.

 

그런데 내 컴퓨터에 spring-context-5.0.2.RELEASE.jar 파일을 다운받은 적이 없으므로

나의 maven local repository 경로에는 이 파일이 없을것이다.

 

그렇다면 이 파일을 처음 가져올 때는 어디서 가져올까?

maven 원격 중앙 리포지토리(https://repo.maven.apache.org)에서 가져온다.

maven 원격 중앙 리포지토리에서 다운받아서 maven 로컬 리포지토리에 갖다놓는다.

그러면 이제 maven local repository 에 jar 파일이 생겼으니까 이 파일을 사용한다.

 

5. 컴파일하기

이제 프로젝트 root 경로에서 mvn compile 을 실행해보자.

C:\Users\LG_PC\Documents\mine\spring-study\project> mvn compile

이제 mvn compile 실행 과정에서 출력된 이 메세지들을 읽어보자.

 

6. 컴파일 실행과정에서 출력되는 메세지 읽어보기

 

C:\Users\LG_PC\Documents\mine\spring-study\project> mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------< sp5:sp5-chap02 >---------------------------
[INFO] Building sp5-chap02 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading     from     central:
https://repo.maven.apache.org/maven2/org/springframework/spring-context/5.0.2.R
ELEASE/spring-context-5.0.2.RELEASE.pom
Downloading     from     central:
https://repo.maven.apache.org/maven2/org/springframework/spring-context/5.0.2.R
ELEASE/spring-context-5.0.2.RELEASE.pom (5.7 KB at 3.1 KB/s)

...생략...

Downloaded from central: https://repo.maven.apache.org/maven2/org/springframewo
rk/spring-core/5.0.2.RELEASE/spring-core-5.0.2.RELEASE.jar (1.2 MB at 221.KB/s)
Download from central: https://repo.maven.apache.org/maven2/org/springframework
/spring-context/5.0.2.RELEASE/spring-context-5.0.2.RELEASE.jar (1.1 MB at 119KB/s)
[INFO]
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.992 s
[INFO] Finished at: 2020-12-04T06:07:03+09:00
[INFO] ------------------------------------------------------------------------

 

위의 로그들에서

Download from central: https://repo.maven.apache.org/maven2/org/springframework
/spring-context/5.0.2.RELEASE/spring-context-5.0.2.RELEASE.jar (1.1 MB at 119KB/s)

이 부분을 보면 spring-context-5.0.2.RELEASE.jar 파일을 다운로드 했음을 알 수 있다.

 

그런데 다운로드를 이것만 받은게 아니라 다른 파일들 보인다.

Downloading     from     central:
https://repo.maven.apache.org/maven2/org/springframework/spring-context/5.0.2.R
ELEASE/spring-context-5.0.2.RELEASE.pom (5.7 KB at 3.1 KB/s)

...생략...

Downloaded from central: https://repo.maven.apache.org/maven2/org/springframewo
rk/spring-core/5.0.2.RELEASE/spring-core-5.0.2.RELEASE.jar (1.2 MB at 221.KB/s)

개중에는 pom.xml 에서 써놓지 않은 아티팩트들도 있다. 예를 들면 spring-core-5.0.2.RELEASE.jar 가 그렇다.

이런 애들은 왜 다운받아진걸까?

 

7. 의존 전이 (Transitive Dependencies)

우리가 정의한 dependency 에서 spring-context-5.0.2.RELEASE.jar 파일을 다운받으라고 했다.

그런데 이 모듈도 의존하고 있는 모듈이 있을 것이다.

 

아래는 spring-context-5.0.2.RELEASE.pom 파일 내용의 일부이다.

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>5.0.2.RELEASE</version>
  <scope>compile</scope>
 </dependency>

spring-context 가 spring-core 에 의존하고 있다. 즉 spring-context 를 사용하려면 spring-core 도 다운받아야한다.

그래서 여기 정의되어있는 의존 설정에 따라 다른 모듈들도 다운을 받는다.

 

만약 spring-core 모듈이 또 다른 모듈을 의존하고 있다면 그것도 다운받을 것이다.

 

이것을 Transitive Dependencies 라고 한다.


 

출처

1. 초보 웹 개발자를 위한 스프링 5 프로그래밍 입문(최범균)

2. 클래스패스 Class path 란? goateedev.tistory.com/169

 

 

 

+ Recent posts