Writing in the front
Originally wanted to write an article on how to customize the standard Spring Boot Starter, but in order to better understand some of the design concepts and key points of Starter, so some details are extracted separately in advance to explain
In Maven POM.xml, you’ll often see dependencies like the following:
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
Copy the code
What does
true
The mystery of the optional keyword
As usual, draw a picture to illustrate the problem:
Project C uses two classes from Project A (OptionalFeatureAClass) and project B (OptionalFeatureBClass). If Project C does not rely on packageA and packageB, the compilation will fail.
Project D relies on Project C, but classes (OptionalFeatureAClass) and classes (OptionalFeatureBClass) are optional features for Project D, So to ensure that the final WAR/EJB package does not contain unnecessary dependencies, use
to declare that the current dependencies are optional and will not be inherited by other projects by default (just as final classes in Java cannot be inherited by other classes).
What if Project D does need to use OptionalFeatureAClass in Project C? We need to explicitly declare project A dependencies in the pom.xml of project D.
Project D requires OptionalFeatureAClass of Project A, so you need to explicitly add A dependency to Project A in Project D’s POP.xml file
It is easy to understand why Maven designed the optional keyword. Suppose a Project on database persistence (Project C) is designed to accommodate more types of database persistence design. For example, Mysql persistence design (Project A) and Oracle persistence design (Project B), when our Project (Project D) will use the persistence design of Project C, It is impossible to import both mysql and Oracle drivers, so we will specify one explicitly
The actual case
In the Spring-boot-Actuator POP. XML file, more than 20 dependencies are optional
Because Spring Boot cannot pack unnecessary dependencies into your final JAR package, the final JAR package generated by projects using Spring Boot does not include these 20 dependent jars. If you need one, add it explicitly to your project
In the following article, we will use this strategy to customize the Spring Boot Starter, because the Starter is a role similar to Project C, which contains specific functions for other projects.
Reverse application
If Project C does not include
true
<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>
Copy the code
conclusion
Now, when you’re designing functional dependencies in the future, you should know how to design dependencies, and I recommend optional, to put it simply, you can design dependencies for any dish you want, you can just “grab CAI Ming” yourself, Next we create a custom starter…… that emulates the official standard Blog access back to normal, welcome to exchange
Soul asking
- Gradle has a lot of build tools for kids. Do you know what Gradle says?
- Custom starter, do you know what the official standard starter structure is?
Efficiency tools
Welcome to continue to pay attention to the public account: “One soldier of The Japanese Arch”
- Cutting-edge Java technology dry goods sharing
- Efficient tool summary | back “tool”
- Interview question analysis and solution
- Technical data to receive reply | “data”
To read detective novel thinking easy fun learning Java technology stack related knowledge, in line with the simplification of complex problems, abstract problems concrete and graphical principles of gradual decomposition of technical problems, technology continues to update, please continue to pay attention to……