英文:
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" (<optional>true</optional>
)
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" (<optional>true</optional>
)
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...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论