springboot项目中遇到的打包方式的坑。
主要是maven的打包方式和springboot的打包方式区别。
当springboot项目在pom文件中添加最好添加springboot已经集成好的打包方式,不要将maven的打包方式和springboot的打包方式都添加进来这样打包在生产环境时jar包不一定运行的起来。
<properties>
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties>
<build>
<finalName>
${project.artifactId}_${maven.build.timestamp}
</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
错误的pom打包插件事例:
<build>
<finalName>
${project.artifactId}_${maven.build.timestamp}
</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- 资源文件拷贝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- java编译插件 -->
<!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version> <configuration> <source>1.7</source> <target>1.7</target>
<encoding>UTF-8</encoding> </configuration> </plugin> -->
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<!-- 添加Class-Path -->
<addClasspath>true</addClasspath>
<!-- Class-Path添加前缀 -->
<classpathPrefix>lib/</classpathPrefix>
<!-- 指定Main-Class -->
<!-- <mainClass>com.alibaba.dubbo.container.Main</mainClass> -->
<mainClass>com.ylkz.OssProviderApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- 拷贝依赖的jar包到lib目录 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
<!-- 拷贝依赖到lib文件夹 -->
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
将其中的maven打包方式的依赖都给删除点只保留springboot的打包方式既可。遇到的maven打包方式错误导致项目起不来的情况。