项目需要添加一个新的功能,虽然可以在项目中通过新建文件夹或者新建类来完成。但是基于高内聚低耦合的思想,我的思路是新建一个工程,然后通过install打成jar包发布到本地仓库,然后在项目中通过maven依赖这个jar包直接调用接口来实现新功能。
新工程是用springboot+mybatis建立的,结构比较简单,而且单元测试也都没问题,一直到项目引入jar包都是可以的,结果在调用jar包中的类的时候出现了问题
类能够被扫描到,但是一直提示Cannot resolve symbol 'mybatisdemo' ,我猜大概就是这个包不能被正确识别的意思,然后去找了一下IDEA里面maven的外部依赖发现是引入成功的。
由于IDEA是能扫描到这个包只是不能正确使用,那么我猜测可能是在打包环节哪个地方出了问题。我就看了一下jar包的结构发现里面竟然多出来一个BOOT-INF的文件夹,而且我的自定义类全部在这个文件夹里面,问题应该是出在这里,可能springboot项目在打包的时候会把本应该在META-INF的.class文件放到BOOT-INF里面去,从而导致项目找不到包。
于是我专门在网上搜索如何去除BOOF-INF包,原来是在打包的时候要在配置中加入
<skip>true</skip>
贴上完整的pom.xml信息
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!--springboot启动依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!--mybatis整合springboot的依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!--mysql数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<version>${spring-boot.version}</version>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
<!--springboot日志-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!--阿里的json工具-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.9</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
<!--分页PageHelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<configurationFile>F:\Code\mybatis\src\main\resources\generateConfig.xml</configurationFile>
</configuration>
</plugin>
<!--springboot打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--springboot的启动类,必须要填,否则会提示找不到主类-->
<mainClass>com.mybatisdemo.MybatisdemoApplication</mainClass>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
在启动类也必须做一些改动
这里要继承①所示的类并且重写②的方法,这好像是springboot项目特有的步骤吧,参考这篇博客:https://www.jianshu.com/p/c616380095f0,然后执行clean和install就行了,记得跳过测试。
在打包出来的jar包就是正常的结构了,重新引入依赖之前记得将项目缓存清理一下,不然可能还是之前的jar包。
在工程里面类也可以正常使用了~
每次遇到问题都记得总结一下,对自己的提升会有很大的帮助哟~