创建项目
打开idea
创建一个新的项目,选择Spring Initailizr
,下一步下一步,创建好一个项目。
创建好之后只保留pom
文件,其他全部删除
新增模块
在根目录上右键新增模块,如下图,选择Module
之后和创建项目一样,下一步下一步。
……
创建好了三个项目,project
项目保留启动类,其他两个项目只保留src
目录和pom
文件
配置项目
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")
复制代码
- 多环境配置
spring:
profiles:
active: dev
复制代码
dev
和prod
要放到直接放到resources
中,不能放在子目录中,不知道为什么
启动代码,访问 http://localhost:8080/api/test
获取代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END