Maven optional keyword thorough illustration

EDITORIAL

Originally wanted to write a "how to customize the Spring Boot Starter", but in order to better understand some of the design ideas and key points where the Starter, so some of the details in advance to extract the contents singled out to explain the instructions

In Maven pom.xml, you will often see a similar dependencies in the following code:

<dependency>
  <groupId>sample.ProjectA</groupId>
  <artifactId>Project-A</artifactId>
  <version>1.0</version>
  <scope>compile</scope>
  <optional>true</optional> 
</dependency>

Here's <optional>true</optional>What does it mean?

Mystery optional keyword

The old rules, draw a diagram illustrates the problem:

Since project C using the two classes from the project A (OptionalFeatureAClass) and the project B class (OptionalFeatureBClass). If there is no dependency packageA project C and packageB, the compiler will fail.

project D dependent project C, but for the project D, the class (OptionalFeatureAClass) and class (OptionalFeatureBClass) is an optional feature , so in order to make the final war / ejb package does not contain unnecessary dependencies, the use of <optional>the statement is dependent on the current selected by default and will not be inherited by other projects (like in Java final class can not be inherited, like other classes)

If the project D really need to use project C in OptionalFeatureAClass how to do it? Then we need to add an explicit statement in the pom.xml project D in the project A dependent, continue look:

Project D Project A need to use the OptionalFeatureAClass, you need to add an explicit dependence on Project A Project D in pom.xml file in

This is also good to understand why the Maven why design optional keyword, and assume a persistent database project (Project C) on, in order to fit more persistent type of database design, such as Mysql persistent design (Project A ) and Oracle persistent design (project B), when persistent design our project (project D) use of project C, it is impossible to introduce both the driver and the introduction oracle mysql drive it, so we have to explicitly specify a, this is the truth of

actual case

In the Spring-Boot-Actuator the pom.xml file, there are more than 20 dependent is optional

Because Spring Boot is not possible to rely also not necessary to pack your jar package in the final, so the jar package project uses a spring boot actuator finally generated will not contain more than 20 rely on this jar, where if you want to use one explicitly added to your project just fine

In the next article, custom Spring Boot Starter is this strategy, because the starter is included with specific features for other service projects, similar to the role of Project C of this article, and here you understand the mysteries of optional yet?

Reverse Application

If the Project C does not depend on the introduction of plus <optional>true</optional>, Project D and need to rely on Project C, but only how to use Project A class to do it? Maven is also a solution, using the keyword exclusion, not much to say, on a piece of code to understand:

<dependencies>
    <dependency>
      <groupId>top.dayarch.demo</groupId>
      <artifactId>Project-C</artifactId>
      <exclusions>
        <exclusion>
          <groupId>top.dayarch.demo</groupId>
          <artifactId>Project-B</artifactId>
        </exclusion>
      </exclusions> 
    </dependency>
</dependencies>

to sum up

Here, when you design a functional dependence in the future, you should understand how to design dependencies, and I am here to recommend the use of optional form, in simple terms, dependent on what food you have designed, what to eat their own food, "Cai hold "is like, then we create a standard analog official starter custom ...... blog access returned to normal, welcomed the exchange

Soul questioning

  1. There are a lot of children's shoes project team Gradle build tool, you know how Gradle is represented by it?
  2. Custom starter, you know the structure of the official standard starter is what it?

Improve productivity tools


Welcome to continued public concern number: "arch day a soldier."

  • Dry cutting-edge Java technology sharing
  • Productivity tools summary | Reply "Tools"
  • Analysis of interview questions and answers
  • Technical Data collection | Reply "Information"

Easy to read detective novels thinking fun learning Java technology stack-related knowledge, in line with the simplification of complex issues, problems specific abstract and graphical decomposition of the principle of progressive technical issues, technology continues to update, please stay tuned ......

Guess you like

Origin www.cnblogs.com/FraserYu/p/11796301.html