Dev/Server

[Maven] Maven build시 profile 설정

pu3vig 2022. 6. 20. 14:45
728x90
  • target:  서버 환경에 따라, maven build시, profile 설정

 


  • method: 

1. pom.xml

<profiles>
  <profile>
    <id>local</id>
    <build>
    
    ...
    
    </build>
  </profile>
  <profile>
    <id>dev</id>
    <build>
      <directory>${proejct.basedir}/target-dev</directory>
      <finalName>project</finalName>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <executions>
            <execution>
              <phase>prepare-package</phase>
              <goals>
                <goal>run</goal>
              </goals>
              <configuration>
                <tasks>
                  <delete>
                    <fileset dir="${project.resource.local.outPutDirectory}"
                             includes="**/config.properties,**/config_dev.properties,**/config_prod.properties">
                  </delete>
                  <copy file="${project.resource.directory}/config_dev.properties"
                        tofile="${project.resource.local.outPutDirectory}/config.properties">
                </tasks>
              </configuration>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
          </configuration>
        </plugin>
      </plugins>
    </build>
  </profile>
  <profile>
    <id>prod<id>
    <build>
    
    ...
    
    </build>
  </profile>
</profiles>

※ local/dev/prod 3개의 profile을 설정하고 maven build 시, SVN에 있는 config.properties / config_dev.properties / config_prod.properties 중 환경에 맞게 profile을 설정하고, 설정한 profile의 execution에 따라 config.properties 파일로 변환해서 적용하는 에제

 


2. Maven build

// maven build시, profile=dev
# mvn clean package -P dev

※ maven build시, 일반적으로 profile 설정이 없는 경우, mvn clean으로 충분하지만, profile을 설정하여 사용하고자 하는 경우,

package -P ${profile_id} 를 추가하여 실행

 


  • warning: 

- Maven build 시, profile 설정하는 방법은 Jvm 실행 시 -Dspring.profiles.active 방식과 완전히 다름

- Maven build 시, profile을 설정하면, 설정한 profile에 맞춰 package를 생성하기 때문에, local / dev / prod에서 동일한 설정파일명을 보유할 수 있음

- Jvm runtime 시, profile을 설정하면, 이미 package는 build 이전에 환경별로 생성한 파일을 그대로 동일하게 갖게 되고, runtime에서 설정한 profile에 해당하는 파일을 설정파일로 사용

※ -Dspring.profiles.active=dev를 runtime 옵션으로 준 경우, springframework에서는 application-dev.properties 파일을 자동으로 설정파일로 지정

 

728x90