maven項(xiàng)目在實(shí)踐中的構(gòu)建管理之路的方法
前言
最近一個(gè)月參與了公司幾個(gè)項(xiàng)目的腳手架構(gòu)建,適當(dāng)總結(jié)下經(jīng)驗(yàn)。之前見(jiàn)過(guò)太多項(xiàng)目依賴,構(gòu)建,管理混亂不堪,導(dǎo)致后續(xù)的維護(hù)性差,甚至出現(xiàn)由此引發(fā)的事故。當(dāng)時(shí)就有一個(gè)規(guī)范管理的想法。
依賴管理
依賴管理,其實(shí)就是依賴范圍的管理。這里我叫他 依賴池。也就是 所有相關(guān)項(xiàng)目的依賴只能從這個(gè)池子里拿,不能超出其范圍。池子里的依賴我們定義為都是久經(jīng)考驗(yàn)的同志。以maven工程為例,我們可以定義 一個(gè)名為ooxx-dependencies 的 pom 類型的工程。這里用來(lái)存放我們經(jīng)過(guò)技術(shù)選型并測(cè)試通過(guò)的依賴。每次依賴變動(dòng)發(fā)布都要有新的版本號(hào)。也就是 依賴池的迭代一定要以版本號(hào)為標(biāo)志,多版本并行。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ooxx</groupId>
<artifactId>ooxx-dependencies</artifactId>
<version>1.0.0.RELEASE</version>
<name>ooxx dependencies</name>
<description>the root dependencies</description>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<springboot.version>2.1.5.RELEASE</springboot.version>
<spring-boot-admin.version>2.1.4</spring-boot-admin.version>
<springSecurityJwt.version>1.0.10.RELEASE</springSecurityJwt.version>
<mysql.version>5.1.47</mysql.version>
<hikari.version>3.2.0</hikari.version>
<hutool.version>4.5.5</hutool.version>
<mybatisplus.version>3.1.1</mybatisplus.version>
<wexin-pay.version>3.2.0</wexin-pay.version>
<wexin-miniapp.version>3.2.0</wexin-miniapp.version>
<swagger.version>2.9.2</swagger.version>
</properties>
<distributionManagement>
<repository>
<id>nexus</id>
<name>Releases</name>
<url>http://url/repository/maven-releases</url>
</repository>
<snapshotRepository>
<id>nexus</id>
<name>Snapshot</name>
<url>http://url/repository/maven-snapshots</url>
</snapshotRepository>
</distributionManagement>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
<!-- 排除Tomcat依賴 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
<version>${springSecurityJwt.version}</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>${hikari.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
<!--swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
然后,我們根據(jù)業(yè)務(wù)會(huì)定義一個(gè)parent項(xiàng)目,這個(gè)項(xiàng)目同樣是pom工程,區(qū)別于依賴池的是, 依賴池基于技術(shù)棧而不關(guān)注業(yè)務(wù),parent關(guān)注于業(yè)務(wù),不同業(yè)務(wù)application 依賴不同的parent,parent 來(lái)定義具體業(yè)務(wù)的module層次劃分。當(dāng)然parent 必須從依賴池構(gòu)建??赡芾痈庇^, 我們有一個(gè)項(xiàng)目,模塊分為:1.后臺(tái)管理模塊 2.app接口模塊 3.通用依賴模塊 4.數(shù)據(jù)層模塊 5.app 啟動(dòng)模塊 可以結(jié)合上面例子進(jìn)行如下構(gòu)建parent
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ooxx</groupId>
<artifactId>ooxx-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>parent</name>
<description>the parent</description>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<ooxx.version>1.0.0</ooxx.version>
<ooxx-dependencies.version>1.0.0.RELEASE</ooxx-dependencies.version>
</properties>
<dependencyManagement>
<dependencies>
<!--依賴池 -->
<dependency>
<groupId>com.ooxx</groupId>
<artifactId>ooxx-dependencies</artifactId>
<version>${ooxx-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--數(shù)據(jù)層模塊 -->
<dependency>
<groupId>com.ooxx</groupId>
<artifactId>ooxx-db</artifactId>
<version>${ooxx.version}</version>
</dependency>
<!--通用依賴模塊 -->
<dependency>
<groupId>com.ooxx</groupId>
<artifactId>ooxx-common</artifactId>
<version>${ooxx.version}</version>
</dependency>
<!-- 后臺(tái)管理模塊-->
<dependency>
<groupId>com.ooxx</groupId>
<artifactId>ooxx-manage-api</artifactId>
<version>${ooxx.version}</version>
</dependency>
<!--app接口模塊 -->
<dependency>
<groupId>com.ooxx</groupId>
<artifactId>ooxx-app-api</artifactId>
<version>${ooxx.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
上述的具體如 app接口模塊 可以直接引用依賴池中的依賴進(jìn)行具體開(kāi)發(fā)。
補(bǔ)充
同時(shí)建議 版本號(hào) 為{數(shù)字}.{說(shuō)明格式}。比如1.0.0.RC、 1.0.0.GA 等用于不同的場(chǎng)景。pom 名稱盡量 模板化 如 ooxx-parent 下的子module 命名為 ooxx-db、ooxx-app-api 之類。這樣可以用maven 模板生成統(tǒng)一的模板項(xiàng)目以快速構(gòu)建項(xiàng)目。同時(shí)達(dá)到 “見(jiàn)其名而知其意”的效果。因個(gè)人能力有限,不足之處或者更好的建議還望多多指教。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
G1垃圾回收器在并發(fā)場(chǎng)景調(diào)優(yōu)詳解
這篇文章主要為大家介紹了G1垃圾回收器在并發(fā)場(chǎng)景調(diào)優(yōu)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04
Mybatis中的mapper是如何和XMl關(guān)聯(lián)起來(lái)的
這篇文章主要介紹了Mybatis中的mapper是如何和XMl關(guān)聯(lián)起來(lái)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
使用Java實(shí)現(xiàn)DNS域名解析的簡(jiǎn)單示例
這篇文章主要介紹了使用Java實(shí)現(xiàn)DNS域名解析的簡(jiǎn)單示例,包括對(duì)一個(gè)動(dòng)態(tài)IP主機(jī)的域名解析例子,需要的朋友可以參考下2015-10-10
使用Spirng Boot Admin監(jiān)控Spring Cloud應(yīng)用項(xiàng)目
這篇文章主要介紹了使用Spirng Boot Admin監(jiān)控Spring Cloud應(yīng)用項(xiàng)目,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
springboot關(guān)閉druid監(jiān)控 druid2改配置文件無(wú)效的解決
這篇文章主要介紹了springboot關(guān)閉druid監(jiān)控 druid2改配置文件無(wú)效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Java利用Picocli開(kāi)發(fā)一個(gè)簡(jiǎn)化命令行工具
Picocli 是一個(gè)強(qiáng)大、易用且功能豐富的 Java 庫(kù),用于開(kāi)發(fā)命令行工具,本文我們就來(lái)為大家介紹一下Java如何利用Picocli進(jìn)行命令行簡(jiǎn)化功能的吧2025-03-03
java讀取excel圖片導(dǎo)入代碼示例(親測(cè)有效)
在日常工作中,我們經(jīng)常要將一些照片插入到Excel表格中,這篇文章主要給大家介紹了關(guān)于java讀取excel圖片導(dǎo)入的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10

