我的项目结构
父工程:online-education
子模块:service_edu
子模块(我将它作为公共模块): api-commons
我需要在service_edu模块中引入api-commons模块,首先我在service_edu模块的pom文件中引入
<!--引入公共模块依赖-->
<dependency>
<groupId>com.IT.online-education</groupId>
<artifactId>api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
然后我在service_edu模块的主启动类上加上扫描路径
package com.IT.education;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"com.IT"})
public class EduApplication {
public static void main(String[] args) {
SpringApplication.run(EduApplication.class,args);
}
}
然后我将api-commons模块先clean然后install,最后启动service_edu时,出现了下面的错误
显示找不到这个类,在网上查找问题后发现是打包方式的问题,于是检查了父工程和两个子工程的pom文件,两个子工程中是没有加的,只有父工程中加入了,并且代码如下,我把它分为两块,一块是maven,另一块是spring-boot-maven,然后将第二块全部删除,再重新clean和install,终于解决了!
<build>
<plugins>
<!--第一块-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!--第二块-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<mainClass>com.it.guliparent.GuliParentApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>