1

这是我项目的 POM:

<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>learn.spring</groupId>
    <artifactId>someProject</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>someProject</name>
    <url>http://maven.apache.org</url>
    <repositories>
        <repository>
            <id>maven2-repository.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.springframework.version>3.1.0.RELEASE</org.springframework.version>
        <org.slf4j.version>1.5.10</org.slf4j.version>
        <org.slf4j.version>1.5.10</org.slf4j.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>oro</groupId>
            <artifactId>oro</artifactId>
            <version>2.0.8</version>
        </dependency>

        <dependency>
            <groupId>commons-validator</groupId>
            <artifactId>commons-validator</artifactId>
            <version>1.3.1</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>

        </dependency>




        <!-- Jersey + Spring -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.8</version>
        </dependency>


        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-spring</artifactId>
            <version>1.8</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                </exclusion>

                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aop</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>4.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>1.7-beta-1</version>
        </dependency>

        <!-- Log4j Dependency -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.5.8</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.5.8</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.8</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>
        <!-- Postgres dependency -->
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901.jdbc4</version>
        </dependency>

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

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>20030825.184428</version>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>20030825.183949</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>20040616</version>
        </dependency>

        <!-- JSTL dependency -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- Spring Web Support -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>

            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>someProject</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.0-SNAPSHOT</version>
            </plugin>
        </plugins>
    </build>



</project>

我已指定要在构建路径中使用 Spring 版本 3.1.0,但它会自动转换为 Spring 3.0.5.RELEASE。我猜是因为Spring + Jersey依赖。当我尝试启动服务器时,mvn tomcat:run出现以下异常:

java.lang.NoClassDefFoundError: org/springframework/core/env/EnvironmentCapable
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1141)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1612)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1141)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1612)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)
        at java.lang.Class.getDeclaredConstructors0(Native Method)
        at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
        at java.lang.Class.getConstructor0(Class.java:2699)
        at java.lang.Class.newInstance0(Class.java:326)
        at java.lang.Class.newInstance(Class.java:308)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4079)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
        at org.apache.catalina.startup.Embedded.start(Embedded.java:825)
        at org.codehaus.mojo.tomcat.AbstractRunMojo.startContainer(AbstractRunMojo.java:558)
        at org.codehaus.mojo.tomcat.AbstractRunMojo.execute(AbstractRunMojo.java:255)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: java.lang.ClassNotFoundException: org.springframework.core.env.EnvironmentCapable
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491) 

EnvironmentCapable.class 文件存在于spring-core-3.1.0.RELEASE.jar. 我手动将 jar 添加到我的构建路径中,但仍然得到相同的异常。我正在使用 Eclipse 靛蓝。请指教。

4

3 回答 3

4

如果你检查pom.xml里面jersey-spring-1.8.jar,你会看到这个,

    <properties>
        <spring20-release-version>2.0.5</spring20-release-version>
        <spring25-release-version>[2.5.2,3)</spring25-release-version>
    </properties>

这意味着,jersey-spring-1.8 强制您仅使用最多 3 的 spring 版本。我认为这是问题所在。

于 2012-12-27T07:14:57.243 回答
1

是从哪里来<version>${org.springframework.version}</version>的?它没有在您的 POM 文件中定义为属性,所以我猜这个属性来自依赖项之一并接管您的3.1.RELEASE(提示:查看 jersey-spring 依赖项)不要使用属性来定义版本!使用 Maven 的 < DependencyManagement > 部分来控制隐式和显式版本!我建议您使用依赖范围provided

于 2012-12-27T07:17:56.013 回答
0

问题在于 POM 中的 LDAP 依赖项。它将 3.0.5 版本集成到构建路径中。随着mvn dependency:tree它变得明显。

 learn.spring:someProject:war:1.0
 +- junit:junit:jar:3.8.1:test (scope not updated to compile)
 +- oro:oro:jar:2.0.8:compile
 +- commons-validator:commons-validator:jar:1.3.1:compile
 |  +- commons-beanutils:commons-beanutils:jar:1.7.0:compile
 |  \- commons-digester:commons-digester:jar:1.6:compile
 +- org.springframework.ldap:spring-ldap-core:jar:1.3.1.RELEASE:compile
 |  +- commons-logging:commons-logging:jar:1.0.4:compile
 |  +- commons-lang:commons-lang:jar:2.5:compile
 |  +- org.springframework:spring-core:jar:3.0.5.RELEASE:compile
 |  +- org.springframework:spring-beans:jar:3.0.5.RELEASE:compile
 |  \- org.springframework:spring-tx:jar:3.0.5.RELEASE:compile
 +- com.sun.jersey:jersey-server:jar:1.8:compile
 |  +- asm:asm:jar:3.1:compile
 |  \- com.sun.jersey:jersey-core:jar:1.8:compile
 +- com.sun.jersey:jersey-json:jar:1.8:compile
 |  +- org.codehaus.jettison:jettison:jar:1.1:compile
 |  |  \- stax:stax-api:jar:1.0.1:compile
 |  +- com.sun.xml.bind:jaxb-impl:jar:2.2.3-1:compile
 |  |  \- javax.xml.bind:jaxb-api:jar:2.2.2:compile
 |  |     +- javax.xml.stream:stax-api:jar:1.0-2:compile
 |  |     \- javax.activation:activation:jar:1.1:compile
 |  +- org.codehaus.jackson:jackson-core-asl:jar:1.7.1:compile
 |  +- org.codehaus.jackson:jackson-mapper-asl:jar:1.7.1:compile
 |  +- org.codehaus.jackson:jackson-jaxrs:jar:1.7.1:compile
 |  \- org.codehaus.jackson:jackson-xc:jar:1.7.1:compile
 +- com.sun.jersey.contribs:jersey-spring:jar:1.8:compile
 +- javax.servlet:javax.servlet-api:jar:3.0.1:provided
 +- net.sf.jasperreports:jasperreports:jar:4.7.0:compile
 |  +- com.lowagie:itext:jar:2.1.7:compile
 |  |  +- bouncycastle:bcmail-jdk14:jar:138:compile
 |  |  +- bouncycastle:bcprov-jdk14:jar:138:compile
 |  |  \- org.bouncycastle:bctsp-jdk14:jar:1.38:compile
 |  |     +- org.bouncycastle:bcprov-jdk14:jar:1.38:compile
 |  |     \- org.bouncycastle:bcmail-jdk14:jar:1.38:compile
 |  +- jfree:jcommon:jar:1.0.15:compile
 |  +- jfree:jfreechart:jar:1.0.12:compile
 |  +- xml-apis:xml-apis:jar:1.3.02:compile
 |  +- eclipse:jdtcore:jar:3.1.0:compile
 |  +- org.codehaus.castor:castor:jar:1.2:compile
 |  \- org.apache.poi:poi-ooxml:jar:3.7:compile
 |     +- org.apache.poi:poi:jar:3.7:compile
 |     +- org.apache.poi:poi-ooxml-schemas:jar:3.7:compile
 |     |  +- org.apache.xmlbeans:xmlbeans:jar:2.3.0:compile
 |     |  \- org.apache.geronimo.specs:geronimo-stax-api_1.0_spec:jar:1.0:compile
 |     \- dom4j:dom4j:jar:1.6.1:compile
 +- org.codehaus.groovy:groovy-all:jar:1.7-beta-1:compile
 |  +- org.apache.ant:ant:jar:1.7.1:compile
 |  |  \- org.apache.ant:ant-launcher:jar:1.7.1:compile
 |  \- jline:jline:jar:0.9.94:compile
 +- org.slf4j:jcl-over-slf4j:jar:1.5.8:runtime
 +- org.slf4j:slf4j-api:jar:1.5.8:runtime
 +- org.slf4j:slf4j-log4j12:jar:1.5.8:runtime
 +- log4j:log4j:jar:1.2.14:compile
 +- postgresql:postgresql:jar:9.1-901.jdbc4:compile
 +- org.springframework:spring-jdbc:jar:3.1.0.RELEASE:compile
 +- commons-dbcp:commons-dbcp:jar:20030825.184428:compile
 +- commons-pool:commons-pool:jar:20030825.183949:compile
 +- commons-collections:commons-collections:jar:20040616:compile
 +- javax.servlet:jstl:jar:1.2:compile
 +- org.springframework:spring-web:jar:3.1.0.RELEASE:compile
 |  +- aopalliance:aopalliance:jar:1.0:compile
 |  \- org.springframework:spring-context:jar:3.1.0.RELEASE:compile
 |     \- org.springframework:spring-expression:jar:3.1.0.RELEASE:compile
 \- org.springframework:spring-aop:jar:3.1.0.RELEASE:compile
    \- org.springframework:spring-asm:jar:3.1.0.RELEASE:compile

正如aviad建议的那样,添加provided到 LDAP 依赖项效果很好。我添加exclusions了相当繁琐的依赖项。:)

<dependency>
            <groupId>org.springframework.ldap</groupId>
            <artifactId>spring-ldap-core</artifactId>
            <version>1.3.1.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                </exclusion>

                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aop</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-tx</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
于 2012-12-27T10:51:07.773 回答