咱們能夠將模塊劃分爲以下形式:前端
注意:<packaging>pom</packaging> 管理模塊,主要做用是管理整個工程的所有模塊(管理每個項目),當有新模塊加入時須要在 modules
元素下配置對應的模塊目錄java
<?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.funtl</groupId> <artifactId>my-shop</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> </modules> </project>
該項目稱之爲 Root 項目,主要做用是管理整個工程的所有模塊,當有新模塊加入時須要在 modules
元素下配置對應的模塊目錄web
在my-shop工程目錄下建立一個名爲 my-shop-dependencies
的項目,pom.xml
文件以下:spring
<?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> <parent> <groupId>com.funtl</groupId> <artifactId>my-shop</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <artifactId>my-shop-dependencies</artifactId> <packaging>pom</packaging> <name>my-shop-dependencies</name> <description></description> <properties> <!-- 環境配置 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!-- 統一的依賴管理 --> <commons-lang3.version>3.5</commons-lang3.version> <jstl.version>1.2</jstl.version> <log4j.version>1.2.17</log4j.version> <servlet-api.version>3.1.0</servlet-api.version> <slf4j.version>1.7.25</slf4j.version> <spring.version>4.3.17.RELEASE</spring.version> </properties> <dependencyManagement> <dependencies> <!-- Spring Begin --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring End --> <!-- Servlet Begin --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <!-- Servlet End --> <!-- Log Begin --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jul-to-slf4j</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- Log End --> <!-- Commons Begin --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <!-- Commons End --> </dependencies> </dependencyManagement> <build> <plugins> <!-- Compiler 插件, 設定 JDK 版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>${project.build.sourceEncoding}</encoding> <showWarnings>true</showWarnings> </configuration> </plugin> </plugins> <!-- 資源文件配置, javac不會講資源文件打包--> <resources> <resource> <directory>src/main/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build> </project>
PS:別忘記在 my-shop
工程的 pom.xml
中增長 <module>my-shop-dependencies</module>
配置apache
在my-shop工程目錄下建立名爲 my-shop-commons
的項目,pom.xml
文件以下:api
<?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> <parent> <groupId>com.zy</groupId> <artifactId>my-shop-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../my-shop-dependencies/pom.xml</relativePath> </parent> <artifactId>my-shop-commons</artifactId> <packaging>jar</packaging> <name>my-shop-commons</name> <description></description> <!--忽略了版本號,my-shop-dependencies已經控制了版本的管理--> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> </dependencies> </project>
PS:別忘記在 my-shop
工程的 pom.xml
中增長 <module>my-shop-commons</module>
配置mvc
在my-shop工程目錄下建立一個名爲 my-shop-domain
的項目,pom.xml
文件以下:dom
<?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> <parent> <groupId>com.funtl</groupId> <artifactId>my-shop-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../my-shop-dependencies/pom.xml</relativePath> </parent> <artifactId>my-shop-domain</artifactId> <packaging>jar</packaging> <name>my-shop-domain</name> <description></description> </project>
PS:別忘記在 my-shop
工程的 pom.xml
中增長 <module>my-shop-domain</module>
配置maven
在my-shop工程目錄下建立一個名爲 my-shop-web-admin
的項目,pom.xml
文件以下:ide
<?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> <parent> <groupId>com.funtl</groupId> <artifactId>my-shop-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../my-shop-dependencies/pom.xml</relativePath> </parent> <artifactId>my-shop-web-admin</artifactId> <packaging>war</packaging> <name>my-shop-web-admin</name> <description></description> <dependencies> <dependency> <groupId>com.funtl</groupId> <artifactId>my-shop-commons</artifactId> <version>${project.parent.version}</version> </dependency> <dependency> <groupId>com.funtl</groupId> <artifactId>my-shop-domain</artifactId> <version>${project.parent.version}</version> </dependency> </dependencies> </project>
PS:別忘記在 my-shop
工程的 pom.xml
中增長 <module>my-shop-web-ui</module>
配置
在my-shop工程目錄下建立一個名爲 my-shop-web-api
的項目,pom.xml
文件以下:
<?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> <parent> <groupId>com.funtl</groupId> <artifactId>my-shop-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../my-shop-dependencies/pom.xml</relativePath> </parent> <artifactId>my-shop-web-api</artifactId> <packaging>war</packaging> <name>my-shop-web-api</name> <description></description> <dependencies> <dependency> <groupId>com.funtl</groupId> <artifactId>my-shop-commons</artifactId> <version>${project.parent.version}</version> </dependency> </dependencies> </project>
PS:別忘記在 my-shop
工程的 pom.xml
中增長 <module>my-shop-web-api</module>
配置
至此一個完整的模塊化工程建立完畢,此時的 Root 工程 pom.xml
文件以下:
<?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.funtl</groupId> <artifactId>my-shop</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>my-shop-dependencies</module> <module>my-shop-commons</module> <module>my-shop-domain</module> <module>my-shop-web-admin</module> <module>my-shop-web-ui</module> <module>my-shop-web-api</module> </modules> </project>
咱們能夠在 Root 工程中使用 Maven 提供的 mvn clean
命令測試一下效果
檢查環境是否有問題
mvn clean package -Dmaven.test.skip=true