Brief description
Projects are increasingly moving towards modular development. Using maven to build projects will inevitably involve the relationship between father and son pom. The parent of the parent pom file will inherit the springboot project. In this way, there are many pitfalls in development. Let’s record it briefly.
Before looking at the problem, understand the two tags in maven<dependencyManagement>
and<dependencies>
, skip it if you understand.
maven tag
1、<dependencyManagement>
This actually plays the role of managing the version number of dependency jars. It is generally only used in the top level of the project. If all submodules want to use the jar declared here, you only need to add the corresponding groupId and artifactId to the submodule. There is no need to declare the version number. It should be noted that this is just a dependency declared, not a real download jar. Only when used in the submodule can you download the dependency.
2、<dependencies>
After we introduced a jar package here, if the version number is not added here, then maven will go<dependencyManagement>
Find the jar corresponding to groupId and artifactId. If there is one, inherit it. If there is no one, an error will be reported. At this time, the corresponding jar package will be downloaded in the local repository we configured. At this time, all submodules will inherit all declared jars here by default.
In general, the dependency and version number are declared in it. The dependencies in this tag will not be inherited by the submodule. They are just declarations. Dependencies are directly introduced into the child pom. The specific version number will be found in the parent and son.
The packages of the parent pom are all pom, and the packages of the child project pom are all jars. There are two solutions for quoting pom in parent-child configuration, herespringboot
The project is an example to illustrate the problem.
The first pom configuration
We hope to introduce relevant dependencies in the parent pom, all of which are recorded in<dependencies>
Next, the submodule directly inherits the dependencies of the parent pom. There is no need to introduce dependencies during development in the submodule. However, if there is a module in the project, it may be a single toolkit, and it does not need tospringboot
The dependency of the problem will conflict when it is started. This can be solved in this way. Define the springboot version number in the parent pom, and configure the springboot plug-in dependency as the module launched by the project. Ordinary dao, severce, and common do not need to be introduced. The following configuration files are listed in the file, and the focus is on explaining the problem:
Parent configuration:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."> <modelVersion>4.0.0</modelVersion> <groupId></groupId> <artifactId>demo-api</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>demo-web</module> <module>demo-common</module> <module>demo-service</module> </modules> <properties> <>2.1.</> <>1.8</> <>UTF-8</> <>UTF-8</> <>1.2.47</> <>5.1.6</> </properties> <dependencyManagement> <dependencies> <dependency> <groupId></groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!--Both dependencies will be inherited by submodules--> <dependency> <groupId></groupId> <artifactId>fastjson</artifactId> <version>${}</version> </dependency> <dependency> <groupId></groupId> <artifactId>pagehelper</artifactId> <version>${}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>dev</id> <properties> <>dev</> </properties> <!-- Whether it is default trueIndicates default--> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <!-- Test environment --> <id>test</id> <properties> <>test</> </properties> </profile> <profile> <!-- Production environment --> <id>prod</id> <properties> <>prod</> </properties> </profile> </profiles> </project>
Normal submodule configuration:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."> <parent> <artifactId>demo-api</artifactId> <groupId></groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId></groupId> <artifactId>demo-common</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-common</name> <description>demo-common project</description> <packaging>jar</packaging> </project>
Start class submodule configuration:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."> <modelVersion>4.0.0</modelVersion> <parent> <groupId></groupId> <artifactId>demo-api</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId></groupId> <artifactId>demo-web</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-web</name> <description>demo-web project</description> <properties> <>1.8</> </properties> <dependencies> <!--Introducing submodule relatedjar --> <dependency> <groupId></groupId> <artifactId>demo-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId></groupId> <artifactId>share-read-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId></groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <!--important If not setresource Will cause@@Not foundpomConfiguration in the file--> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <!--Introduced only in startup projectsspringbootPlugin --> <plugin> <groupId></groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId></groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
The second pom configuration
Declare all dependencies in the parent pom, and the child module introduces all required ones:
Parent configuration:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."> <modelVersion>4.0.0</modelVersion> <groupId></groupId> <artifactId>demo</artifactId> <version>3.0.0</version> <properties> <>UTF-8</> <>UTF-8</> <>1.8</> <>1.1.14</> <>1.2.5</> <>1.2.70</> </properties> <!-- Dependency Statement --> <dependencyManagement> <dependencies> <!-- SpringBootDependency configuration--> <dependency> <groupId></groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.</version> <type>pom</type> <scope>import</scope> </dependency> <!--Alibaba database connection pool --> <dependency> <groupId></groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${}</version> </dependency> <!-- pagehelper Pagination plugin --> <dependency> <groupId></groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>${}</version> </dependency> </dependencies> </dependencyManagement> <modules> <module>demo-web</module> <module>demo-common</module> </modules> <packaging>pom</packaging> <dependencies> </dependencies> <build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${}</source> <target>${}</target> <encoding>${}</encoding> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>public</id> <name>aliyun nexus</name> <url>/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id> <name>aliyun nexus</name> <url>/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> <profiles> <profile> <id>dev</id> <properties> <>dev</> </properties> <!-- Whether it is default trueIndicates default--> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <!-- Test environment --> <id>test</id> <properties> <>test</> </properties> </profile> <profile> <!-- Production environment --> <id>prod</id> <properties> <>prod</> </properties> </profile> </profiles> </project>
Normal submodule configuration:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."> <parent> <artifactId>demo</artifactId> <groupId></groupId> <version>1.0.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>demo-system</artifactId> <dependencies> <dependency> <groupId></groupId> <artifactId>demo-common</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> </dependency> </dependencies> </project>
Start class submodule configuration:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."> <parent> <artifactId>demo</artifactId> <groupId></groupId> <version>3.0.0</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <artifactId>demo-admin</artifactId> <description> webServe </description> <dependencies> <dependency> <artifactId>demo</artifactId> <groupId></groupId> <version>1.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId></groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.1.</version> <configuration> <fork>true</fork> <!-- If the configuration is not,devtoolsWill not take effect --> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId></groupId> <artifactId>maven-war-plugin</artifactId> <version>3.0.0</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> <warName>${}</warName> </configuration> </plugin> </plugins> </build> </project>
The above is summarized by an individual in the construction project, for reference and the emphasis is on understanding.
This is the article about the implementation of dependency citations in the maven father and son project. For more related contents of the maven father and son project dependency citations, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!