根据集成应用程序所依赖的Spring版本,选择相应的库。

huangapple go评论88阅读模式
英文:

Library Depending upon integrating application for Spring Version

问题

是的,这是可能的。您可以通过在内部库的依赖中使用Spring的范围来实现这一点。这将允许应用程序在使用内部库时指定所需的Spring版本。

要做到这一点,请在内部库的构建文件(例如Maven的pom.xml或Gradle的构建脚本)中将Spring的依赖范围设置为compile(或implementation,具体取决于您使用的构建工具),而不是将Spring版本硬编码到库中。这将使应用程序能够在其自己的构建文件中指定所需的Spring版本。

以下是一个示例,显示如何在Maven项目的pom.xml中设置依赖范围:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <!-- 使用compile范围,让应用程序决定版本 -->
        <scope>compile</scope>
    </dependency>
    <!-- 其他库的依赖 -->
</dependencies>

通过这样做,您的内部库将依赖于Spring,但不会指定版本。应用程序可以在其自己的构建文件中指定所需的Spring版本,并且构建工具将确保正确的版本被包含在类路径中。这样,您的内部库将与应用程序中使用的Spring版本兼容。

英文:

So I am writing a library for some features and the inner library needs to use Spring. I don't want to put in the version of Spring in the inner library. I want that the application using the library defines the version of Spring to use.

Is this even possible? If yes, then how?

答案1

得分: 0

关于Maven的部分:

"Maven wise, clearly your library needs to be dependent on spring because it has to be compiled somehow so at least spring annotations like @Autowire or @Configuration / @Bean need to be in the compilation classpath.

However in the pom.xml of the library you can declare the dependency on spring as "optional" (&lt;optional&gt;true&lt;/optional&gt;)

So when maven will compile the application that has your library as a dependency won't need to "take" transitively the spring as well

You can read about optional dependencies here. Their example with ProjectA, ProjectB and ProjectX is relevant..."

关于可选依赖的内容,你可以在这里阅读。他们提到的ProjectA、ProjectB和ProjectX的示例是相关的。"

英文:

Maven wise, clearly your library needs to be dependent on spring because it has to be compiled somehow so at least spring annotations like @Autowire or @Configuration / @Bean need to be in the compilation classpath.

However in the pom.xml of the library you can declare the dependency on spring as "optional" (&lt;optional&gt;true&lt;/optional&gt;)

So when maven will compile the application that has your library as a dependency won't need to "take" transitively the spring as well

You can read about optional dependencies here. Their example with ProjectA, ProjectB and ProjectX is relevant...

huangapple
  • 本文由 发表于 2020年5月4日 17:05:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/61588634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定