java.lang.NoSuchMethodError Ant / Maven通过命令行失败

时间:2015-10-02 00:05:13

标签: java eclipse maven ant

由于maven故障导致通过命令行执行ant构建时出现问题。我可以毫无问题地构建eclipse,但是当尝试通过命令行执行时,Maven会阻塞并返回以下错误:

  

java.lang.NoSuchMethodError:   org.apache.maven.artifact.ant.DependenciesTask.setPathType(Ljava /郎/字符串;)

我设置了环境变量M2 M2_HOME和JAVA_HOME。一切似乎都应该如此,但是当涉及到任何依赖方法或库时,我似乎无法弄清楚eclipse在空白处填充的位置。

POM文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company.webapp</groupId>
    <artifactId>V6</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>company Web Site</name>

    <properties>
        <maven.compiler.target>1.6</maven.compiler.target>
        <maven.compiler.source>1.6</maven.compiler.source>
        <spring.version>3.2.4.RELEASE</spring.version>
        <servlet-api.version>3.0.1</servlet-api.version>
        <jaxb-api.version>2.1</jaxb-api.version>
        <spring-hateoas.version>0.7.0.RELEASE</spring-hateoas.version>
        <junit.version>4.11</junit.version>
        <spring-mvctest.version>1.0.0.M2</spring-mvctest.version>
        <httpclient.version>4.3.3</httpclient.version>
        <jstl.version>1.2</jstl.version>
        <jersey-client.version>1.8</jersey-client.version>
        <jackson-mapper-asl.version>1.9.13</jackson-mapper-asl.version>
        <jackson-core.version>2.4.0</jackson-core.version>
        <jackson-databind.version>2.4.3</jackson-databind.version>
        <logback-spring.version>1.0-SNAPSHOT</logback-spring.version>
        <logback-classic.version>1.0.13</logback-classic.version>
        <slf4j.version>1.7.5</slf4j.version>
        <company-ws-util.version>1.1.5-SNAPSHOT</company-ws-util.version>
        <company-util-misc.version>1.2.31-SNAPSHOT</company-util-misc.version>
        <company-util-cache.version>1.4.7-SNAPSHOT</company-util-cache.version>
        <java-mail.version>1.4</java-mail.version>
        <commons-lang3.version>3.1</commons-lang3.version>
    </properties>

    <dependencies>      
        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>${httpclient.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>

        <!-- Java Mail API -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>${java-mail.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet-api.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>

        <dependency>
              <groupId>com.sun.jersey</groupId>
              <artifactId>jersey-client</artifactId>
              <version>${jersey-client.version}</version>
        </dependency>

        <!-- Jackson JSON Mapper -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson-mapper-asl.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson-databind.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson-core.version}</version>
        </dependency>

        <!-- Internal Libraries -->
        <dependency>
            <groupId>com.company.util</groupId>
            <artifactId>company-util-misc</artifactId>
            <version>${company-util-misc.version}</version>
        </dependency>

        <dependency>    
            <groupId>com.company.ws.util</groupId>
            <artifactId>company-ws-util</artifactId>
            <version>${company-ws-util.version}</version>
        </dependency>
        <dependency>    
            <groupId>com.company.util</groupId>
            <artifactId>company-util-cache</artifactId>
            <version>${company-util-cache.version}</version>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback-classic.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>     

        <!-- Test scope -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
            <version>${junit.version}</version>
        </dependency>
    </dependencies>
    <build>

            <plugins>
              <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0-alpha-2</version>
                <executions>
                  <execution>
                    <phase>initialize</phase>
                    <goals>
                      <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                      <files>
                        <file>${basedir}/build/@mvn@/mvn.build.properties</file>
                      </files>
                    </configuration>
                  </execution>
                </executions>
              </plugin>
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                  <webResources>
                    <resource>
                      <directory>${basedir}/build/${VERSION}</directory>
                    </resource>
                  </webResources>
                  <webappDirectory>${basedir}/build/${VERSION}</webappDirectory>
                </configuration>
              </plugin>
            </plugins>
        <directory>build/target</directory>
        <finalName>${WARFILE}</finalName>
    </build>
</project>

完整错误堆栈:

     [echo] MVN BUILD (MAVEN):
     [echo] --------------------------
    [mkdir] Skipping C:\"PATH"\build\@mvn@ because it already exists.
[propertyfile] Updating property file: C:\"PATH"\build\@mvn@\mvn.build.properties
Property "quickbuild" has not been set
     [echo] Executing maven build on Developer machine (other machine)
parsing buildfile jar:file:/C:/Apps/ANT/apache-ant-1.9.5/lib/maven-artifact-ant-2.0.4-dep.jar!/org/apache/maven/artifact/ant/antlib.xml with URI = jar:file:/C:/Apps/ANT/apache-ant-1.9.5/lib/maven-artifact-ant-2.0.4-dep.jar!/org/apache/maven/artifact/ant/antlib.xml from a zip file
  [antcall] Exiting C:\"PATH"\build.xml.

BUILD FAILED
C:\"PATH"\build.xml:349: The following error occurred while executing this line:
C:\"PATH"\build.xml:279: The following error occurred while executing this line:
C:\"PATH"\ant\imports\mvn.xml:46: Maven sub-build has failed! Error in trycatch block:
EXCEPTION BEGIN
C:\"PATH"\ant\imports\mvn.xml:38: java.lang.NoSuchMethodError: org.apache.maven.artifact.ant.DependenciesTask.setPathType(Ljava/lang/String;)V{line.separator}EXCEPTION END
    at org.apache.tools.ant.taskdefs.Exit.execute(Exit.java:164)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
    at net.sf.antcontrib.logic.TryCatchTask$CatchBlock.execute(TryCatchTask.java:137)
    at net.sf.antcontrib.logic.TryCatchTask.execute(TryCatchTask.java:225)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
    at net.sf.antcontrib.logic.IfTask.execute(IfTask.java:217)
    at sun.reflect.GeneratedMethodAccessor16.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.TaskAdapter.execute(TaskAdapter.java:155)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.MacroInstance.execute(MacroInstance.java:396)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
    at net.sf.antcontrib.logic.IfTask.execute(IfTask.java:197)
    at sun.reflect.GeneratedMethodAccessor16.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.TaskAdapter.execute(TaskAdapter.java:155)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:435)
    at org.apache.tools.ant.Target.performTasks(Target.java:456)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
    at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:38)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
    at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:441)
    at org.apache.tools.ant.taskdefs.CallTarget.execute(CallTarget.java:105)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:435)
    at org.apache.tools.ant.Target.performTasks(Target.java:456)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1376)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
    at org.apache.tools.ant.Main.runBuild(Main.java:853)
    at org.apache.tools.ant.Main.startAnt(Main.java:235)
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:285)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:112)

3 个答案:

答案 0 :(得分:10)

正如评论中所提到的,Eclipse中使用的Maven Ant tasks版本与命令行中使用的版本之间存在差异。 Eclipse似乎正在使用此库的较新版本,其中方法DependenciesTask.setPathType(String)存在,而命令行依赖于旧版本。

通过查看首选项页面,您可以获得有关Eclipse正在使用的Ant任务版本的更多信息。尝试导航到窗口 - &gt;偏好 - &gt;蚂蚁 - &gt;运行时,并查看外部库的Classpath选项卡。

另一方面,从Ant构建的输出:

[echo] Executing maven build on Developer machine (other machine)
parsing buildfile jar:file:/C:/Apps/ANT/apache-ant-1.9.5/lib/maven-artifact-ant-2.0.4-dep.jar!/org/apache/maven/artifact/ant/antlib.xml with URI = jar:file:/C:/Apps/ANT/apache-ant-1.9.5/lib/maven-artifact-ant-2.0.4-dep.jar!/org/apache/maven/artifact/ant/antlib.xml from a zip file

命令行显然使用的是版本2.0.4,它位于Ant主目录的lib文件夹中。检查this page,最新版本是2.1.3,其中包含缺少的方法。因此,将Ant lib目录中的版本更新为最新版本是有意义的(检查项目页面上的下载链接)。

答案 1 :(得分:3)

你总是可以针对用于eclipse的相同settings.xml运行maven(使用“-s”)

我们可以通过在调试移动中运行maven来获得更多描述:在运行maven时使用“-X”。

答案 2 :(得分:2)

好的,这是确切的问题和解决方案......

我对几件事感到困惑。最初,我不清楚maven-ant-tasks和maven-artifact-ant是同一个库,我认为我需要安装maven-ant-tasks,但是这已经被原始响应清理了。其次,在研究如何更新(或安装)maven-ant-tasks时,我发现这已经在我的配置中发生,并且由于早在2.1.2就出现了正确的方法,我可以看到我们的以下配置系统是正确的,问题不应该发生

我的项目配置代码:

<taskdef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib:org.apache.maven.artifact.ant">
  <classpath>
    <pathelement location="${basedir}/ant/lib/maven-ant-tasks-2.1.3.jar"/>
  </classpath>
</taskdef>

文档支持此处:http://maven.apache.org/ant-tasks/installation.html(代码略有不同,但目的和结果相同)

唉,问题仍在发生......在-verbose转储并比较eclipse构建输出和命令行输出之后,我可以看到唯一的区别是命令行试图同时调用它们文件,当它发生时,只加载了几个所需的方法。

在查看eclipse的ant目录后,我注意到maven-ant-tasks库根本就不存在,所以我尝试从我自己的安装中删除它和Voila!...我本来期望得到多态的结果,但显然这不起作用。