17

我的 maven java 项目使用 maven-antrun-plugin 来执行部署我的应用程序的 deploy.xml ant 脚本。deploy.xml 使用该<if>任务,这似乎是导致问题的原因;

[INFO] 执行任务
[taskdef] 无法从资源 net/sf/antcontrib/antlib.xml 加载定义。找不到。

部署:
[INFO] --------------------------------------------- ---------------
[错误] 构建错误
[信息] --------------- -------------------------------------------------- -------
[INFO] An Ant BuildException has occurred: The following error occurred while execution this line:
E:\My_Workspace\xxxxxx\xxxxxx\xxxxxxx\deploy.xml:24: Problem: failed to create task or type如果
原因:名称未定义。
行动:检查拼写。
行动:检查是否已声明任何自定义任务/类型。
行动:检查任何 <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,当我查看罐子内部时,我可以看到net/sf/antcontrib/antcontrib.properties所以那里没有问题。

当我检查 的值时,maven.compile.classpath我看不到对 的任何引用,这可能是问题吗?为什么它们在被定义为依赖项时不出现?maven.compile.classpathmaven.compile.classpathantcontribantcontrib

4

4 回答 4

29

我认为添加 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>
于 2012-12-04T10:56:08.070 回答
4

我发现您需要在插件中包含 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>
于 2017-05-08T17:36:14.483 回答
3

好的,我已经解决了。

将依赖项移出<build><plugin>标签并将它们与其他项目依赖项一起放入似乎已经成功了。

于 2011-01-18T13:22:22.617 回答
0

另一种解决方案是:将 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/

于 2017-03-29T19:11:28.823 回答