1、父服务为什么要加<packaging>pom</packaging>
- 配置
<packaging>pom</packaging>
的意思是使用maven分模块管理。 packaging
是pom文件一个重要的属性(打包类型),packaging默认类型jar类型。- 一般来说对于所有的父级项目的
packaging
都需要设置为pom
。如果不对父模块做配置,则maven
会将该项目打成jar
包,如果其没有jar包所需的启动类,则会报错。
2、<dependencyManagement></dependencyManagement>
是啥?怎么用?
- 一般只有在最外层的项目中才会存在
dependencyManagement
。 - 它的作用是管理其中的依赖。
- 如果子模块需要继承该父模块
dependencyManagement
中的所有依赖,则直接手动通过<dependency>
引入该父模块就行了。 - 它还有一个用法就是,如果子模块需要引入其中的某个依赖,则只需要写依赖的
<groupId>
和<artifactId>
就行了,不用写版本号<version>
。
最常见的地方就是idea创建 springboot 项目的默认 pom.xml ,其 pom.xml 中的 parent 标签内容为 springboot 的一个版本, Ctrl + 鼠标左键进去,会发现里面也有dependencyManagement,配置了许许多多的依赖。他们都有默认版本。所以我们经常使用 springboot 时,只要其 pom.xml 中有对应 springboot 的 parent 标签,我们写依赖时,绝大部分都不需要写版本号。
直接引入,不需要添加版本号
3、子项目默认依赖父项目的依赖
子项目只要存在于父项目的modules中,则会自动默认依赖父项目中的非dependencyManagement
包含的依赖。
<modules>
<module>service_base</module>
<module>spring_security</module>
</modules>
4、依赖其他非父模块的服务
直接干干干!!!
<dependency>
<groupId>com.tangxz</groupId>
<artifactId>spring_security</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
5、微服务项目打包:
没有主函数的服务中的pom.xml中不允许添加打包插件(项目主工程);
是其它模块依赖的模块(common服务)不允许添加打包插件,否则项目中maven的任何命令都会报错。
打包插件部分:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- 打包跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>