8

FTPpom.xmlを使用してファイルをデプロイするためにAntタスクを実行しています。-Dftp=trueただし、このデプロイメントは、引数がMavenコマンド(つまり)で指定されている場合にのみ実行する必要がありますmvn clean install -Dftp=true。したがって、私は次のコードを書きました:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks if="ftp">
                            <echo message="Deploying files through FTP..."/>
                            ...
                        </tasks>
                    </configuration>
                </execution>
            </executions>

これを使用すると、Mavenコマンドでプロパティをpom.xml定義しない場合、Antタスクは実行されません。-Dftpただし、たとえば、このプロパティに何らかの値を-Dftp=false指定すると、Antタスクが実行されます。これは正しくありません。

特定のプロパティに特定の値がある場合にのみ実行されるようにAntRunタスクを構成するにはどうすればよいですか?

ps:が等しいprofile場合にのみアクティブになるaを使用できることはわかっています。このソリューションは機能しますが、何らかの理由で、Antrunプラグインブロックが必要です。ftptruebuild

<profiles>
    <profile>
        <id>deployment</id>
        <activation>
            <activeByDefault>false</activeByDefault>
            <property>
                <name>ftp</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    ... (define the Ant task here)
4

3 に答える 3

10

Ant-contribには、使用できるifタスクがあります。

  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.3</version>
    <executions>
      <execution>
        <id>ftp</id>
        <phase>package</phase>
        <goals>
          <goal>run</goal>
        </goals>
        <configuration>
          <tasks>
            <taskdef resource="net/sf/antcontrib/antcontrib.properties"
              classpathref="maven.plugin.classpath" />
            <if>
              <equals arg1="${ftp}" arg2="true" />
              <then>
                <echo message="The value of property ftp is true" />
              </then>
              <else>
                <echo message="The value of property ftp is not true" />
              </else>
            </if>
          </tasks>
        </configuration>
      </execution>
    </executions>
    <dependencies>
      <dependency>
        <groupId>ant-contrib</groupId>
        <artifactId>ant-contrib</artifactId>
        <version>20020829</version>
      </dependency>
    </dependencies>
  </plugin>

あなたは必要ありません<else>、これはただデモ目的のためでした。

于 2010-02-24T08:37:45.893 に答える
3

maven-antrun-plugin:1.8を使用する場合Maven antrunプラグインのドキュメントで説明されているように、<target />構成で属性を指定して、条件に応じてAntタスクを実行するかどうかを指定できます。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>run</goal>
        </goals>
        <configuration>
          <target if="ftp">
            <echo message="To run, just call mvn package -Dftp=true"/>
          </target>
        </configuration>
      </execution>
    </executions>
  </plugin>

要求どおりですが、非推奨の<tasks/>の代わりに<target/>を使用しています

于 2015-07-23T06:35:32.867 に答える
2

Ant-contribのIF構文が気に入らない場合は、antelopetasksを使用できます。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <inherited>false</inherited>
    <configuration>
        <target>
            <taskdef name="if" classname="ise.antelope.tasks.IfTask"/>

            <if name="maven.ant.target">
                <ant target="${maven.ant.target}"/>
                <else>
                    <fail message="Please specify a target to execute in 'maven.ant.target' property" />
                </else>
            </if>
        </target>
    </configuration>
    <dependencies>
        <!-- http://antelope.tigris.org/nonav/docs/manual/bk03.html -->
        <dependency>
            <groupId>org.tigris.antelope</groupId>
            <artifactId>antelopetasks</artifactId>
            <version>3.2.10</version>
        </dependency>
    </dependencies>
</plugin>
于 2012-05-04T10:22:38.930 に答える