java.lang.NoSuchMethodError Ant/Maven failing via commandline

2022-09-04 20:36:37

由于 maven 故障,通过命令行进行 ant 构建时出现问题。我可以在 eclipse 中构建而不会出现问题,但是当尝试通过命令行执行时,Maven 会窒息并返回以下错误:

java.lang.NoSuchMethodError: org.apache.maven.artifact.ant.DependenciesTask.setPathType(Ljava/lang/String;)

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

聚甲醛文件

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

答案 1

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

通过检查首选项页面,您可以获得有关 Eclipse 正在使用的 Ant 任务版本的更多信息。尝试导航到 Window -> 首选项 -> Ant -> 运行时,并查看外部库的“类路径”选项卡。

另一方面,从 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 文件夹中。检查此页面,最新版本是2.1.3,其中包含缺少的方法。因此,将 Ant lib 目录中的版本更新为最新版本是有意义的(检查项目页面上的下载链接)。


答案 2

您始终可以针对用于 eclipse 的相同设置.xml运行 maven(使用 “-s”)

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


推荐