当前位置 博文首页 > li5672的专栏:使用Maven构建多模块项目

    li5672的专栏:使用Maven构建多模块项目

    作者:[db:作者] 时间:2021-08-30 15:59

    创建项目

    打开idea创建一个新的项目,选择Spring Initailizr,下一步下一步,创建好一个项目。
    image.png

    image.png

    创建好之后只保留pom文件,其他全部删除

    image.png

    新增模块

    在根目录上右键新增模块,如下图,选择Module之后和创建项目一样,下一步下一步。

    image.png

    ……

    创建好了三个项目,project项目保留启动类,其他两个项目只保留src目录和pom文件

    image.png

    配置项目

    root-server

    • 修改打包方式
       <packaging>pom</packaging>
    
    • 增加子模块配置
    
        <modules>
           <module>project-service</module>
           <module>project-app</module>
           <module>project-start</module>
       </modules>
    
    • 打包配置
    <build>
           <plugins>
               <!--打包jar-->
               <plugin>
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-jar-plugin</artifactId>
                   <configuration>
                       <!--不打包资源文件-->
                       <excludes>
                           <exclude>config/**</exclude>
                           <exclude>*.xml</exclude>
                           <exclude>*.yml</exclude>
                       </excludes>
                       <archive>
                           <manifest>
                               <addClasspath>true</addClasspath>
                               <!--MANIFEST.MF 中 Class-Path 加入前缀-->
                               <classpathPrefix>lib/</classpathPrefix>
                               <!--jar包不包含唯一版本标识-->
                               <useUniqueVersions>false</useUniqueVersions>
                               <!--指定入口类-->
                               <mainClass>com.lzz.project.start.ProjectStartApplication</mainClass>
                           </manifest>
                           <manifestEntries>
                               <!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
                               <Class-Path>./resources/</Class-Path>
                           </manifestEntries>
                       </archive>
                       <outputDirectory>${project.build.directory}</outputDirectory>
                   </configuration>
               </plugin>
    
               <!--拷贝依赖 copy-dependencies-->
               <plugin>
                   <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-dependency-plugin -->
    
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-dependency-plugin</artifactId>
                   <version>3.1.2</version>
                   <executions>
                       <execution>
                           <id>copy-dependencies</id>
                           <phase>package</phase>
                           <goals>
                               <goal>copy-dependencies</goal>
                           </goals>
                           <configuration>
                               <outputDirectory>
                                   ${project.build.directory}/lib/
                               </outputDirectory>
                           </configuration>
                       </execution>
                   </executions>
               </plugin>
           </plugins>
       </build>
    

    注意指定入口类

    修改子模块

    • 修改parent节点
       <parent>
         <artifactId>root-server</artifactId>
          <groupId>com.lzz</groupId>
          <version>0.0.1-SNAPSHOT</version>
          <relativePath>../pom.xml</relativePath>
      </parent>
    
    • 指定打包方式
        <packaging>jar</packaging>
    
    • 删除build节点

    完整配置

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <parent>
        <artifactId>root-server</artifactId>
         <groupId>com.lzz</groupId>
         <version>0.0.1-SNAPSHOT</version>
         <relativePath>../pom.xml</relativePath>
     </parent>
     <packaging>jar</packaging>
     <groupId>com.lzz</groupId>
     <artifactId>project-app</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <name>project-app</name>
     <description>Demo project for Spring Boot</description>
     <properties>
         <java.version>1.8</java.version>
     </properties>
    </project>
    
    

    以上配置适用所有子模块,复制到所有子模块

    运行代码

    在service中增加一个class

    @Service
    public class ProjectService {
     public String sendMessage() {
         return "maven  test";
     }
    }
    

    在app模块中增加一个Controller

    @RestController
    @RequestMapping("/api/")
    public class ProjectController {
     @Autowired
     private ProjectService projectService;
    
     @GetMapping("test")
     public String mess() {
    
         return projectService.sendMessage();
     }
    }
    

    pom文件中新增

      <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <dependency>
             <groupId>com.lzz</groupId>
             <artifactId>project-service</artifactId>
             <version>0.0.1-SNAPSHOT</version>
         </dependency>
     </dependencies>
    

    启动项目

    • 修改pom文件
    <dependencies>
         <dependency>
             <groupId>com.lzz</groupId>
             <artifactId>project-app</artifactId>
             <version>0.0.1-SNAPSHOT</version>
         </dependency>
     </dependencies>
    
    • 修改启动类
    @SpringBootApplication(scanBasePackages = "com.lzz")
    

    image.png

    • 多环境配置
    spring:
      profiles:
        active: dev
    

    image.png

    devprod要放到直接放到resources中,不能放在子目录中,不知道为什么

    启动代码,访问 http://localhost:8080/api/test

    image.png

    获取代码

    获取完整代码

    cs