Post

메이븐 POM 파일

메이븐 POM 파일

POM.XML

메이븐 실행시에 참고하는 파일 프로젝트 루트 디렉토리에 위치

POM 파일 기본 정보

프로젝트 정보

  • ModelVersion

    • pom.xml을 이루고 있는 maven xml 문서 형식의 버젼이다.

    • 현재는 무조건 4.0.0이다.

    • 5.0.0은 draft 상태이며 정식 발표되지 않았다.

      • https://cwiki.apache.org/confluence/display/MAVEN/POM+Model+Version+5.0.0
  • GroupId

    • 일종의 네임스페이스<폴더의 개념>
    • 동일 프로젝트명을 사용할 수 있게 해줌.
    • Package 명명 규칙을 따른다.
    • 프로젝트의 그룹명, 일반적으로 다른 컴포넌트와 라이브러리와 차별될 수 있는 유니크한 명칭을 가짐
    • 관계적으로 회사 도메인명(google.com, naver.com)을 거꾸로한 명칭을 사용함
    • 중앙의 공용 Maven Repository에서 수 많은 라이브러리를 서비스하면서 Unique한 GroupId를 가지게 하기 위한 관례
  • ArtifactId

    • 프로젝트 이름이자 이 패키지를 파일로 묶을 때 사용할 파일명.
    • 프로젝트 이름과 동일하게 설정
    • 소문자로만 작성하며 특수문자는 사용하지 않는다.
    • 해당 프로젝트 명칭(컴포넌트 명칭), groupId 범위 내에서 유일해야 함
  • Name

  • Version

    • 동일 GroupID와 Artifact에서 개발 배포 및 진척에 따른 다른 내용의 코드가 개발 될 수 있게 구분해주는 용도

    • Version 표시 방법 : (메이저버젼).(마이너버젼).(순차버젼-식별자)

      • 메이저 버젼 : 매우 큰 변화가 생겼을 경우 증가
      • 마이너 버젼 : 작은 기능상의 변화가 생기거나 기능이 추가되었을 경우에 증가
      • 순차 버젼 : 버그가 수정되고 새로운 기능의 계획이 추가되었을 경우에 증가
    • SNAPSHOT : 개발용, RELEASE : 배포용

  • Packaging

    • 프로젝트 타입
      • War, Jar, Ear, Ejb…
    • 자바 웹 프로젝트는 WAR
    • 일반 자바 프로젝트는 JAR
1
2
3
4
5
6
7
<project>
    <groupId>com.blidkaga</groupId>
    <artifactId>HelloWorld</artifactId>
    <modelVersion>4.0.0</modelVersion>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
</project>

Properties

  • Maven 내부에서 반복적으로 사용될 상수 값을 정의할 때 사용
1
2
3
4
5
6
7
8
9
10
11
<properties>
    <org.springframework-version>3.1.1.RELEASE</org.springframework-version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

Dependencies(의존성 라이브러리 정보)

  • project 태그 바로 하위
  • 의존성 라이브러리 정보를 포함함
  • 최소 groupId, artifactId, version 정보가 필요함
  • dependencies 섹션 아래 세부 dependency가 기술됨
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
... 생략
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
Scope
  • dependency 하위에 포함되는 항목
  • 해당 dependency가 포함되는 범위에 대한 타입
scope 종류
  • compile
    • 기본 scope. 미입력시에도 기본 적용
    • 모든 상황에서 포함됨
  • provided
    • compile과 유사하게 모든 상황에서 수행된다.
    • 하지만, 다른 외부 컨테이너에서 기본 제공되는 API인 경우 provided로 지정시 마지막 패키징할 때 포함되지 않음
    • 예를 들어 tomcat에서 기본적으로 servlet api를 제공하기 때문에 servlet api를 provied로 지정하면 패키징시 제외된다.
  • runtime
    • 컴파일 시에는 불필요 실행시에 필요한 경우.
    • 런타임 및 테스트 시 classpath에 추가 되지만, 컴파일시에는 추가되지 않음
  • test
    • 테스트시에만 사용
  • system
    • provided와 유사
    • system의 특정 path를 참조하도록 지정
    • Maven의 Central Repository를 사용하지 않음
  • import
    • scope는 dependencyManagement 섹션에서 pom의 의존관계에 대해 사용

빌드 정보

  • project 태그 바로 하위
  • plugins
    • 빌드에서 사용할 플러그인
  • 디렉토리 구조
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
... 생략
<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <outputDirectory>${project.basedir}/target/classes</outputDirectory>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>resources-${deploy.phase}/</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>
        </plugin>
    </plugins>
</build>

Repository

  • 의존성을 다운로드 받을 위치의 repository
  • 기술되지 않을 시 기본적인 위치
    • http://repo.maven.apache.org/maven2
  • 다수의 <repository> 기술 가능
  • 회사 내부의 repository를 기술 하기도 한다.
    • nexus
    • artifactory를 이용
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...생략   
<repositories>
    <repository>
        <id>spring-snapshot</id>
        <name>Spring Snapshot Repository</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>
</repositories>

Plugin Repository

  • Maven plugin을 다운로드 받을 수 있는 저장소 위치를 기술
1
2
3
4
5
6
7
8
9
10
11
12
13
<pluginRepositories>
    <pluginRepository>
        <id>acme corp</id>
        <name>Acme Internal Corporate Repository</name>
        <url>http://acmecorp.com/plugins</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
            <enabled>false</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>

배포

  • deploy goal을 실행했을 시 배포되는 위치를 기술함
  • URL 항목에는 일반적으로 회사 내부에 설치한 Repository URL이 기술된다.
    • NEXUS, Artifactory 등등으로 구성된 저장소
1
2
3
4
5
6
7
8
9
10
11
12
<distributionManagement>
    <repository>
        <id>releases-repo</id>
        <name>Releases Repository</name>
        <url>일반적인 회사 내부 repository url</url>
    </repository>
    <snapshotRepository>
        <id>snapshots-repo</id>
        <name>Snapshots Repository</name>
        <url>일반적인 회사 내부 repository url</url>
    </snapshotRepository>
</distributionManagement>

Super POM.XML

  • 프로젝트를 시작할 때 기술하는 pom.xml은 maven의 super pom.xml을 상속 받는다.
    • 즉 기본값이다.
  • 확인 위치 : maven 설치 디렉토리 > lib > maven-model-builder-3.5.3.jar 파일 내부 > org > apache > maven > model > pom-4.0.0.xml
  • 아래 내용은 Maven 3.5.3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!-- START SNIPPET: superpom -->
<project>
  <modelVersion>4.0.0</modelVersion>

  <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>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

  <build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
    <pluginManagement>
      <!-- NOTE: These plugins will be removed from future versions of the super POM -->
      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.5.3</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>

  <profiles>
    <!-- NOTE: The release profile will be removed from future versions of the super POM -->
    <profile>
      <id>release-profile</id>

      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>

      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
<!-- END SNIPPET: superpom -->

POM 파일에 정의된 Library 정보<의존성>

  • 메이븐에서 특정 Library를 정의하면 자동으로 그와 관련된<의존된> 다른 라이브러리를 자동으로 가져와서 구성한다.

메이븐 프로젝트 디렉토리 구조

구분 디렉토리 경로 비고
소스 디렉토리(언어별 구성) /src/main/java/src/main/groovy – 배포할 자바 코드파일 – 배포할 그루비 코드 파일
리소스 디렉토리 /src/main/resources 배포할 리소스 파일
테스트 소스 디렉토리 /src/test/java 운영에 배포하지 않고 테스트를 위한 자바 파일
테스트 리소스 디렉토리 /src/test/resources 테스트에 사용할 리소스 파일. 리소스에 정의되어 있다면 해당 파일로 교체되어 테스트
  • * 소스 디렉토리에는 자바 파일만 두고, 리소스 디렉토리에는 자바 파일을 제외한 다른 파일을 둠.
  • * 테스트에 필요한 파일은 테스트 소스 디렉토리와 테스트 리소스 디렉토리에 위치함.
  • * 테스트 소스와 테스트 리소스 디렉토리의 파일은 배포시에 제외됨.

POM.XML 파일 구성

위치는 상관없지만 다음의 순서로 정의하면 추후에 관리가 편함.

POM.XML 파일 구조

This post is licensed under CC BY 4.0 by the author.