项目: springboot
目标:将项目打成jar包供别的项目使用
一、尝试打包
springboot中,如果直接使用自带的maven打包插件,打出来的包是可运行的,但是不能给别的项目依赖使用;
使用如下插件配置:
1、方式一:
原生的Maven插件打包
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
2、方式二:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
打包形成文件:
第一个 jar 表示可以被其他项目依赖的 jar ,第二个 jar 则表示一个可执行 jar。
二、发现问题
我在项目A中引用了一个三方开源的jar包,如下:
<dependency>
<groupId>net.openhft</groupId>
<artifactId>compiler</artifactId>
<version>2.3.4</version>
</dependency>
把A打包成jar之后,项目B依赖此jar,运行项目B发现import不到compiler包下的类;
三、解决
使用maven-assembly-plugin 插件进行项目A打包:
项目A中pom配置:
<build>
<!--<pluginManagement><!– lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) –>-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin </artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.bd.util.appclient.AppMain</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<!--</pluginManagement>-->
</build>
如此之后进行项目A打包,在target目录下发现两个jar包:
- ***.jar
- ***-jar-with-dependencies.jar
很显然,第二个jar包是我们需要的,但是如果我们直接使用deploy命令,发布到maven私服的还是第一个;
继续改造: 添加配置:
<appendAssemblyId>false</appendAssemblyId>
最终pom文件中插件配置为:
<build>
<!--<pluginManagement><!– lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) –>-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin </artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.bd.util.appclient.AppMain</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<!--</pluginManagement>-->
</build>
最终项目A打包,形成****.jar,完成目标;
附:Maven中的dependency的scope作用域详解
1、test范围指的是测试范围有效,在编译和打包时都不会使用这个依赖
2、compile范围指的是编译范围有效,在编译和打包时都会将依赖存储进去(默认)
3、provided依赖:在编译和测试的过程有效,最后生成war包时不会加入,诸如:servlet-api,因为servlet-api,tomcat等web服务器已经存在了,如果再打包会冲突
4、 runtime在运行的时候依赖,在编译的时候不依赖
参考文章:
https://segmentfault.com/a/1190000019706787