Maven不会复制WEB-INF / lib文件夹中的spring jar

时间:2014-08-04 09:15:32

标签: java spring maven

我有一个多模块maven项目:parent-proj有两个子模块:child-dao-projchild-web-projchild-dao-proj是一个maven java项目,而child-web-proj是一个Web项目。 在pom父项目中,我声明了两者所需的所有必需依赖项:

 <modelVersion>4.0.0</modelVersion>
<groupId>fr.myproject</groupId>
<artifactId>parent-proj</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
    <module>child-dao-proj</module>
    <module>child-web-proj</module>
</modules>
<packaging>pom</packaging>

 <dependencyManagement>
    <dependencies>
        <!--Spring dependencies-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>${spring.data.jpa}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>1.8.1.RELEASE</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-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Hibernate Jars -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>


        <!-- Mysql connector-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.31</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.180</version>
        </dependency>

        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Test dependencies-->
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
        </dependency>
    </dependencies>
</dependencyManagement>

我为child-dao-proj中的持久性jar(hibernate,spring-orm,spring-data等)以及{{{spring-mvc,spring-web等)的所有网络jar(spring-mvc,spring-web等)放置了所有必需的依赖项。 1}}。

当我运行mvn clean install并在Tomcat中部署Web项目时,我遇到了这个错误:child-web-proj

搜索后,似乎弹簧罐未复制到java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener目录中。

任何人都知道如何解决此问题?

[编辑]这里是网络项目的pom:

WEB-INF/lib

我忘了提到我正在使用Intellij。

[编辑]以下是<parent> <artifactId>parent-proj</artifactId> <groupId>fr.myproject</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>child-web-proj</artifactId> <packaging>war</packaging> <dependencies> <dependency> <groupId>fr.myproject.dao</groupId> <artifactId>child-dao-proj</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <scope>test</scope> </dependency> </dependencies> 的输出:

mvn dependency:tree

1 个答案:

答案 0 :(得分:0)

这是管理您情况的正确方法:

您可以在父节的dependencyManagement中导入spring-framework-bom,以确保所有spring依赖项都在同一版本中。

    <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.1.0.BUILD-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

在yout war项目中,你可以用这种方式声明依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
<dependencies>
通过这种方式你可以避免重复弹簧版本到每个弹簧的模块