반응형

Maven - Build Life Cycle


(원문 위치 : http://www.tutorialspoint.com/maven/maven_build_life_cycle.htm )

(** 그냥 개념 잡기위해 쭉 읽어보면 될 것 같다. 이클립스 maven 프로젝트 생성 시 project navigator에서 우측 클릭하면 나오는 maven관련 명령에 대한 설명인 것으로 보여진다.)

What is Build Lifecycle?

Build Lifecycle은 목표가 실행되어지는 순서를 정의한 잘 정의되어진 단계(phase)의 순서이다. 여기서 단계(phase)는 life cycle에서 단계(stage)를 표현한다.

에를 들면, 전형적인 Maven Build Lifecycle은 아래와 같은 순서의 단계로 구성된다.
PhaseHandlesDescription
prepare-resourcesresource copying

Resource copying can be customized in this phase. (Resource copying은 이 단계에서 customize 될 수 있다.)

compilecompilationSource code compilation is done in this phase.(소스코드 컴파일은 이 단계에서 된다.)
packagepackaging

This phase creates the JAR / WAR package as mentioned in packaging in POM.xml.(이 단계는 POM.xml내 package에서 언급된것과 같이 JAR/WAR package를 생성한다.)

installinstallation

This phase installs the package in local / remote maven repository.(이 단계는 로컬/원격 maven저장소에 패키지를 설치한다.)

특정 단계 이전 또는 이후에 동작(run)해야만 하는 목표(goal)을 등록하기 위해 사용될 수 있는 'pre'와 'post'단계가 항상 있다.

Maven이 프로젝트 build를 시작할때, 정의된 순서의 단계에 따라 진행하고 각 단계에 등록된 목표를 실행한다. Maven은 아래 세가지 표준 lifecycle을 갖는다.
  • clean

  • default(or build)

  • site

'Goal'은 프로젝트의 build와 관리에 제공하는 특정 작업을 표현한다. 이것은 0 또는 그 이상의 build 단게의 범위를 갖는다. 어떠한 build 단계에도 속하지 않는 goal은 직접적인 호출에 의해 build lifecycle의 바깥에서 실행되어 질 수 있다.

실행 순서는 호출되는 goal과 build 단계의 순서에 따른다. 예를 들면 아래 명령을 생각해 보자. Clean and package 인자는 'dependency:copy-dependencies'가 goal인 동안 build 단계이다.
mvn clean dependency:copy-dependencies package
여기서 우선 'clean'단계가가 실행될 것이다. 그리고 'dependency:copy-dependencies goal'이 실행될 것이다. 마지막으로 package단계가 실행될 것이다.

Clean Lifecycle

'mvn post-clean' 명을 실행할 때, Maven은 아래 단계로 구성되는 clean lifecycle을 호출한다.
  • pre-clean

  • clean

  • post-clean

Maven clean goal(clean:clean)은 clean lifecyle에서 clean 단계에 속한다. clean:clean의 목표는 build 디렉토리를 삭제하는 것으로 build의 output을 삭제한다. 따라서 man clean명령이 실행될 때, Maven은 build 디렉토리를 지운다.

위의 clean lifecycle의 어느 단계세서도 목표를 언급하는 것으로 이 행동을 customize 할 수 있다.

아래 예제에서, pre-clean, clean, post-clean단계를 위해 maven-antrun-plugin:run 모교를 추가할 것이다. 이것은 clean lifecycle의 단계를 표시하는 text message를 echo하도록 한다.

C:\MVN\project 폴더에 pom.xml을 생성했다.
<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>com.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<build>
<plugins>
   <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-antrun-plugin</artifactId>
   <version>1.1</version>
   <executions>
      <execution>
         <id>id.pre-clean</id>
         <phase>pre-clean</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>pre-clean phase</echo>
            </tasks>
         </configuration>
      </execution>
      <execution>
         <id>id.clean</id>
         <phase>clean</phase>
         <goals>
          <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>clean phase</echo>
            </tasks>
         </configuration>
      </execution>
      <execution>
         <id>id.post-clean</id>
         <phase>post-clean</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>post-clean phase</echo>
            </tasks>
         </configuration>
      </execution>
   </executions>
   </plugin>
</plugins>
</build>
</project>
이제 콘솔을 열고 pom.xml을 포함한 폴더에서 아래 mvn 명령을 실행한다.
C:\MVN\project>mvn post-clean

Maven은 처리를 시작하면서  clean life cycle의 모든 단계를 표시할 것이다.

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.pre-clean) @ project ---
[INFO] Executing tasks
     [echo] pre-clean phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ project ---
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.clean) @ project ---
[INFO] Executing tasks
     [echo] clean phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.post-clean) @ project ---
[INFO] Executing tasks
     [echo] post-clean phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.078 s
[INFO] Finished at: 2015-09-26T08:03:06+05:30
[INFO] Final Memory: 7M/247M
[INFO] ------------------------------------------------------------------------

Default (or Build) Lifecycle

이는 Maven의 처음 life cycle이고 프로그램을 build하기위해 사용되어 진다. 이것은 아래 23단계를 갖는다.

Lifecycle PhaseDescription
validate

Validates whether project is correct and all necessary information is available to complete the build process. (프로젝트가 올바른지 확인하고 build 프로세스를 완료하기위해 필요한 정보를 가능하게 한다.)

initialize

Initializes build state, for example set properties (build 상태를 초기화한다. 예를 들면 속성 설정 같은)

generate-sources

Generate any source code to be included in compilation phase. (컴파일 단계에서 포함되어야 하는 어떠한 소스코드를 생성한다.)

process-sources

Process the source code, for example, filter any value. (소스코드를 처리한다. 예를 들면 어떠한 값을 filer하는)

generate-resourcesGenerate resources to be included in the package. (패키지에 포함되어야 하는 리소스를 생성한다.)
process-resources

Copy and process the resources into the destination directory, ready for packaging phase. (목표 디렉토리에 리소스를 복사하고 처리한다. 패키징 단계를 위해 준비한다.)

compileCompile the source code of the project. (프로젝트의 소스를 컴파일한다.)
process-classes

Post-process the generated files from compilation, for example to do bytecode enhancement/optimization on Java classes. (컴파일에서부터 생성된 파일을 후처리한다. 예를 들면 자바클래스의 bytecode를 강화/최적화 하는 것)

generate-test-sources

Generate any test source code to be included in compilation phase. (컴파일 단계에서 포함되어야 하는 테스트 소스 코드를 생성한다.)

process-test-sources

Process the test source code, for example, filter any values. (테스트 소스코드를 처리한다. 예를 들면 어떠한 값을 filter)

test-compile

Compile the test source code into the test destination directory. (테스트 목표 디렉토리의 테스트 코드를 컴파일한다.)

process-test-classes

Process the generated files from test code file compilation. (테스트 코드 파일 컴파일로부터 생성된 파일을 처리한다.)

test

Run tests using a suitable unit testing framework(Junit is one). (suitable unit testing framework - Junit같은 -을 사용하여 테스트를 한다.)

prepare-package

Perform any operations necessary to prepare a package before the actual packaging. (실제 패키징 전에 패키지를 준비하기 위해 필수적인 작업을 수행한다.)

package

Take the compiled code and package it in its distributable format, such as a JAR, WAR, or EAR file. (JAR, WAR, EAR같은 배포 포멧에 컴파일된 코드와 패키지를 옮긴다.)

pre-integration-test

Perform actions required before integration tests are executed. For example, setting up the required environment. (통합테스트가 실행되어지기 전에 필요한 action을 수행한다. 예를 들면 필요한 환경 설정)

integration-test

Process and deploy the package if necessary into an environment where integration tests can be run. (통합 테스트가 동작할 수 있는 환경에 필요하다면 패키지를 배포하고 처리한다.)

post-integration-test

Perform actions required after integration tests have been executed. For example, cleaning up the environment. (통합테스트가 실행된 후 필요한 action을 수행한다. 예를 들면 환경 정리)

verify

Run any check-ups to verify the package is valid and meets quality criteria. (패키지를 검사하기 위한 검사를 하고 품질 기준을 맞춘다.)

install

Install the package into the local repository, which can be used as a dependency in other projects locally. (로컬 저장소에 패키지를 설치한다. 이는 지역적으로 다른 프로젝트에 독립적으로 사용되어 질 수 있다.)

deploy

Copies the final package to the remote repository for sharing with other developers and projects. (다른 개발자와 프로젝트와 공유하기 위한 원격 저장소에 초종 패키지를 복사한다.)

언급할 가치가 있는 Maven lifecycle과 관련된 중요한 개념이 있다.

  • When a phase is called via Maven command, for example mvn compile, only phases upto and including that phase will execute. (Maven 명령을 통해 단계가 호출되어 졌을 때, 예를 들면 man compile, 단지 그 단계까지 그 단계를 포함하여 실행할 것이다.)

  • Different maven goals will be bound to different phases of Maven lifecycle depending upon the type of packaging (JAR / WAR / EAR). (다른 maven 목표는 패키징 타입에 의존한 Maven lifecycle의 다른단계에 속하게 될 것이다.)

아래 예제에서 Build lifecycle의  몇단계를 위해 'maven-antrun-plugin:run'을 첨부할 것이다. 이것은 lifecycle 단계를 표시하는 test메시지를 출력한다.


C:\MVN\project 폴더내 pom.xml을 갱신하였다.


<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>com.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
   <execution>
      <id>id.validate</id>
      <phase>validate</phase>
      <goals>
         <goal>run</goal>
      </goals>
      <configuration>
         <tasks>
            <echo>validate phase</echo>
         </tasks>
      </configuration>
   </execution>
   <execution>
      <id>id.compile</id>
      <phase>compile</phase>
      <goals>
         <goal>run</goal>
      </goals>
      <configuration>
         <tasks>
            <echo>compile phase</echo>
         </tasks>
      </configuration>
   </execution>
   <execution>
      <id>id.test</id>
      <phase>test</phase>
      <goals>
         <goal>run</goal>
      </goals>
      <configuration>
         <tasks>
            <echo>test phase</echo>
         </tasks>
      </configuration>
   </execution>
   <execution>
         <id>id.package</id>
         <phase>package</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
         <tasks>
            <echo>package phase</echo>
         </tasks>
      </configuration>
   </execution>
   <execution>
      <id>id.deploy</id>
      <phase>deploy</phase>
      <goals>
         <goal>run</goal>
      </goals>
      <configuration>
      <tasks>
         <echo>deploy phase</echo>
      </tasks>
      </configuration>
   </execution>
</executions>
</plugin>
</plugins>
</build>
</project>

이제 콘솔을 열고 pom.xml이 포함된 폴더로 가서 아래를 실행한다.

C:\MVN\project>mvn compile

Maven은 처리를 시작하고 컴파일 단계까지 build life cycle의 단계를 표시할 것이다.

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.validate) @ project ---
[INFO] Executing tasks
     [echo] validate phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project --
-
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\project\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ project ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.compile) @ project ---
[INFO] Executing tasks
     [echo] compile phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.704 s
[INFO] Finished at: 2015-09-26T08:22:05+05:30
[INFO] Final Memory: 10M/247M
[INFO] ------------------------------------------------------------------------

Site Lifecycle

Maven Site plugin은 일반적으로 레포트, 배포사이트 등을 생성하기 위한 fresh documentation을 생성하기 위해 사용된다.


단계

  • pre-site

  • site

  • post-site

  • site-deploy

아래 예제에서 Site lifecycle의 모든 단계에 'maven-antrun-plugin:run' 목표를 첨부할 것이다. 이것은 lifeccycle의 단계를 표시하는 text 메시지를 출력할 것이다.

C:\MVN\project 폴더에 pom.xml을 갱신했다.

<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>com.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
   <executions>
      <execution>
         <id>id.pre-site</id>
         <phase>pre-site</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>pre-site phase</echo>
            </tasks>
         </configuration>
      </execution>
      <execution>
         <id>id.site</id>
         <phase>site</phase>
         <goals>
         <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>site phase</echo>
            </tasks>
         </configuration>
      </execution>
      <execution>
         <id>id.post-site</id>
         <phase>post-site</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>post-site phase</echo>
            </tasks>
         </configuration>
      </execution>
      <execution>
         <id>id.site-deploy</id>
         <phase>site-deploy</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>site-deploy phase</echo>
            </tasks>
         </configuration>
      </execution>
   </executions>
</plugin>
</plugins>
</build>
</project>

이제 콘솔을 열고 pom.xml이 포함된 폴더로 가서 아래 mvn 명령을 실행한다.

C:\MVN\project>mvn site

Maven은 처리를 시작하고 site 단계까지 site life cycle의 단계를 표시할 것이다.

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.pre-site) @ project ---
[INFO] Executing tasks
     [echo] pre-site phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-site-plugin:3.3:site (default-site) @ project ---
[WARNING] Report plugin org.apache.maven.plugins:maven-project-info-reports-plug
in has an empty version.
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten t
he stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support buildin
g such malformed projects.
[INFO] configuring report plugin org.apache.maven.plugins:maven-project-info-rep
orts-plugin:2.8.1
[WARNING] No project URL defined - decoration links will not be relativized!
[INFO] Rendering site with org.apache.maven.skins:maven-default-skin:jar:1.0 ski
n.
[INFO] Generating "Dependency Convergence" report    --- maven-project-info-repo
rts-plugin:2.8.1
[INFO] Generating "Dependency Information" report    --- maven-project-info-repo
rts-plugin:2.8.1
[INFO] Generating "About" report    --- maven-project-info-reports-plugin:2.8.1
[INFO] Generating "Plugin Management" report    --- maven-project-info-reports-p
lugin:2.8.1
[INFO] Generating "Project Plugins" report    --- maven-project-info-reports-plu
gin:2.8.1
[INFO] Generating "Project Summary" report    --- maven-project-info-reports-plu
gin:2.8.1
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.site) @ project ---
[INFO] Executing tasks
     [echo] site phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.390 s
[INFO] Finished at: 2015-09-26T08:43:45+05:30
[INFO] Final Memory: 18M/247M
[INFO] ------------------------------------------------------------------------


반응형

+ Recent posts