본문 바로가기

TroubleShooting/Java

maven 에 if condition 추가

728x90

maven 으로 빌드할때 특정 옵션일 경우, 다르게 동작시키고 싶은 경우

- A 옵션이 true 일때, a 파일을 지우고 아니면 남기고 싶다

 

pom.xml 에 배포 환경마다 profile 관련 처리를 하는 maven-antrun-plugin 이 포함되어 있을텐데, 그 configuration 을 수정하는게 맞다.

 

maven-antrun-plugin 은 1.7 을 사용했다.

<properties>
  <k8s>false</k8s>
</properties>   
   
<dependencies>
   <dependency>
     <groupId>ant-contrib</groupId>
     <artifactId>ant-contrib</artifactId>
     <version>1.0b3</version>
   </dependency>
</dependencies>
<configuration>
  <target>
    <echo>deployTarget : ${deployTarget}</echo>
    <echo>project.build.outputDirectory : ${project.build.outputDirectory}</echo>
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.runtime.classpath"/>
    <if>
      <equals arg1="${opt}" arg2="true"/>
      <then>
        <echo message="The value of property opt is true" />
        <delete>
          <fileset dir="${project.build.outputDirectory}" includes="*.yml" />
          ...
        </delete>
        <delete dir="${project.build.outputDirectory}/profiles"/>
      </then>
      <else>
        <echo message="The value of property opt is not true" />
        <copy
        file="${project.build.outputDirectory}/profiles/${deployTarget}-application.yml"
        tofile="${project.build.outputDirectory}/application.yml"
        overwrite="true" failonerror="false"/>
        ...
      </else>
    </if>
  </target>
</configuration>

maven 빌드시 다음과 같이 옵션을 추가할 수 있다.

maven clean package -Palpha -Dopt=true

opt 가 true 라서 target/classes/*.yml 파일들은 삭제가 되고 profiles 디렉토리도 삭제된다.

 

참고 사이트

https://stackoverflow.com/questions/18052589/maven-if-sentences-in-pom-xml-in-the-property-tag

 

Maven: If sentences in pom.xml in the property tag

I'd like to set a property if an environment variable is set. I googled a lot on it and all I found is something similar to the code below, but I keep getting the error: [FATAL] Non-parseable PO...

stackoverflow.com

https://ptrthomas.wordpress.com/2009/03/08/why-you-should-use-the-maven-ant-tasks-instead-of-maven-or-ivy/