Eseguire un compito Ant a Maven solo se una proprietà è impostata
-
22-09-2019 - |
Domanda
Il mio pom.xml
è in esecuzione un compito Ant per distribuire un file tramite FTP. Tuttavia, questa distribuzione deve essere fatto solo se l'argomento -Dftp=true
viene dato nel comando Maven (cioè mvn clean install -Dftp=true
). Così, ho scritto il seguente codice:
<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>
Con questo pom.xml
, il compito Ant non viene eseguito se non definire la proprietà -Dftp
nel mio comando Maven. Tuttavia, se io do ogni tipo di valore per questa proprietà, ad esempio -Dftp=false
, la task Ant è gestito, che non è corretto.
Come configurare l'attività AntRun per essere eseguito solo se una determinata proprietà ha un dato valore?
ps: so di poter utilizzare un profile
che è attivo solo quando ftp
è uguale a true
. Questa soluzione funziona, ma per qualche ragione, voglio avere la mia Antrun blocco plug build
.
<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)
Soluzione
C'è un if
compito in Ant-contrib che si potrebbe usare:
<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>
Non è necessario il <else>
, questo era solo a scopo dimostrativo.
Altri suggerimenti
Nel caso in cui non ti piace IF sintassi nel Ant-contrib è possibile utilizzare 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>
con Maven-antrun-plugin: 1.8 È possibile specificare attributi nella Configurazione per eseguire o meno attività Ant a seconda delle condizioni riportate nel Maven antrun plug documentazione
<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>
Come richiesto, ma utilizzando