How Can I Work with Transitive Dependencies?
Spring Cloud Contract 插件添加了为您创建 Stub jar 的任务。出现的一个问题是,在重新使用 Stub 时,您可能会错误地导入该 Stub 的所有依赖项。在构建 Maven 构件时,即使您有几个不同的 jar,但它们都共享一个 pom.xml
文件,如下表所示:
The Spring Cloud Contract plugins add the tasks that create the stubs jar for you. One
problem that arises is that, when reusing the stubs, you can mistakenly import all of
that stub’s dependencies. When building a Maven artifact, even though you have a couple
of different jars, all of them share one pom.xml
file, as the following listing shows:
├── producer-0.0.1.BUILD-20160903.075506-1-stubs.jar
├── producer-0.0.1.BUILD-20160903.075506-1-stubs.jar.sha1
├── producer-0.0.1.BUILD-20160903.075655-2-stubs.jar
├── producer-0.0.1.BUILD-20160903.075655-2-stubs.jar.sha1
├── producer-0.0.1.BUILD-SNAPSHOT.jar
├── producer-0.0.1.BUILD-SNAPSHOT.pom
├── producer-0.0.1.BUILD-SNAPSHOT-stubs.jar
├── ...
└── ...
有三种处理这些依赖项的方法,这样就不会出现任何传递依赖项问题:
There are three possibilities of working with those dependencies so as not to have any issues with transitive dependencies:
-
Mark all application dependencies as optional
-
Create a separate
artifactid
for the stubs -
Exclude dependencies on the consumer side
How Can I Mark All Application Dependencies as Optional?
如果在 producer
应用程序中,您将所有依赖项标记为可选的,当您在另一个应用程序中包含 producer
Stub(或当该依赖项由 Stub Runner 下载时),因为所有依赖项都是可选的,所以不会下载它们。
If, in the producer
application, you mark all of your dependencies as optional,
when you include the producer
stubs in another application (or when that
dependency gets downloaded by Stub Runner), then, since all of the dependencies are
optional, they do not get downloaded.