簡體   English   中英

使用 antcontrib<if> 通過 maven-antrun-plugin 的任務

[英]Using antcontrib <if> task via maven-antrun-plugin

我的 maven java 項目使用 maven-antrun-plugin 來執行部署我的應用程序的 deploy.xml ant 腳本。 deploy.xml 使用<if>任務,這似乎是導致問題的原因;

[INFO] 執行任務
[taskdef] 無法從資源 net/sf/antcontrib/antlib.xml 加載定義。 無法找到。

部署:
[信息] ----------------------------------------------- -------------------------
[錯誤] 構建錯誤
[信息] ----------------------------------------------- -------------------------
[信息] 發生 Ant BuildException:執行此行時發生以下錯誤:
E:\\My_Workspace\\xxxxxx\\xxxxxx\\xxxxxxx\\deploy.xml:24: 問題:無法創建任務或鍵入 if
原因:名稱未定義。
行動:檢查拼寫。
行動:檢查是否已聲明任何自定義任務/類型。
行動:檢查任何<presetdef>/<macrodef>聲明已經發生。

這是我的 pom 中的 antrun 插件配置;

<plugin>
    <inherited>false</inherited>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>remote-deploy</id>
            <phase>install</phase>
            <configuration>
                <tasks>
                    <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.plugin.classpath"/>

                        <property name="compile_classpath" refid="maven.compile.classpath" />
                        <property name="runtime_classpath" refid="maven.runtime.classpath" />
                        <property name="plugin_classpath" refid="maven.plugin.classpath" />
                                
                        <echo message="compile classpath: ${compile_classpath}"/>
                        <echo message="runtime classpath: ${runtime_classpath}"/>
                        <echo message="plugin classpath: ${plugin_classpath}"/>

                        <ant antfile="${basedir}/deploy.xml">
                            <target name="deploy" />
                        </ant>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>1.0b3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-jsch</artifactId>
            <version>1.7.1</version>
        </dependency>
    </dependencies>
</plugin>

.. 這是我的 deploy.xml 中的相關部分;

<target name="deploy" if="deploy">
    <if>    <!-- line 24 -->
        <and>

為什么我查看我的 maven repo 我可以看到ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar並且當我查看 jar 時我可以看到net/sf/antcontrib/antcontrib.properties所以沒有有問題。

當我檢查maven.compile.classpathmaven.compile.classpathmaven.compile.classpath的值時,我看不到任何對antcontrib引用,這可能是問題嗎? 為什么當antcontrib定義為依賴時它們不出現?

我認為添加 ant 來編譯類路徑以運行 maven 插件不是一個好主意。

我使用 Maven 3.0.4,它通過為 ant-contrib 標簽指定命名空間來工作,例如:

<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>

我的maven-antrun-plugin依賴項:

<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>

我發現您需要在插件中包含 ant-contrib 依賴項,這將使 taskdef 標簽能夠找到 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>

好的,我已經解決了。

將依賴項移出<build><plugin>標記並將它們與其他項目依賴項一起放入似乎已經完成了。

另一種解決方案是:將 ant-contrib-1.0b3.jar 保留到一個路徑,然后像這樣定義它

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

然后

<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>

我在這里放了完整的代碼示例,您可以下載https://www.surasint.com/run-ant-with-if-from-maven/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM