Maven:分模块构建工程
作者:互联网
分模块构建maven工程分析
在企业项目开发过程中 由于项目规模庞大,业务复杂,参与的人员比较多,一般会通过合理的模块拆分将一个大型的项目拆分为N多个小模块,分别进行开发。而且拆分出的模块可以非常容易的被其他模块复用。
常见的拆分方式有两种
第一种:
按照业务模块进行拆分,每个模块拆分成一个maven工程,例如将一个项目分为用户模块、订单模块、购物车模块等,每个模块对应就是一个maven工程
第二种:
按照层进行拆分,例如持久层、业务层、表现层等,每个层对应就是一个maven工程
不管是上面哪种拆分方式,通常都会提供一个父工程,将一些公共的代码和配置提取到父工程中进行统一管理和配置。
Maven工程的继承
在Java语言中,类之间是可以继承的,通过继承,子类就可以引用父类中非private的属性和方法。同样,在maven工程之间也可以继承,子工程继承父工程后,就可以使用在父工程中引入的依赖。继承的目的是为了消除重复代码。
被继承的Maven项目中的POM的关键部分定义:
被继承的maven工程通常称为父工程,父工程的打包方式必须为pom,所以我们区分某个maven工程是否为父工程就看这个工程的打包方式是否为pom
继承的Maven项目中的POM的关键部分:
继承其他maven父工程的工程通常称为子工程,在pom.xml文件中通过parent标签进行父工程的继承
优点:
父子项目,合理有效的附庸依赖jar包,子项目互相独立,更加便于敏捷开发和独立管理
缺点:
父子项目,项目之间的系统集成性能比较差
eg:
创建父工程:
pom.xml文件信息:
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.jw</groupId> 8 <artifactId>jw-parent</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>pom</packaging> 11 12 <name>jw-parent</name> 13 <!-- FIXME change it to the project's website --> 14 <url>http://www.example.com</url> 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.8</maven.compiler.source> 19 <maven.compiler.target>1.8</maven.compiler.target> 20 <spring.version>5.3.3</spring.version> 21 </properties> 22 23 <dependencies> 24 <dependency> 25 <groupId>junit</groupId> 26 <artifactId>junit</artifactId> 27 <version>4.11</version> 28 <scope>test</scope> 29 </dependency> 30 <dependency> 31 <groupId>org.springframework</groupId> 32 <artifactId>spring-context</artifactId> 33 <version>${spring.version}</version> 34 </dependency> 35 </dependencies> 36 37 <dependencyManagement> 38 <dependencies> 39 <dependency> 40 <groupId>mysql</groupId> 41 <artifactId>mysql-connector-java</artifactId> 42 <version>8.0.22</version> 43 </dependency> 44 </dependencies> 45 </dependencyManagement> 46 47 <build> 48 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 49 <plugins> 50 <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> 51 <plugin> 52 <artifactId>maven-clean-plugin</artifactId> 53 <version>3.1.0</version> 54 </plugin> 55 <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> 56 <plugin> 57 <artifactId>maven-resources-plugin</artifactId> 58 <version>3.0.2</version> 59 </plugin> 60 <plugin> 61 <artifactId>maven-compiler-plugin</artifactId> 62 <version>3.8.0</version> 63 </plugin> 64 <plugin> 65 <artifactId>maven-surefire-plugin</artifactId> 66 <version>2.22.1</version> 67 </plugin> 68 <plugin> 69 <artifactId>maven-jar-plugin</artifactId> 70 <version>3.0.2</version> 71 </plugin> 72 <plugin> 73 <artifactId>maven-install-plugin</artifactId> 74 <version>2.5.2</version> 75 </plugin> 76 <plugin> 77 <artifactId>maven-deploy-plugin</artifactId> 78 <version>2.8.2</version> 79 </plugin> 80 <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> 81 <plugin> 82 <artifactId>maven-site-plugin</artifactId> 83 <version>3.7.1</version> 84 </plugin> 85 <plugin> 86 <artifactId>maven-project-info-reports-plugin</artifactId> 87 <version>3.0.0</version> 88 </plugin> 89 </plugins> 90 </pluginManagement> 91 </build> 92 </project>
创建子工程:
pom.xml文件信息:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <parent> 8 <groupId>com.jw</groupId> 9 <artifactId>jw-parent</artifactId> 10 <version>1.0-SNAPSHOT</version> 11 <relativePath>../jw-parent/pom.xml</relativePath> 12 </parent> 13 14 <groupId>com.jw</groupId> 15 <artifactId>jw-model</artifactId> 16 <version>1.0-SNAPSHOT</version> 17 18 <dependencies> 19 <dependency> 20 <groupId>mysql</groupId> 21 <artifactId>mysql-connector-java</artifactId> 22 </dependency> 23 </dependencies> 24 25 </project>
Maven工程的聚合
在maven工程的pom.xml文件中可以使用<modules>标签将其他maven工程聚合到一起,聚合的目的是为了进行统一操作。
例如拆分后的maven工程有多个,如果要进行打包,就需要针对每个工程分别执行打包命令,操作起来非常繁琐。这时就可以使用<modules>标签将这些工程统一聚合到maven工程中,需要打包的时候,只需要在此工程中执行一次打包命令,其下被聚合的工程就都会被打包了。
优点:
项目之间的整体性较高,便于系统集成和维护。
创建父工程jw-aggregation,和上面类似;
父工程的 src目录不需要,可以删除
创建子工程:(Module)
子工程pom文件:
此时父工程:
可以创建多个module
对父模块进行编译和打包,会同步对子模块操作:
对jw-aggregation进行compile操作信息:
1 C:\Java\jdk1.8.0_251\bin\java.exe -Dmaven.multiModuleProjectDirectory=D:\myCode\JavaPro\jw-aggregation -Dmaven.home=D:\workTools\apache-maven-3.6.3 -Dclassworlds.conf=D:\workTools\apache-maven-3.6.3\bin\m2.conf "-Dmaven.ext.class.path=D:\workTools\JetBrains\IntelliJ IDEA 2020.1.3\plugins\maven\lib\maven-event-listener.jar" "-javaagent:D:\workTools\JetBrains\IntelliJ IDEA 2020.1.3\lib\idea_rt.jar=54736:D:\workTools\JetBrains\IntelliJ IDEA 2020.1.3\bin" -Dfile.encoding=UTF-8 -classpath D:\workTools\apache-maven-3.6.3\boot\plexus-classworlds-2.6.0.jar;D:\workTools\apache-maven-3.6.3\boot\plexus-classworlds.license org.codehaus.classworlds.Launcher -Didea.version2020.1.3 -s D:\myCode\mavenConf\settings.xml compile 2 [INFO] Scanning for projects... 3 [INFO] ------------------------------------------------------------------------ 4 [INFO] Reactor Build Order: 5 [INFO] 6 [INFO] jw-aggregation [pom] 7 [INFO] jw-web [jar] 8 [INFO] jw-center [jar] 9 [INFO] jw-service [jar] 10 [INFO] 11 [INFO] -----------------------< com.jw:jw-aggregation >------------------------ 12 [INFO] Building jw-aggregation 1.0-SNAPSHOT [1/4] 13 [INFO] --------------------------------[ pom ]--------------------------------- 14 [INFO] 15 [INFO] ---------------------------< com.jw:jw-web >---------------------------- 16 [INFO] Building jw-web 1.0-SNAPSHOT [2/4] 17 [INFO] --------------------------------[ jar ]--------------------------------- 18 [INFO] 19 [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ jw-web --- 20 [INFO] Using 'UTF-8' encoding to copy filtered resources. 21 [INFO] skip non existing resourceDirectory D:\myCode\JavaPro\jw-aggregation\jw-web\src\main\resources 22 [INFO] 23 [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ jw-web --- 24 [INFO] Changes detected - recompiling the module! 25 [INFO] Compiling 1 source file to D:\myCode\JavaPro\jw-aggregation\jw-web\target\classes 26 [INFO] 27 [INFO] --------------------------< com.jw:jw-center >-------------------------- 28 [INFO] Building jw-center 1.0-SNAPSHOT [3/4] 29 [INFO] --------------------------------[ jar ]--------------------------------- 30 [INFO] 31 [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ jw-center --- 32 [INFO] Using 'UTF-8' encoding to copy filtered resources. 33 [INFO] skip non existing resourceDirectory D:\myCode\JavaPro\jw-aggregation\jw-center\src\main\resources 34 [INFO] 35 [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ jw-center --- 36 [INFO] Changes detected - recompiling the module! 37 [INFO] Compiling 1 source file to D:\myCode\JavaPro\jw-aggregation\jw-center\target\classes 38 [INFO] 39 [INFO] -------------------------< com.jw:jw-service >-------------------------- 40 [INFO] Building jw-service 1.0-SNAPSHOT [4/4] 41 [INFO] --------------------------------[ jar ]--------------------------------- 42 [INFO] 43 [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ jw-service --- 44 [INFO] Using 'UTF-8' encoding to copy filtered resources. 45 [INFO] skip non existing resourceDirectory D:\myCode\JavaPro\jw-aggregation\jw-service\src\main\resources 46 [INFO] 47 [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ jw-service --- 48 [INFO] Changes detected - recompiling the module! 49 [INFO] Compiling 1 source file to D:\myCode\JavaPro\jw-aggregation\jw-service\target\classes 50 [INFO] ------------------------------------------------------------------------ 51 [INFO] Reactor Summary for jw-aggregation 1.0-SNAPSHOT: 52 [INFO] 53 [INFO] jw-aggregation ..................................... SUCCESS [ 0.003 s] 54 [INFO] jw-web ............................................. SUCCESS [ 1.279 s] 55 [INFO] jw-center .......................................... SUCCESS [ 0.037 s] 56 [INFO] jw-service ......................................... SUCCESS [ 0.036 s] 57 [INFO] ------------------------------------------------------------------------ 58 [INFO] BUILD SUCCESS 59 [INFO] ------------------------------------------------------------------------ 60 [INFO] Total time: 1.466 s 61 [INFO] Finished at: 2021-08-13T00:38:48+08:00 62 [INFO] ------------------------------------------------------------------------
package操作:
install操作:
标签:INFO,maven,工程,plugin,jw,Maven,构建,模块,resources 来源: https://www.cnblogs.com/luomu4412/p/15131272.html