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.

How can I Create a Separate artifactid for the Stubs?

如果您创建单独的 artifactid,则可以以任何您希望的方式对其进行设置。例如,您可能决定根本不使用任何依赖项。

If you create a separate artifactid, you can set it up in whatever way you wish. For example, you might decide to have no dependencies at all.

How can I Exclude Dependencies on the Consumer Side?

作为消费者,如果将 Stub 依赖项添加到您的 classpath 中,则可以明确排除不需要的依赖项。

As a consumer, if you add the stub dependency to your classpath, you can explicitly exclude the unwanted dependencies.