来源于公众未读代码 ,

作者达西呀

创建项目

创建一个 SpringBoot 项目非常的简单,简单到这里根本不用再提。你可以在使用 IDEA 新建项目时直接选择 Spring Initlalize 创建一个 Spring Boot 项目,也可以使用 Spring 官方提供的 Spring Boot 项目生成页面得到一个项目。

下面介绍一下使用 Spring 官方生成的方式,如果你已经有了一个 Spring Boot 项目,这部分可以直接跳过

  1. 打开 https://start.spring.io/
  2. 填写 group 和 Artifact 信息,选择依赖(我选择了 Spring Web 和 Lombok )。spring 官网创建初始项目
  3. 点击 Generate 按钮下载项目。
  4. 打开下载的项目,删除无用的 .mvn 文件夹,mvnw 、 mvnw.cmd 、HELP.md 文件。

到这里已经得到了一个 Spring Boot 初始项目了,我们直接导入到 IDEA 中,看一眼 pom.xml 的内容。

<?xml version="1.0" encoding="UTF-8"?>4.0.0org.springframework.bootspring-boot-starter-parent2.2.5.RELEASEcom.wdbytespringboot-module-demo0.0.1-SNAPSHOTspringboot-module-demoDemo project for Spring Boot1.8org.springframework.bootspring-boot-starter-weborg.projectlomboklomboktrueorg.springframework.bootspring-boot-starter-testtestorg.junit.vintagejunit-vintage-engineorg.springframework.bootspring-boot-maven-plugin

把目录结构调整成自己想要的结构,然后添加 controller 和 entity 用于测试。

项目目录结构

ProductController 类源代码。

@RestController@RequestMapping("/product")publicclass ProductController {    /**     * 获取商品列表     *     * @return     */    @GetMapping("/list")    public Map list() {        // 模拟查询商品逻辑        Product product = new Product();        product.setProductName("小米粥");        product.setProductPrice(new BigDecimal(2.0));        product.setProductStock(100);        Map resultMap = new HashMap<>();        resultMap.put("code", 000);        resultMap.put("message", "成功");        resultMap.put("data", Arrays.asList(product));        return resultMap;    }}

Product 类源代码。

@Datapublicclass Product {    /** 商品名称. */    private String productName;    /** 商品价格. */    private BigDecimal productPrice;    /** 商品库存。*/    privateint productStock;}

模块化

借助 IDEA 工具可以快速的把项目改造成 maven 多模块,这里我们把准备测试 demo 拆分为 common 和 web 两个模块,common 模块存放实体类。web 模块存放 controller 层(这里项目虽小,拆分只是为了演示)。话不多说,直接开始。

  1. 配置主 pom.xml 打包方式 为 pom<?xml version="1.0" encoding="UTF-8"?>xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">4.0.0pom
    ....
    ....
  2. 创建 common 模块项目直接 new -> module。创建模块选择 maven -> next,填写模块名称。填写模块名称继续 next 完成模块创建。
  3. 创建 web 模块web 模块的创建和 common 模块如出一辙,不再赘述。完成两个模块的创建之后,你会发现你的主 pom.xml 文件里自动添加了 module 部分。product-commonproduct-web
  4. 移动代码到指定模块移动 Product.java 到 product-common 模块,其他部分代码和 resource 部分直接移动到 product-web 模块,移动完后你的代码结构是这个样子。多模块目录结构

到这里,多模块已经拆分完成了, 但是 ProductController 代码里的红色警告让你发现事情还没有结束。

依赖管理

处理依赖问题

你发现了代码里的红色警告,不过你也瞬间想到了是因为把 Product 类移动到了 product-common 模块,导致这里引用不到了。

红色警告

然后你查看了下 product-common 模块的 pom.xml 里的内容。

<?xml version="1.0" encoding="UTF-8"?>        springboot-module-demo        com.wdbyte0.0.1-SNAPSHOT4.0.0    product-common

机智的在 Product-web 模块的 pom.xml 里引入 product-common,手起键落,轻松搞定。

<?xml version="1.0" encoding="UTF-8"?>        springboot-module-demo        com.wdbyte0.0.1-SNAPSHOT4.0.0    product-web    com.wdbyte            product-common        

满心欢喜的你快速的点击 Build-> Build Project,得到的 Error 警告刺痛了顶着黑眼圈的你。

不过你还是迅速定位了问题,查看 maven 依赖,你发现是因为没有指定 product-common 依赖的版本号。

报错信息

原来如此,因为没有指定版本号,我们指定上不就完事了嘛。在最外层的主 pom.xml 中添加 添加上指定依赖和要指定的版本号。

com.wdbyte                product-common                0.0.1-SNAPSHOT

刷新 maven ,发现项目已经不报错了,编译成功,运行启动类,熟悉的 Spring logo 又出现在眼前。

优化依赖

是的,Spring Boot 应用在改造成多模块后成功运行了起来,但是你貌似发现一个问题,模块 common 和模块 web 都继承了主 pom ,主 pom 中有 Lombok 、Spring Boot Web 和 Spring Boot Test 依赖,而 common 模块里只用到了 Lombok 啊,却一样继承了 Spring Boot 其他依赖,看来还是要改造一把。

  1. 只有 common 模块用到的依赖移动到 common 模块。<?xml version="1.0" encoding="UTF-8"?>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">
    springboot-module-democom.wdbyte0.0.1-SNAPSHOT4.0.0
    product-commonorg.projectlombok
    lomboktrue
  2. 只有 web 模块用到的依赖移动到 web 模块。<?xml version="1.0" encoding="UTF-8"?>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">
    springboot-module-democom.wdbyte0.0.1-SNAPSHOT4.0.0
    product-webcom.wdbyte
    product-commonorg.springframework.boot
    spring-boot-starter-weborg.springframework.boot
    spring-boot-starter-testtestorg.junit.vintage
    junit-vintage-engine
  3. 抽取用到的版本号到 ,这里抽取 common 模块的依赖版本。到这里最外层主 pom 的内容是这样的。<?xml version="1.0" encoding="UTF-8"?>xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">4.0.0pomproduct-commonproduct-weborg.springframework.boot
    spring-boot-starter-parent2.2.5.RELEASEcom.wdbyte
    springboot-module-demo0.0.1-SNAPSHOTspringboot-module-demoDemo project for Spring Boot1.80.0.1-SNAPSHOTcom.wdbyte
    product-common${product-common.version}org.springframework.boot
    spring-boot-maven-plugin

看似完美,重新 Build-> Build Project ,发现一切正常,运行发现一切正常,访问正常。

访问接口

打包编译

好了,终于到了最后一步了,你感觉到胜利的曙光已经照到了头顶,反射出耀眼的光芒。接着就是 mvn package。

[INFO] springboot-module-demo ............................. SUCCESS [  2.653 s][INFO] product-common ..................................... FAILURE [  2.718 s][INFO] product-web ........................................ SKIPPED[INFO] ------------------------------------------------------------------------[INFO] BUILD FAILURE[INFO] ------------------------------------------------------------------------[INFO] Total time: 6.084 s[INFO] Finished at: 2020-03-19T08:15:52+08:00[INFO] Final Memory: 22M/87M[INFO] ------------------------------------------------------------------------[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.5.RELEASE:repackage (repackage) on project product-common: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.2.5.RELEASE:repackage failed: Unable to find main class -> [Help 1][ERROR]

ERROR 让你伤心了,但是你还是从报错中寻找到了一些蛛丝马迹,你看到是 spring-boot-maven-plugin 报出的错误。重新审视你的主 pom 发现 编译插件用到了 spring-boot-maven-plugin。

org.springframework.boot                spring-boot-maven-plugin            

略加思索后将这段移动到 web 模块的 pom,因为这是 Spring Boot 的打包方式,现在放在主 pom 中所有的模块都会继承到,那么对于 common 模块来说是肯定不需要的。

移动后重新打包,不管你是运行命令 mvn package 还是双击 IDEA 中的 maven 管理中的 package ,想必这时候你都已经打包成功了

IDEA 打包

在 web 模块下的目录 target 里也可以看到打包后的 jar 文件 product-web-0.0.1-SNAPSHOT.jar。可以使用 java 命令直接运行。

$ springboot-module-demoproduct-webarget>java -jar product-web-0.0.1-SNAPSHOT.jar  .   ____          _            __ _ _ / / ___'_ __ _ _(_)_ __  __ _    ( ( )___ | '_ | '_| | '_ / _` |     /  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |___, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::        (v2.2.5.RELEASE)2020-03-19 08:33:03.337  INFO 15324 --- [           main] com.wdbyte.Application                   : Starting Application v0.0.1-SNAPSHOT on DESKTOP-8SCFV4M with PID 15324 (C:甥敳獲83981Desktopspringboot-module-demoproduct-webargetproduct-web-0.0.1-SNAPSHOT.jar started by 83981 in C:甥敳獲83981Desktopspringboot-module-demoproduct-webarget)2020-03-19 08:33:03.340  INFO 15324 --- [           main] com.wdbyte.Application                   : No active profile set, falling back to default profiles: default2020-03-19 08:33:04.410  INFO 15324 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)2020-03-19 08:33:04.432  INFO 15324 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]2020-03-19 08:33:04.432  INFO 15324 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]2020-03-19 08:33:04.493  INFO 15324 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext2020-03-19 08:33:04.493  INFO 15324 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1107 ms2020-03-19 08:33:04.636  INFO 15324 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'2020-03-19 08:33:04.769  INFO 15324 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''2020-03-19 08:33:04.772  INFO 15324 --- [           main] com.wdbyte.Application                   : Started Application in 1.924 seconds (JVM running for 2.649)2020-03-19 08:33:07.087  INFO 15324 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

想必少了点什么,多模块不仅为了结构清晰,更是为了其他项目可以复用模块(如 common 模块),现在这个时候如果你新打开了一个项目,依赖 common 发现是引用不到的,因为你需要把模块安装到本地仓库。可以点击 IDEA -> Maven -> install,也可以通过 maven 命令。

# -Dmaven.test.skip=true 跳过测试# -U 强制刷新# clean 清理缓存# install 安装到本地仓库$ springboot-module-demo> mvn -Dmaven.test.skip=true -U clean install

重新引入发现没有问题了。

文中代码已经上传到 Github:niumoo/springboot

springboot导入项目依赖报错_最详细的 Spring Boot 多模块开发与排坑指南相关推荐

  1. spring的sanpshot报错_最详细的 Spring Boot 多模块开发与排坑指南

    创建项目 创建一个 SpringBoot 项目非常的简单,简单到这里根本不用再提.你可以在使用 IDEA 新建项目时直接选择 Spring Initlalize 创建一个 Spring Boot 项目 ...

  2. springboot导入项目依赖报错_使用Spring Boot很简单,go!!!

    Spring Boot依赖 使用Spring Boot很简单,先添加基础依赖包,有以下两种方式 1. 继承spring-boot-starter-parent项目    org.springframe ...

  3. springboot导入项目依赖报错_如何解决spring boot 项目导入依赖后代码报错问题

    如何解决spring boot 项目导入依赖后代码报错问题 2020-08-15  14:17:18 代码截图如图所示(由于本人问题已经解决,没来得及截图,所以在网上找了一张图片) ​ 针对图中所示的 ...

  4. idea导入maven项目依赖报错_解决Maven依赖冲突的好帮手,这款IDEA插件了解一下?

    1.何为依赖冲突 Maven是个很好用的依赖管理工具,但是再好的东西也不是完美的.Maven的依赖机制会导致Jar包的冲突. 举个例子,现在你的项目中,使用了两个Jar包,分别是A和B.现在A需要依赖 ...

  5. SpringBoot:运行项目是报错org.apache.ibatis.builder.IncompleteElementException:

    本人在左前后端分离项目时,运算后端的项目,出现报错 org.apache.ibatis.builder.IncompleteElementException: Could not find resul ...

  6. springboot导包显示不存在_基础篇:Spring Boot入门体验(图文教程)

    优质文章,及时送达 什么是 Spring Boot? Spring Boot 是由 Pivotal 团队提供的全新框架.Spring Boot 是所有基于 Spring Framework 5.0 开 ...

  7. springboot 上传文件解析入库_十五分钟用Spring Boot实现文件上传功能

    Spring Boot最好的学习方法就是实战训练,今天我们用很短的时间启动我们第一个Spring Boot应用,并且制作一个文件上传系统, 用户可以将本地文件上传到服务器上.我将假设读者为几乎零基础, ...

  8. IDEA导入项目时候报错怎么办?

    1.可能是环境没弄好,把相关依赖导入进去就好了. 2.如果是maven项目,先检查一下maven的设置,然后pom.xml检查一下,更新maven项目就好了. 3.如果其他项目能运行,把代码拷贝进去能 ...

  9. jwt认证机制优势和原理_最详细的Spring Boot 使用JWT实现单点登录

    Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点的单点登录(S ...

最新文章

  1. uva140 Bandwidth
  2. SqlServer2005/2008下sysproperties无效的解决办法
  3. 什么原因可能导致主备延迟?
  4. 《Debug Hacks》和调试技巧【转】
  5. ASP.NET MVC @helper使用说明
  6. linux过滤端口抓包_Linux抓包工具tcpdump使用总结,WireShark的过滤用法
  7. zookeeper学习02 使用
  8. javascript闭包新认识
  9. 学生成绩管理系统(C语言版)
  10. 几个实用PPT排版技巧,让幻灯片不在枯燥
  11. 在线世界地图生成器 pixelmap可调色
  12. CRM八面体:客户关系管理成功案例2 Yorkshire Water
  13. android系统佳明app,佳明garmin运动手表app
  14. 阿里java技术专家是p几
  15. 传腾讯计划出售美团全部股权,知情人士辟谣;苹果证实iOS 16要大量推送广告;Linux 6.0-rc1 发布|极客头条
  16. E.Neko and Flashback
  17. 批处理(bat)打开之后闪退怎么办?
  18. 利用c++进行程序词法分析
  19. vivado对mcs文件固化
  20. [网络安全自学篇] 五十.虚拟机基础之安装XP系统、文件共享、网络快照设置及Wireshark抓取BBS密码

热门文章

  1. 用python配置文件_使用。Python中的Py配置文件,python
  2. ubuntu自定义安装里怎么选_中央空调到底应该怎么选?小户型也能安装中央空调?行家说实话了...
  3. mysql分页插件springboot_SpringBoot--使用Mybatis分页插件
  4. 职高计算机选修6知识点,(计算机基础考试7.doc
  5. 数据库实例:mysql与redis结合用户登录
  6. Ubuntu从零安装 Hadoop And Spark
  7. 离散系数的计算公式_如何求不同变量之间的离散程度
  8. php 将颜色透明度,css中如何使颜色透明度
  9. torch.nn.Module()
  10. 写了一篇关于 NLP 综述的综述!