在开发中有这么一种需求。单位做的每一个项目都会涉及到自己的用户管理模块、组织机构模块、流程引擎模块,这些模块大多数情况都重复开发了,虽然工作量不是很大,但也挺浪费时间,拷贝代码这种方式也太LOW了,考虑形成可以使用的Jar包,直接引用,逼格高!跟着我一步一步走吧,网上教程大多不满足我的需求,我也踩了很多坑,记录一下解决办法和解决思路。
建立Lib模块
查阅了很多Maven的资料,以及瞎猫碰死耗子想死的心都有的经历,知道了这么一个知识:先建立一个Parent级别的Maven项目,来管理Lib模块。
建立Anivia父级模块
使用Maven建立Parent级别项目,按照我自己的喜好,起名为Anivia,Package为:cn.kisslinux。
1.png
- 修改anivia下的pom.xml内容如下:
<modelVersion>4.0.0</modelVersion> <packaging>pom</packaging> <!--设置Parent为SpringBootParent--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> </parent> <groupId>cn.kisslinux</groupId> <artifactId>anivia</artifactId> <version>1.0-SNAPSHOT</version>
- 删除anivia下的src目录。这个目录下的src是没有什么用的。
建立Anivia-Dependencies依赖管理模块
在anivia项目下新建module,名字为anivia-dependencies
2.png
这个时候anivia下的pom.xml文件会增加如下内容:
<modules>
<module>anivia-dependencies</module>
</modules>
- 编辑anivia-dependencies下的pom.xml文件,内容如下:
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging> <!-- 这里是 pom-->
<parent>
<groupId>cn.kisslinux</groupId>
<artifactId>anivia</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>anivia-dependencies</artifactId>
<name>Anivia :: Dependencies</name>
<version>1.0-SNAPSHOT</version>
<description>Anivia :: Dependencies</description>
- 删除anivia-dependencies下的src目录,因为它也没用。。。
建立Anivia-User模块
这个模块是我们最终要发布出去的模块。
- 在Anivia下新建一个module,名字为anivia-user,建立方式和上面的anivia-dependencies方式一样,我就不截图了。此时anivia下的pom.xml会增加anivia-user的module。anivia下的pom.xml看起来大概是这个样子的:
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>anivia-dependencies</module>
<module>anivia-user</module>
</modules>
<!--设置Parent为SpringBootParent-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>cn.kisslinux</groupId>
<artifactId>anivia</artifactId>
<version>1.0-SNAPSHOT</version>
- 配置anivia-user的pom.xml,先从简单的开始,让调用者能响应user的controller。pom.xml看起来是这个样子的:
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging> <!-- 这里是jar -->
<!-- 请注意下面这个地方,parent是anivia-dependencies -->
<parent>
<artifactId>anivia-dependencies</artifactId>
<groupId>cn.kisslinux</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../anivia-dependencies</relativePath>
</parent><!--这里不能少-->
<artifactId>anivia-user</artifactId>
<name>Anivia :: User</name>
<version>1.0-SNAPSHOT</version>
<description>Anivia :: User</description>
<dependencies>
<!--restful支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
在anivia-user下建立一个controller,注意我的package是cn.kisslinux.anivia.user.controller
3.png
发布Lib
很简单,点击Idea右侧Maven->anivia->Lifecycle->install
图片.png
此时,在自己家目录下下.m2中应该会生成响应的Jar包
图片.png
建立一个Demo测试使用
建立过程没啥特别注意的
图片.png
在pom.xml中添加依赖,可以看到在External Libraries中已经有我们写的jar包了。
图片.png
在demo项目中随便建立一个controller
图片.png
运行项目,发现并没有成功。能正常加载/init/index,但不能加载/user/index。
解决办法如下:图片.png
之后,就发现都可以访问啦!
现在问题就已经解决了大部分了。我们下一步把JPA加上,因为anivia-user自己也会操作数据库
为anivia-user增加springboo-data-jpa
在anivia-user的pom.xml中添加jpa的依赖
图片.png
新建User的entity
图片.png
新建UserRepository
图片.png
偷懒,就不建立server层了哈!
修改anivia-user的pom.xml,版本号增加一下: 图片.png
再次发布Jar包,应该可以看到1.1的Jar包已经打包了。
修改demo对anivia-user的依赖,升级一下版本。并检查。 图片.png
在Demo中使用一下userRepository吧!
图片.png
运行项目,爆粗啦!
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
问题产生原因:没有在demo的配置文件中配置数据库,编辑demo项目下的application.properties,内容如下:
# db
## master 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jooq.sql-dialect=MySQL
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
同时在demo下的pom.xml中添加mysql的依赖
图片.png
再运行一下,又爆粗啦!!!
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userRepository in com.example.demo.controller.InitController required a bean of type 'cn.kisslinux.anivia.user.repository.UserRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'cn.kisslinux.anivia.user.repository.UserRepository' in your configuration.
说找不到userRepository,
解决办法如下:
图片.png
再运行一下啊:
又爆粗啦!一部分stack如下:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'initController': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class cn.kisslinux.anivia.user.entity.User
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at com.example.demo.DemoApplication.main(DemoApplication.java:19) [classes/:na]
解决办法如下:
图片.png
再次运行!!!
没有报错,刷新网页,可以看到下面的消息:
图片.png
终于结束了!!!
OMG。。。我快累死了!