一,新建parent工程(springboot项目)
删除src目录,只保留(.idea文件夹 , 和项目 pom 文件, 以及一个 *.iml 文件 )
添加以下属性:
<!-- 1.添加子模块 -->
<modules>
<module>gods-common</module>
</modules>
<!-- 2.指定属性 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven-compliler.version>3.6.1</maven-compliler.version>
<maven-surefire.verison>2.9</maven-surefire.verison>
<maven.compile.encoding>UTF-8</maven.compile.encoding>
</properties>
<!-- 3.指定打包方式为pom -->
<packaging>pom</packaging>
<!-- 4.build插件 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compliler.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${maven.compile.encoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire.verison}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
二.新建子模块(maven项目)
<?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">
<parent>
<artifactId>gods-of-rome</artifactId>
<groupId>cn.codeduck</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gods-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--指定模块和模块间的依赖,例如web模块依赖service模块和model模块-->
<dependencies>
<dependency>
<groupId>cn.codeduck</groupId>
<artifactId>gods-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
三,web模块(maven项目)下,新建java和resource文件夹,
java目录下新建包和启动类
启动类:
package cn.codeduck;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GodsApplication {
public static void main(String[] args) {
SpringApplication.run(GodsApplication.class, args);
}
}
resource文件夹下新建static和templates目录,以及application配置文件
application.yaml
server:
#端口号
port: 8080
#tomcat设置
tomcat:
uri-encoding: utf-8
threads:
max: 200
max-swallow-size: 2
max-http-form-post-size: 2
max-connections: 8192
accept-count: 200
#session生命周期
servlet:
session:
timeout: 30m
spring:
#应用设置
application:
name: gods
#配置文件
profiles:
include: dao
application-dao.yaml
spring:
#数据源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/gods?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT%2b8
username: root
password: root
四,相关问题
1.忽略测试进行打包。测试代码不会影响项目发布,但是会影响项目的打包。
mvn clean package -Dmaven.test.skip=true
- 项目打包报错:Non-resolvable parent POM for demo:demo-base:0.0.1-SNAPSHOT: Could not find artifact
demo:demo:pom:0.0.1-SNAPSHOT and 'parent.relativePath' points at no local POM
修改父项目,子Module:demo-base,demo-dao,demo-service,demo-web的 pom 文件,删除<parent></parent>标签的这句 <relativePath/> <!-- lookup parent from repository -->
- 项目打包报错:[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.16.RELEASE:
repackage (default) on project demo-base: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.16.RELEASE:repackage failed: Unable to find main class -> [Help 1]
这是 demo-base 找不到主类的问题。因为此项目为多模块项目,但并不是每一个子模块都需要打成可执行的 Jar 包,如此项目中实际需要打成可执行的Jar 包的只有demo-web,且我们在构建项目之前已经将出demo-web外的项目的启动类即主类删了。所以此处会报错。
解决方法:修改处demo-web外的项目:父项目demo,demo-base、demo-dao、demo-service 的 pom 文件,删除其中关于打包的配置。
4.项目打包报错:
Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.4.5:repackage (repackage) on project gods-model: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.4.5:repackage failed: Unable to find main class
pom文件中,删除spring-boot-maven-plugin插件