使用maven構建多模組專案
阿新 • • 發佈:2019-01-08
①首先建立一個springboot專案
②再new一個新的模組(新模組依然是springboot專案,同理可以繼續建立需要的新模組)
目錄結構如下圖所示
③構建模組的依賴關係,模組之間的依賴如下圖所示
在父模組的pom檔案中新增專案的所有依賴,子模組可以繼承父模組的依賴。
<dependencies> <!--redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--spring MVC--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--mysql資料庫驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--spring-boot test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--gaoxi-common-service-facade--> <dependency> <groupId>com.gaoxi</groupId> <artifactId>gaoxi-common-service-facade</artifactId> <version>0.0.1</version> </dependency> </dependencies>
注意,父模組的打包方式為pom形式。
注意: 將Common-Service-Facade的打包方式設成jar ,當打包這個模組的時候,Maven會將它打包成jar,並安裝在本地倉庫中。這樣其他模組打包的時候就可以引用這個jar。而且Common-Service-Facade的版本號不能為Snapshot版本,需要明確版本號。
<groupId>com.gaoxi</groupId> <artifactId>gaoxi-common-service-facade</artifactId> <version>0.0.1</version> <packaging>jar</packaging> <name>gaoxi-common-service-facade</name> <description>gaoxi-common-service-facade</description>
Maven的Snapshot版本與Release版本的區別如下:
1. Snapshot版本代表不穩定、尚處於開發中的版本
④最後明確父模組與子模組的依賴關係,在父模組的pom檔案中需要新增如下宣告
<modules>
<module>Gaoxi-User</module>
<module>Gaoxi-Common-Service-Facade</module>
</modules>
在子模組中新增如下宣告
<?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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.gaoxi</groupId> <artifactId>gaoxi-user</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>Gaoxi-User</name> <description>gaoxi-user</description> <parent> <groupId>com.gaoxi</groupId> <artifactId>gaoxi</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> </project>