Using antcontrib task via maven-antrun-plugin

前端 未结 4 1922
半阙折子戏
半阙折子戏 2020-12-03 21:06

My maven java project uses the maven-antrun-plugin to execute a deploy.xml ant script that deploys my app. The deploy.xml uses the task and this seem

相关标签:
4条回答
  • 2020-12-03 21:39

    another solution would be: keep the ant-contrib-1.0b3.jar to a path and then define it like this

    <property name="runningLocation" location="" />
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="${runningLocation}/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>
    

    then

    <target name="doSomething">
        <if>
            <equals arg1="${someProp}" arg2="YES" />
            <then>
                <echo message="It is YES" />
            </then>
            <else>
                <echo message="It is not YES" />
            </else>
        </if>
    </target>
    

    I put full code example here which you can download https://www.surasint.com/run-ant-with-if-from-maven/

    0 讨论(0)
  • 2020-12-03 21:42

    I found that you need to include the ant-contrib dependency inside the plugin which will enable the taskdef tag to find antcontrib.properties

      <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>ant-contrib</groupId>
                        <artifactId>ant-contrib</artifactId>
                        <version>20020829</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>copy-and-rename-template-files</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target name = "copy-and-rename-template-files">
                                <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                                <if>
                                    <available file="src/main/resources/docker/templates" type="dir"/>
                                    <then>
                                        <copy todir="${project.build.directory}/docker">
                                            <fileset dir="src/main/resources/docker/templates">
                                                <include name="**/*"/>
                                            </fileset>
                                        </copy>
    
    
                                        <move todir="${project.build.directory}/docker">
                                            <fileset dir="${project.build.directory}/docker">
                                                <include name="*"/>
                                            </fileset>
                                            <mapper>
                                                <regexpmapper from="(.*)project(.*)" to="\1${project.artifactId}\2"/>
                                            </mapper>
                                        </move>
                                    </then>
    
                                    <else>
                                        <echo>src/main/resources/docker/templates does not exist, skipping processing docker templates</echo>
                                    </else>
                                </if>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    
    0 讨论(0)
  • 2020-12-03 21:46

    I think it is not a very good idea to add ant to compile classpath in order to run maven plugin.

    I use Maven 3.0.4 and it worked by specifying namespace for ant-contrib tags, for example:

    <configuration>
      <target>
        <echo message="The first five letters of the alphabet are:"/>
        <ac:for list="a,b,c,d,e" param="letter" xmlns:ac="antlib:net.sf.antcontrib">
          <sequential>
            <echo>Letter @{letter}</echo>
          </sequential>
        </ac:for>
      </target>
    </configuration>
    

    My maven-antrun-plugin dependencies:

    <dependencies>
      <dependency>
        <groupId>ant-contrib</groupId>
        <artifactId>ant-contrib</artifactId>
        <version>1.0b3</version>
        <exclusions>
          <exclusion>
            <groupId>ant</groupId>
            <artifactId>ant</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.apache.ant</groupId>
        <artifactId>ant-nodeps</artifactId>
        <version>1.8.1</version>
      </dependency>
    </dependencies>
    
    0 讨论(0)
  • 2020-12-03 21:47

    OK, I've solved it.

    Moving the dependencies out of the <build><plugin> tag and putting them in with the other project dependencies seems to have done the trick.

    0 讨论(0)
提交回复
热议问题