ClassFormatError ServletException Maven项目错误

ClassFormatError ServletException Maven project error

我有一个有测试的 Maven 项目 class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
    "file:../test-war/src/main/webapp/WEB-INF/applicationContext.xml",
    "file:../test-war/src/main/webapp/WEB-INF/config/servlet-context.xml"
})
@Transactional
public class SpringTest  {

项目是 运行,"mvn clean install" 抛出错误:

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException
  at java.lang.ClassLoader.defineClass1(Native Method)
  at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
  at org.springframework.core.type.StandardAnnotationMetadata.hasAnnotatedMethods(StandardAnnotationMetadata.java:161)

试了很多方法还是不行,有什么想法吗?

这是 pom.xml 的相关依赖项:

 <dependency>
        <groupId>javaee</groupId>
        <artifactId>javaee-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.20.0-GA</version>
        </dependency>
    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-servlet_3.0_spec</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <scope>test</scope>
    </dependency>

以及来自父项的依赖项pom.xml

<dependency>
    <groupId>javaee</groupId>
    <artifactId>javaee-api</artifactId>
    <version>5</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.0.0.GA</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.jboss.ejb3</groupId>
    <artifactId>jboss-ejb3-ext-api</artifactId>
    <version>1.0.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.7.Final</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.7.Final</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-commons-annotations</artifactId>
    <version>3.3.0.ga</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.5.6-Final</version>
    <scope>provided</scope>
</dependency>            
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>3.1.0.GA</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>2.3.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>

插件:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <includes>
                <include>**/MyTestSuite.class</include>
            </includes>
        </configuration>
    </plugin>
</plugins>

尝试了很多选项,删除了测试范围提供的所有选项等,但仍然出现相同的错误。

尝试用surefire plugin configuration<plugin>变成pom.xml。您可以在下面找到插件:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12</version>
    <configuration>
        <classpathDependencyExcludes>
            <!-- exclude code absent api -->
            <classpathDependencyExclude>javax.faces:javax.faces-api</classpathDependencyExclude>
        </classpathDependencyExcludes>
    </configuration>
</plugin>

问题可以通过以下方式消失:

  • Reordering/updating 一些依赖项
  • 正在创建新的依赖项:
<dependency>
    <groupId>org.apache.geronimo.specs</groupId>
    <artifactId>geronimo-ejb_3.0_spec</artifactId>
    <version>1.0.1</version>
    <scope>test</scope>
</dependency>