整个org.springframework的Maven依赖

时间:2011-06-18 19:35:02

标签: spring maven dependencies

如何为所有org.springframework设置Maven依赖关系?

我的意思是,如何在几行中完成,而不是为每个模块提供依赖,例如:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.0.5.RELEASE</version>
</dependency>

等。

谢谢你的帮助

8 个答案:

答案 0 :(得分:12)

你可以这样做

<properties>
    <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
</properties>

<dependencies>
    <!-- Core utilities used by other modules. Define this if you use Spring 
        Utility APIs (org.springframework.core.*/org.springframework.util.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- Expression Language (depends on spring-core) Define this if you use 
        Spring Expression APIs (org.springframework.expression.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define 
        this if you use Spring Bean APIs (org.springframework.beans.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- Aspect Oriented Programming (AOP) Framework (depends on spring-core, 
        spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- Application Context (depends on spring-core, spring-expression, spring-aop, 
        spring-beans) This is the central artifact for Spring's Dependency Injection 
        Container and is generally always defined -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- Various Application Context utilities, including EhCache, JavaMail, 
        Quartz, and Freemarker integration Define this if you need any of these integrations -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- Transaction Management Abstraction (depends on spring-core, spring-beans, 
        spring-aop, spring-context) Define this if you use Spring Transactions or 
        DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, 
        spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, 
        and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx) 
        Define this if you need ORM (org.springframework.orm.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB, 
        JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans, 
        spring-context) Define this if you need OXM (org.springframework.oxm.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-oxm</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- Web application development utilities applicable to both Servlet and 
        Portlet Environments (depends on spring-core, spring-beans, spring-context) 
        Define this if you use Spring MVC, or wish to use Struts, JSF, or another 
        web framework with Spring (org.springframework.web.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- Spring MVC for Servlet Environments (depends on spring-core, spring-beans, 
        spring-context, spring-web) Define this if you use Spring MVC with a Servlet 
        Container such as Apache Tomcat (org.springframework.web.servlet.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- Spring MVC for Portlet Environments (depends on spring-core, spring-beans, 
        spring-context, spring-web) Define this if you use Spring MVC with a Portlet 
        Container (org.springframework.web.portlet.*) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc-portlet</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- Support for testing Spring applications with tools such as JUnit and 
        TestNG This artifact is generally always defined with a 'test' scope for 
        the integration testing framework and unit testing stubs -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${org.springframework.version}</version>
        <scope>test</scope>
    </dependency>

答案 1 :(得分:11)

此答案针对较新版本4.X.X

如果您想更有效地处理依赖项版本,请在<dependencies></dependencies>标记之前使用此代码。

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

使用 BOM 的好处是您不再需要指定依赖项的版本。因此,您的依赖项应如下所示:

<dependencies>
    <!-- Spring framework -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
</dependencies>

答案 2 :(得分:4)

如其他答案所述,您只想使用实际需要的内容,例如:如果你需要Spring Web框架,那就搞定吧。但是,通过执行一些依赖性分析并仅指定所需依赖项的最高级别,可以极大地简化和减少pom.xml中列出的依赖项集。

例如,假设您具有以下依赖项(请注意,我使用的是Spring EBR存储库而非Maven Central的工件ID;请参阅this article for more info on the difference):

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>org.springframework.context</artifactId>
    <version>${org.springframework-version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>org.springframework.web.servlet</artifactId>
    <version>${org.springframework-version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>org.springframework.transaction</artifactId>
    <version>${org.springframework-version}</version>
</dependency>

尽管如此,Spring Web的东西实际上已经依赖于上下文库,所以你可以删除上下文引用:

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

这将为您提供上下文库而无需特别引用它,因为它是由Web库中的依赖项隐式引入的。

如果您的IDE中有IntelliJ或m2eclipse或类似内容,您可以通过依赖关系层次结构显示或甚至依赖关系图(这基本上是UML图表)在IDE中显示这些依赖关系。

对于独立的Maven,我认为你只是这样做:

mvn dependencies:list

More on the dependencies plugin is on the plugin site.

这种方法使您的依赖关系非常明确,并且您的应用程序占用空间更小,这基本上是其他人警告的内容,但可以减少您必须在pom.xml中列出的依赖项数量,这就是我认为您我试图解决。

答案 3 :(得分:3)

使用包装pom创建模块并列出所有Spring依赖项。称之为SpringDependencies

然后在每个模块中,依赖SpringDependencies模块。这将传递所有Spring依赖项。

答案 4 :(得分:3)

没有办法,也没有办法。你应该只使用你真正需要的罐子,不需要所有弹簧罐。对于2.x,曾经有一个spring.jar但是在我看过的每个项目中都使用了它,它会导致版本冲突问题。

如果您正在使用Spring的任何子项目,请注意有时它们仍会拉动Spring 2.5(例如Spring Batch,我也认为是Spring Web Flow),在这种情况下,您应该使用exclusions标记。 pom.xml中。

第一次组装它不是最方便的,但是你可以在其他项目中重复使用它。

答案 5 :(得分:2)

Maven依赖项中没有任何通配符,并且没有任何工件可以收集所有Spring模块。您的项目是否真的使用所有 Spring模块?

答案 6 :(得分:2)

在Spring 2.x中有一个这样的一体化模块,但它在Spring 3.x中发生的模块重构中无法生存。所以,答案是“不”。

答案 7 :(得分:1)

Spring-context在内部解析其他依赖项。请在下面找到依赖树。

[INFO] com.example:springpractice:jar:1.0-SNAPSHOT
[INFO] \- org.springframework:spring-context:jar:4.3.7.RELEASE:compile
[INFO]    +- org.springframework:spring-aop:jar:4.3.7.RELEASE:compile
[INFO]    +- org.springframework:spring-beans:jar:4.3.7.RELEASE:compile
[INFO]    +- org.springframework:spring-core:jar:4.3.7.RELEASE:compile
[INFO]    |  \- commons-logging:commons-logging:jar:1.2:compile
[INFO]    \- org.springframework:spring-expression:jar:4.3.7.RELEASE:compile