SpringBoot多模块开发-使⽤dependencyManagement版本管理
 今天,我们来了解下如何管理和优化⽗项⽬和⼦模块的依赖包版本,⼜或者我们希望⼦项⽬B不需要全部依赖⽗项⽬A的内容,避免⼀股脑全部导⼊。
⾸先,来看四个pom⽂件:
1、demo-parent
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>demo-parent</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modules>
<module>demo-apply</module>
<module>demo-common</module>
<module>demo-static</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<version>3.1</version>
<configuration>
<source>${java.version}</source>    <target>${java.version}</target>    </configuration>
</plugin>
</plugins>
</build>
</project>
2、demo-apply
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>
<artifactId>demo-apply</artifactId>
<packaging>jar</packaging>
<name>demo-apply</name>
<description>Demo project for Spring Boot</description>
spring到底是干啥的<parent>
<groupId>com.demo</groupId>
<artifactId>demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../l</relativePath><!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- 加⼊其余模块的依赖 -->
<dependency>
<groupId>com.demo</groupId>
<artifactId>demo-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.demo</groupId>
<artifactId>demo-static</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 当所依赖的其他模块,有启动类的时候,需要以下配置,指定唯⼀启动类,
本次事例未涉及其他模块有启动类,可⾃⾏测试 -->
<configuration>
<!-- 指定该Main Class为全局的唯⼀⼊⼝ -->
<mainClass>com.demo.demoapply.DemoApplyApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依赖的包都打包到⽣成的Jar包中-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3、demo-static
<artifactId>demo-static</artifactId>
<packaging>war</packaging>
<name>demo-static</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.demo</groupId>
<artifactId>demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../l</relativePath><!-- lookup parent from repository --> </parent>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
4、demo-common
<artifactId>demo-common</artifactId>
<packaging>jar</packaging>
<name>demo-common</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.demo</groupId>
<artifactId>demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../l</relativePath><!-- lookup parent from repository -->
</parent>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
 很明显,我们的demo-parent为⽗项⽬,demo-apply为应⽤⼊⼝,demo-common放的应该是共⽤⼯具类,demo-static应该是共⽤的静态⽂件,如页⾯等,demo-apply依赖两者。
 好,看到这⾥,就有疑问了,按理来说,demo-static,存放页⾯,打包成war包,应该不需要可以运⾏,那么也就不需要依赖springboot的包,那么我们进⾏如下改变:
demo-parent:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>demo-parent</name>
<description>Demo project for Spring Boot</description>
<!-- 删除 -->
<!--<parent>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-parent</artifactId>-->

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。