使用Maven构建多模块项目

创建项目

打开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

获取代码

获取完整代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值