본문 바로가기

Build/Maven

09. pom aggregation&inheritance

Maven 자체가 여러 다른 모듈들을 다운받아 사용하는 것처럼
 
사용자가 직접 자신의 모듈을 만들어 Depenency 관계를 설정하여 사용할 수 있다. 
 
즉 Maven 의 Module 기능을 사용해서 프로젝트를 여러개로 나누어 사용하는 
 
간단한 샘플을 만들어 보자.
 
 
 

student-app 프로젝트 만들기
 
일단 student-app 이라는 Java App 을 만들고 Maven Project 로 Convert 해보자.
 
중요한 것은 Packaging 을 pom 으로 만들어야 하는 것이다.
 
 
이렇게 변경된 Maven Project 의 pom 파일을 열어보면 아래와 같은 내용을 확인할 수 있다.
 
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.student</groupId>
  <artifactId>student-app</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
</project>
 

student-ui 프로젝트 만들기
 
이번에는 File-New 에서 첫번째 maven module 프로젝트를 만들어 보자. 
 
아래와 같이 Maven Module 이라는 항목을 통해 프로젝트를 만들 수 있다.
 
 
프로젝트 이름은 student-ui 를 사용하도록 하고, 
 
Parent Project 부분이 있는데 이를 앞서 생성한 첫번째 프로젝트인 student-app 으로 설정한다.
 
 
Group Id 만 com.student.ui 로 바꾸고 Packaging 을 jar 로 그대로 유지하고 생성하면 된다.
 
 
 

student-util 프로젝트 만들기
 
student ui 와 마찬가지 방법으로 student-util 이라는 maven module 프로젝트를 만들자.
 
 

student-app 프로젝트 확인하기
 
student-app 으로 돌아와 pom 파일을 확인해 보면
 
그 간의 설정으로 아래와 같이 pom 파일이 바뀌어져 있다.
 
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.student</groupId>
  <artifactId>student-app</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
       <module>student-ui</module>
       <module>student-util</module>
  </modules>
</project>
 
또한 student-app 이라는 프로젝트가 아래와 같이 Modules 프로젝트의
 
내용을 자동으로 담고 있는 것을 확인할 수 있다.
 
 
 
 

student-util 에 Utils 클래스 만들기
 
아래와 같이 Utils 클래스를 생성하고 test 라는 상수를 하나 선언하자.
 
package com.student.util;
public class Utils {
       private Utils() {
             
       }
       
       public static final String test="abc";
}
 

student-ui 에서 student-util 프로젝트의 Utils 클래스 사용하기
 
Maven 의 Dependency 를 설정해 줄 차례다.
 
student-ui 프로젝트로 이동해서 pom 파일을 열고
 
Dependency 항목을 선택후 좌측 Add 버튼을 누른다.
 
 
그러면 아래와 같은 화면이 나타나며 Util 이라고 검색하여 com.student.util 패키지를 찾아 포함시키면 된다.
 
 
그리고 student-ui 의 pom 파일을 확인해 보면 dependency 항목에
 
해당 프로젝트를 빌드하기 전 student-util 프로젝트 의 jar 파일이 먼저 나오도록 설정되었음을 확인할 수 있다.
 
그리고 반드시 Save 버튼을 눌러야 변경된 Pom 파일이 저장된다.
 
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.student</groupId>
    <artifactId>student-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.student.ui</groupId>
  <artifactId>student-ui</artifactId>
  <dependencies>
       <dependency>
             <groupId>com.student.util</groupId>
             <artifactId>student-util</artifactId>
             <version>0.0.1-SNAPSHOT</version>
       </dependency>
  </dependencies>
</project>
 
그리고 pom 파일 저장 후 프로젝트를 모두 선택하고 
 
Maven -> Update Project 를 선택하여 Maven Projects 를 업데이트 한다.
 
 
이 상태에서 App 이라는 Class 를 student-ui 에 아래와 같이 정의해 실행하면 console 창에 abc 가 print 된다.
 
package com.student.ui;
import com.student.util.*;
public class App {
       public static void main(String[] args) {
             System.out.println(Utils.test);
       }
}
 

student-app 의 pom 파일 수정하기
 
해당 클래스를 jar 파일의 실행 Main Class 로 지정하려면 
 
student-app 의 pom 파일에 plugins 를 추가해야 한다. 
 
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.student</groupId>
  <artifactId>student-app</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
       <module>student-ui</module>
       <module>student-util</module>
  </modules>
  <build>
    <plugins>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jar-plugin</artifactId>
         <version>2.4</version>
         <configuration>
           <archive>
             <manifest>
               <mainClass>com.student.ui.App</mainClass>
             </manifest>
           </archive>
         </configuration>
       </plugin>
     </plugins>
  </build>      
</project>
 

결과 확인하기
 
아래와 같이 Maven 빌드가 성공하며 jar 파일이 제대로 실행됨을 알 수 있다.
 
C:\Users\dkim\eclipse-workspace\student-app>mvn clean install
 
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] student-app                                                        [pom]
[INFO] student-util                                                       [jar]
[INFO] student-ui                                                         [jar]
[INFO]
[INFO] ----------------------< com.student:student-app >-----------------------
[INFO] Building student-app 0.0.1-SNAPSHOT                                [1/3]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ student-app ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ student-app ---
[INFO] Installing C:\Users\dkim\eclipse-workspace\student-app\pom.xml to C:\Users\dkim\.m2\repository\com\student\student-app\0.0.1-SNAPSHOT\student-app-0.0.1-SNAPSHOT.pom
[INFO]
[INFO] -------------------< com.student.util:student-util >--------------------
[INFO] Building student-util 0.0.1-SNAPSHOT                               [2/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ student-util ---
[INFO] Deleting C:\Users\dkim\eclipse-workspace\student-app\student-util\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ student-util ---
[WARNING] Using platform encoding (MS949 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ student-util ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding MS949, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Users\dkim\eclipse-workspace\student-app\student-util\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ student-util ---
[WARNING] Using platform encoding (MS949 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ student-util ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ student-util ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ student-util ---
[INFO] Building jar: C:\Users\dkim\eclipse-workspace\student-app\student-util\target\student-util-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ student-util ---
[INFO] Installing C:\Users\dkim\eclipse-workspace\student-app\student-util\target\student-util-0.0.1-SNAPSHOT.jar to C:\Users\dkim\.m2\repository\com\student\util\student-util\0.0.1-SNAPSHOT\student-util-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Users\dkim\eclipse-workspace\student-app\student-util\pom.xml to C:\Users\dkim\.m2\repository\com\student\util\student-util\0.0.1-SNAPSHOT\student-util-0.0.1-SNAPSHOT.pom
[INFO]
[INFO] ---------------------< com.student.ui:student-ui >----------------------
[INFO] Building student-ui 0.0.1-SNAPSHOT                                 [3/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ student-ui ---
[INFO] Deleting C:\Users\dkim\eclipse-workspace\student-app\student-ui\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ student-ui ---
[WARNING] Using platform encoding (MS949 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ student-ui ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding MS949, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Users\dkim\eclipse-workspace\student-app\student-ui\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ student-ui ---
[WARNING] Using platform encoding (MS949 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ student-ui ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ student-ui ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ student-ui ---
[INFO] Building jar: C:\Users\dkim\eclipse-workspace\student-app\student-ui\target\student-ui-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ student-ui ---
[INFO] Installing C:\Users\dkim\eclipse-workspace\student-app\student-ui\target\student-ui-0.0.1-SNAPSHOT.jar to C:\Users\dkim\.m2\repository\com\student\ui\student-ui\0.0.1-SNAPSHOT\student-ui-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Users\dkim\eclipse-workspace\student-app\student-ui\pom.xml to C:\Users\dkim\.m2\repository\com\student\ui\student-ui\0.0.1-SNAPSHOT\student-ui-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for student-app 0.0.1-SNAPSHOT:
[INFO]
[INFO] student-app ........................................ SUCCESS [  0.395 s]
[INFO] student-util ....................................... SUCCESS [  1.505 s]
[INFO] student-ui ......................................... SUCCESS [  0.304 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.379 s
[INFO] Finished at: 2019-06-11T00:48:43+09:00
[INFO] ------------------------------------------------------------------------
 
C:\Users\dkim\eclipse-workspace\student-app\student-ui\target>java -jar student-ui-0.0.1-SNAPSHOT.jar
 
abc
 
 

'Build > Maven' 카테고리의 다른 글

11. Using Dependency Search  (0) 2020.01.23
10. Using WARs  (0) 2020.01.23
08. Maven Repository  (0) 2020.01.23
07. Maven plugin  (0) 2020.01.23
06. Maven phase  (0) 2020.01.23