【问题标题】:Maven dependency for whole org.springframework整个 org.springframework 的 Maven 依赖项
【发布时间】:2011-09-17 21:52:35
【问题描述】:

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

我的意思是,如何在几行中做到这一点,而不是为每个模块提供依赖关系,例如:

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

等等。

谢谢你的帮助

【问题讨论】:

  • 不知道有没有
  • 我删除了实体框架标签,因为它是 MS 技术 - .NET 框架的一部分,与此问题无关。
  • 找不到单独的“框架”词用于标记
  • 'framework' 不会真的是一个有用的标签恕我直言。

标签: spring maven dependencies


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 这也可以让你找出你真正需要哪些模块。批处理呢?网络流量?安全吗?
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        【解决方案5】:

        正如其他答案中所述,您只想使用您实际需要的东西,例如如果你需要 Spring Web 框架,那么就得到它。但是,您可以通过进行一些依赖分析并仅指定所需依赖的最高级别,大大简化和减少 pom.xml 中列出的依赖集。

        例如,假设您有以下依赖项(请注意,我使用的是 Spring EBR 存储库的工件 ID,而不是 Maven Central;请参阅 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 或类似的东西,你可以在 IDE 中直接显示这些依赖关系,或者通过依赖层次结构显示,甚至在依赖关系图中,这基本上是一个 UML 图表。

        对于独立的 Maven,我认为您只需这样做:

        mvn dependencies:list
        

        More on the dependencies plugin is on the plugin site.

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

        【讨论】:

        • 谢谢你,我做了这样的方式使用的库,我真的需要。问题在于 spring-web 依赖。
        【解决方案6】:

        你可以这样做

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

        【讨论】:

          【解决方案7】:

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

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

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

          【讨论】:

          • 哇真的很喜欢这个解决方案。刚改了版本,又重新下载了新版本的jars
          【解决方案8】:

          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
          

          【讨论】:

            猜你喜欢
            • 2016-07-04
            • 2017-06-01
            • 2013-03-31
            • 2013-06-22
            • 1970-01-01
            • 1970-01-01
            • 2016-05-24
            • 2012-11-18
            • 1970-01-01
            相关资源
            最近更新 更多