문제

개미에서 두 개의 부울에 따라 두 개의 다른 줄을 할당하려고합니다.

의사 코드 (ISH) :

if(condition)
   if(property1 == null)
      property2 = string1;
      property3 = string2;
   else
      property2 = string2;
      property3 = string1;

내가 시도한 것은;

<if>
  <and>
    <not><isset property="property1"/></not>
    <istrue value="${condition}" />
  </and>
  <then>
    <property name="property2" value="string1" />
    <property name="property3" value="string2" />
  </then>
  <else>
    <property name="property2" value="string2" />
    <property name="property3" value="string1" />
  </else>
</if>

그러나 나는 포함 된 라인에 대한 null 포인터 예외를 얻는다 "<if>". 나는 그것을 사용하여 일할 수 있습니다 <condition property=...> 태그이지만 한 번에 하나의 속성 만 설정할 수 있습니다. 나는 사용을 시도했다 <propertyset> 그러나 그것은 허용되지 않았습니다.

나는 당신이 아마 짐작할 것과 같이 개미를 처음 사용합니다 :).

가브

도움이 되었습니까?

해결책

이를 수행하는 방법에는 여러 가지가 있습니다. 가장 간단한 것은 단지 둘을 사용하는 것입니다 condition 진술, 재산 불변성을 활용하십시오.

<condition property="property2" value="string1">
    <isset property="property1"/>
</condition>
<condition property="property3" value="string2">
    <isset property="property1"/>
</condition>

<!-- Properties in ant are immutable, so the following assignments will only
     take place if property1 is *not* set. -->
<property name="property2" value="string2"/>
<property name="property3" value="string1"/>

이것은 약간 번거 롭고 잘 확장되지는 않지만 두 가지 속성의 경우 아마도이 접근법을 사용할 것입니다.

다소 더 좋은 방법은 조건부 목표를 사용하는 것입니다.

<target name="setProps" if="property1">
    <property name="property2" value="string1"/>
    <property name="property3" value="string2"/>
</target>

<target name="init" depends="setProps">
    <!-- Properties in ant are immutable, so the following assignments will only
         take place if property1 is *not* set. -->
    <property name="property2" value="string2"/>
    <property name="property3" value="string1"/>

    <!-- Other init code -->
</target>

우리는 다시 재산 불변성을 이용하고 있습니다. 그렇게하고 싶지 않다면 사용할 수 있습니다. unless 속성 및 추가 간접 수준 :

<target name="-set-props-if-set" if="property1">
    <property name="property2" value="string1"/>
    <property name="property3" value="string2"/>
</target>

<target name="-set-props-if-not-set" unless="property1">
    <property name="property2" value="string2"/>
    <property name="property3" value="string1"/>
</target>

<target name="setProps" depends="-set-props-if-set, -set-props-if-not-set"/>

<target name="init" depends="setProps">
    <!-- Other init code -->
</target>

이를 주목하는 것이 중요합니다 if 그리고 unless 의 속성 target 부동산의 가치가 아닌 부동산이 설정되어 있는지 확인하십시오.

다른 팁

당신이 사용할 수있는 개미-콘트립 깔끔한 라이브러리 <if><then><else> 그러나 구문이지만 몇 가지 다운로드/설치 단계가 필요합니다.

이 다른 질문을 참조하십시오. Ant -Contrib- if/then/else 작업

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top