背景
最近在设计和开发部门的基础架构,其中一项工作是要约束各个项目所依赖jar包版本,而实现这个目标最好的方式就是自定义parent。
添加自定义属性
<properties>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
jar包依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>26.0-jre</version>
</dependency>
</dependencies>
</dependencyManagement>
添加子模块
<modules>
<module>yidian-boot-client-starter</module>
<module>yidian-boot-commons</module>
<module>yidian-boot-interfaces-starter</module>
</modules>
插件依赖
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.1.RELEASE</version>
<configuration>
<finalName>${jarFilename}</finalName>
<mainClass>${mainClass}</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
如何引入parent?
<parent>
<groupId>com.sang.main</groupId>
<artifactId>Parent-Moduel</artifactId>
<version>1.0.2</version>
<relativePath>../pom.xml</relativePath> <!--本例中此处是可选的-->
</parent>
如何引用parent中定义的依赖?
在子项目中如下所示,引入pom依赖即可保证与parent中定义的jar包版本一致:
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies>
如何引用parent中定义的插件?
在子项目中如下所示,声明插件,即可保证与parent中定义的插件版本和参数一致:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
网友评论