이 내용은 박재성님이 집필하신 [자바 세상의 빌드를 이끄는 메이븐] 책을 그대로 정리한 것이다.
회사에서 스터디를 하는데 문서 보안 때문에 어쩔수 없이 블로그를 이용하게 되었고,
위 내용은 박재성님의 허락을 받았습니다.

8장 메이븐 프로파일, 배포
 1) 빌드 이식성
  - 특별한 수정이나 변경 없이도 다양한 환경에 배포하고 운영하는 것이 가능해야 한다는 것
  - 개발환경마다 설정이 달라지는 예
    데이터 베이스 설정, 로깅 설정, 프레임워크와 관련한 설정, 서버 설정, 컴파일러 설정
 2) 프로파일
  가 프로파일 기본
   - 메이븐은 서로 다른 환경에 따라 달라지는 설정을 다르게 관리할수 있는 프로파일 기능을 제공
    <project>
 [...]
 <profiles>
  <profile>
   <id>local</id>
   <activation>
    <activeByDefault>true</activeByDefault>
        </activation>
   <properties>
    <jdbc.host>127.0.0.1</jdbc.host>
    <jdbc.username>webzine</jdbc.username>
    <jdbc.password>webzine</jdbc.password>
   </properties>
  </profile>
  <profile>
   <id>development</id>
   <properties>
    <jdbc.host>125.125.125.125</jdbc.host>
    <jdbc.username>webzine</jdbc.username>
    <jdbc.password>webzine</jdbc.password>
   </properties>
  </profile>
  <profile>
   <id>production</id>
   <properties>
    <jdbc.host>125.125.125.120</jdbc.host>
    <jdbc.username>production</jdbc.username>
    <jdbc.password>production</jdbc.password>
   </properties>
  </profile>
 </profiles>
    </project>
   - 메이븐 프로파일은 <profiles/> 엘리먼트 아래 <profile/> 엘리먼트를 추가해 설정
     모든 프로파일은 고유한 id 값을 가짐
     mvn -P<profile_id> Phase[s] Goal[s] 와 같이 실행
     mvn -Plocal test
     <profile/> 엘리먼트 아래에는 <project/> 루트 엘리먼트가 가질수 있는 모든 엘리먼트의 설정이 가능
     <project>
 [...]
 <profiles>
  <profile>
   <build>
    <defaultGoal>....</defaultGoal>
    <finalName>...</finalName>
    <resources>...</resources>
     생략
   </build>
   <reporting>...</reporting>
   <modules>...</modules>
   <dependencies>...</dependencies>
     생략  
  </profile>
 </profiles>
     </project>
     빌드 환경에 따라 다른 설정을 해야 하는 경우 위의 예처럼 프로파일 기능을 통해 설정 가
   - 옵션 P로 id를 전달하는 것이 불편할 경우 default 실행
     <activation>
 <activeByDefault>true</activeByDefault>
     </activation>
     mvn test 를 실행했을 경우 local 이 기본값으로 실행됨
  나 프로파일 활성화
   - 메이븐은 빌드를 실행하는 시점에 프로파일 id를 전달해 프로파일을 실행하는 방법뿐만 아니라 빌드 환경에 따라 프로파일을
     자동으로 실행하는 활성화 activation 기능을 제공
     예를 들어 빌드 환경의 jdk 버젼에 따라 서로 다른 프로파일을 실행하고자 하는 경우
     <project>
 [...]
 <profiles>
  <profile>
   <id>local</id>
   <activation>
    <activeByDefault>true</activeByDefault>
    <jdk>1.5</jdk>
        </activation>
  </profile>
  <profile>
   <id>development</id>
   <activation>
    <jdk>1.6</jdk>
        </activation>
  </profile>
 </profiles>
    </project>
     a <activation/> 엘리먼트를 활용하면 프로파일 id를 인자로 전달하지 않더라도 현재 빌드하고 있는 환경과 일치하는 프로파일을 자동 실행
     b <activeByDefault/> 엘리먼트 값을 true 로 설정한 프로파일은 현재 빌드 환경과 일치하는 프로파일이 없을 경우 기본 실행
       예제의 경우 jdk1.4 또는 jdk1.7 일 경우 jdk1.5 가 기본으로 실행
     c <jdk/> 엘리먼트 값을 1.6으로 설정할 경우 자바 버젼이 1.6으로 시작하는 1.6.0_11, 1.6.0_20에 해당하는 환경에서 실행
   - <activation/> 엘리먼트 설정에는 jdk 뿐만 아니라 os 종류 및 버젼, 파일의 유무, 속성값에 따라 프로파일 활성화 여부 결정할수 있음
     <project>
 [...]
 <profiles>
  <profile>
   <id>local</id>
   <activation>
    <activeByDefault>true</activeByDefault>
    <jdk>1.5</jdk>
        
    <os>
     <name>Windows XP</name>
     <family>Windows</family>
     <arch>x86</arch>
     <version>5.1.2600</version>
    </os>
    <property>
     <name>enviroment</name>
     <value>local</value>
    </property>
    <file>
     <exists>database.properties</exists>
     <missing>application.properties</missing>
    </file>
   </activation>
  </profile>
 </profiles>
    </project>
  다 프로파일 관리를 위한 설정 파일
   - 메이븐의 프로파일 설정은 프로파일 설정 범위에 따라 다음과 같이 네개의 영역에서 설정하는 것이 가능
     . 프로젝트 단위로 프로파일 설정 : 프로젝트의 pom.xml
     . 사용자 단위로 프로파일 설정 : USER_HOME/.m2/settings.xml
     . 모든 사용자에게 공통으로 프로파일 설정 : MAVEN_HOME/conf/settings.xml
     . 별도의 프로파일 설정파일 : 프로젝트 디렉토리 아래에 profiles.xml
     pom.xml 파일은 <project/> 루트 엘리먼트가 가지는 모든 엘리먼트를 가질수 있으나 나머지 파일은
     <repositories/>, <pluginRepositories/>, <properties/> 엘리먼트만 설정 가능. 파일에 설정이 분산될 경우 유지보수, 이해가 어렵기 때문
   - 프로젝트에 설정되어 있는 프로파일 목록을 확인하고 싶은 경우
     mvn help:active-profiles
 3) 프로파일 기능을 활용한 리소스 관리
  - 프로파일에 따라 변경되는 정보를 속성 <properties/> 엘리먼트 설정으로 관리할 경우 속성이 늘어나므로, 배포 환경에 따라 설정 파일까지
    분리해야 한다면 리소스를 관리하는 디렉토리 자체를 각 프로파일 id에 따라 분리하여 관리가 가능
    src/main/resources 디렉토리는 공통 자원을 관리하는 용도로 활용
    


    File -> New -> Other -> Source Folder 로 src/main/resources-local, resources-dev 를 추가
    


    <project>
       [...]
      <build>
   <finalName>webzine</finalName>
   <resources>
    <resource>
     <directory>src/main/resources</directory>
    </resource>
    <resource>
     <directory>src/main/java</directory>
     <excludes>
      <exclude>**/*.java</exclude>
     </excludes>
    </resource>
    <resource>
     <directory>src/main/resource-${environment}</directory>
    </resource>
   </resources>
      </build>
      <profiles>
   <profile>
    <id>local</id>
    <activation>
     <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
     <environment>local</environment>
    </properties>
   </profile>
   <profile>
    <id>dev</id>
    <properties>
     <environment>dev</environment>
    </properties>
   </profile>
      </profiles>
    </project>
  - environment 속성을 프로파일별로 추가하고 <resources/> 엘리먼트 중의 하나를 resources-${environment} 로 설정
 4) 배포 자동화
  - 톰캣을 사용한다는 가정
    톰캣 플러그인을 추가
    <plugins>
    <plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>tocat-maven-plugin</artifactId>
     <version>1.0</version>
    </plugin>
    </plugins>
    위와 같이 설정하면 톰캣 서버의 manager 기능으로 소스 배포 가능
    tomcat-maven-plugin을 위처러 설정하면 톰캣 서버 manager URL 기본값은 http://localhost:8080/manager id: admin, password : 없음 으로 설정됨
    톰캣 서버의 manager를 사용하려면 기본값으로 설정되어 있는 사용자 정보를 추가해야 함
    CATALINA_HOME/conf/tomcat-users.xml 아래 내용 추가
    <role rolename="manager"/>
    <user username="admin" password=" " roles="manager"/> 
  - mven clean package tomcat:deploy
    소스가 이미 배포되어 있는 상태일 경우 tomcat:undeploy 실행
  가 톰캣 manager URL이 기본값과 다른 경우 아래와 같이 설정
    <plugins>
    <plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>tocat-maven-plugin</artifactId>
     <version>1.0</version>
   <configuration>
    <url>http://www.webzine.com:1234/mymanager</url>
   </configuration>
    </plugin>
    </plugins>  
  나 톰캣 manager 접근 아이디와 비밀번호를 설정하는 경우
   - pom.xml 파일의 플러그인 설정을 변경
    <plugins>
    <plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>tocat-maven-plugin</artifactId>
     <version>1.0</version>
   <configuration>
    <server>myserver</server>
   </configuration>
    </plugin>
    </plugins>  
    <configuration/> 엘리먼트에 <server/> 설정을 추가한후 settings.xml 파일에 다음과 같이 설정
    [settings.xml]
    <settings>
 [...]
 <servers>
  <server>
   <id>myserver</id>
   <username>myusername</username>
   <password>mypassword</password>
  </server>
 </servers>
    </settings>
    위와 같이 설정하고 tomcat-users.xml 파일에서 username, password 를 위와 동일하게 변경하면 됨
   - 톰캣 이외의 컨테이너를 사용할 경우 cargo 메이븐 플러그인을 활용
Posted by gt1000

블로그 이미지
gt1000

태그목록

공지사항

어제
오늘

달력

 « |  » 2024.4
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

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함