【Maven】Idea搭建多Module项目问题总结

一、项目搭建

这些文章不错:

SpringBoot+Mybatis多模块(module)项目搭建教程

SpringBoot之多module项目创建

搭建完毕应用分层如下:

           

  • api:开放接口层,可直接封装service方法暴露成rpc接口;通过web封装成http接口
  • common:公共服务层,定义常用的公用类,如config参数配置、constants常量配置、enum枚举分类、exceptions异常定义、support公用支持、utils工具类等
  • dao:数据访问层,定义mapper文件、数据访问接口等,与底层mysql、oracle进行数据交互
  • manager层:通用业务处理层,其实可以简单滴认为就是中间件层(或第三方平台封装层),定义项目用到的中间件,如redis、zookeeper、es或第三方接口的封装等
  • service层:业务层,具体业务逻辑服务层
  • web层:mvc层,开放接口的实现、页面定义和web入口

二、问题总结

问题1:项目启动后就立即关闭(Process finished with exit code 0)

idea搭建maven项目时,会默认一个springboot版本,笔者是version:2.1.5,网上描述该版本仍存在一些bug,故尝试使用以前稳定版本,即在pom.xml中修改配置,如<version>2.1.4.RELEASE</version>,测试后发现正常,项目启动后能够等待服务

问题2:包冲突

项目搭建过程中遇到spring-boot-starter-web包冲突的问题,包冲突有两个表现:一是右侧maven管理的Dependencies中显示包“unknown”,一是在启动时idea提示让引入相应配置。解决如下

第一步:清除本地的镜像中的包(.m2/repository/org/springframework/spring-web)

第二步:然后清除idea的缓存并重新启动即可(File / Invalidate Caches / Restarts...

问题3:Unable to find main class

Spring Boot Maven Plugin插件进行打包,若未指定mainClass时,打包过程报错“Unable to find main class,此时有两种解决办法

方法1:指定mainClass(此时子module下java目录可为空)

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.example.demo.web.WebApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

方法2:子module中添加相应的 main入口类

public class test {
    public static void main(String args[]){};
}

这篇文章不错:Spring Boot Maven Plugin打包异常及三种解决方法:Unable to find main class

问题4:循环依赖问题

打包编译过程时,出现了循环依赖问题。感觉很疑惑,除子module继承主module外,其实并没有任何相互依赖的地方,最后发现是主module要使用<dependencyManagement>来管理声明应用中所用到的包

这篇文章不错: Maven 中 dependencyManagement 标签使用

The projects in the reactor contain a cyclic reference: Edge between 'Vertex{label='com.example:test:0.0.1-SNAPSHOT'}' and 'Vertex{label='com.example:test-service:0.0.1-SNAPSHOT'}' introduces to cycle in the graph com.example:test-service:0.0.1-SNAPSHOT --> com.example:test-web:0.0.1-SNAPSHOT --> com.example:test:0.0.1-SNAPSHOT --> com.example:test-service:0.0.1-SNAPSHOT ->

问题5:包扫描配置

service层定义服务时,如@Service、@Controller、@Reposity等标签定义,若想正常使用,则需要在@SpringBootApplication属性中配置启动扫描包,即通过scanBasePackages来定义应用启动时初始化哪些controller

问题6:子module间依赖问题

子module间若存在依赖,对其进行打包编译时,出现依赖包找不到的情况,提示在本地镜像库找不到或者存在缓存什么的,此时对主module打包编译后发布(install)即可

Failed to execute goal on project test-web: Could not resolve dependencies for project com.example:test-web:jar:0.0.1-SNAPSHOT: Could not find artifact com.example:test-service:jar:0.0.1-SNAPSHOT in nexus-releases (http://nexus.dadaabc.us/nexus/content/repositories/public/)

Failed to execute goal on project test-web: Could not resolve dependencies for project com.example:test-web:jar:0.0.1-SNAPSHOT: Failed to collect dependencies at com.example:test-service:jar:0.0.1-SNAPSHOT: Failed to read artifact descriptor for com.example:test-service:jar:0.0.1-SNAPSHOT: Failure to find com.example:test:pom:0.0.1-SNAPSHOT in http://nexus.dadaabc.us/nexus/content/repositories/public/ was cached in the local repository, resolution will not be reattempted until the update interval of nexus-releases has elapsed or updates are forced

其它一些莫名其妙的问题,重启idea,清除缓存试试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值