写这篇博文只是想抒发下感(愤)慨(愤)
我建了个Project,2个module(pc, wx),module都是对同样的实体类进行操作,于是想把持久层独立出来作为公共依赖。
先说正确的做法:
1.再建一个module(base),写好持久层,以jar打包。
2.Project的pom(父类pom)配好dependencyManagement,dependencyManagement下的包将会被module依赖,如base。Idea会自动配好modules。
3.在module(pc, wx)的dependencies中加入base的依赖,与依赖网上的jar包一样。
这样就可以打包了,很简单。
建议用Idea,会自动加入module之间的依赖。
之前不懂,网上搜了一堆垃圾,简直浪费我的时间和精力!幸好看到一篇比较详细的博文有了启发。
https://www.jianshu.com/p/37d083ce2063
需要注意几点:
1.Project(父类)不能被依赖,只用于定义可依赖包(dependencyManagement)和公共dependencies。这些可依赖包的项目会自动打包并放在本地仓库(本地.m2\repository目录),这个估计很多maven小白都不知道。
接下来的依赖就很简单了,如果你想依赖某个module,就像依赖网上的包一样在dependencies配置依赖它的jar包。毕竟都是用的同一个maven仓库,依赖肯定不会出错。
这是我在pc的pom加入的依赖:
<dependency>
<groupId>com.***</groupId>
<artifactId>base</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
2.把被依赖的公共部分单独做个module,不需要启动类,不需要多余的配置,默认就行。
这是我的base下pom的全部内容
<parent>
<artifactId>***</artifactId>
<groupId>com.***</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>base</artifactId>
如果需要启动,像module(pc, wx),用SpringBoot的默认Build就行。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>