springboot打包成sdk,供第三方调用,这种情况很常见,比如封装公用的模块,公用的接口等操作。现记录一下。

1. 打包方式对比。

不能使用springboot的打包方式,这种方式,打出来的包,包含所有依赖,是一个可以直接启动的jar包,两者jar包会存在冲突,所以不能使用。

名称类型区别
maven-jar-pluginmaven打包插件仅打包class文件
spring-boot-maven-pluginspringboot打包插件打包class文件和依赖的jar包文件

2. 更换打包方式

修改 pom.xml 中的 build 节点的内容,示例如下:

springboot 打包的xml

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <excludes>
          <exclude>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
          </exclude>
        </excludes>
      </configuration>
    </plugin>
  </plugins>
</build>

修改为:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <excludes>
          <!-- 此处需要排除springboot启动类,这样就不会启动对应的-->
        	<exclude>
            com/test/MinioApplication.java
          </exclude>
        </excludes>
      </configuration>
    </plugin>
  </plugins>
</build>

3. 重新进行打包

执行 mvn cleanmvn install 打包。

4. 其他项目依赖

在项目中的 pom.xml 中手动引入依赖即可。如下所示:

<!--引入minio-->
<dependency>
  <groupId>com.test</groupId>
  <artifactId>minio</artifactId>
  <version>v1.0</version>
</dependency>

5. 扫描识别对应的bean的目录

如果需要当前项目管理导入的项目的bean,则需要进行bean的管理,需要扫描对应的导入的包的结构,并解决依赖的bean的问题。

springboot 中需要在启动类上加上 @ComponentScan 注解,并提供对应的 bean 的包。就能正常使用对应功能了。例如:

@ComponentScan("com.test")
文章作者: sopp
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 个人主页
springboot java java
喜欢就支持一下吧