springboot 打包总结(2)


title: springboot 打包总结(2)
time: 22点52分

​ 之前,写个一篇关于springboot博客,由于项目用到了第三方jar包,然后打包便出错了,然后试了很多方法,都无济于事,最终选择了把jar包 打包发布到本地maven仓库,即可解决该问题。但是,若要是引用了很多个jar包,那我们要这样手动发布很多次吗??显然这种不是最好的办法,然后呢,我再总结一下这种方案,同时这种方案也适用与非springboot应用,只要是maven项目即可。

  • 1.首先打包,需要添加springboot的 打包插件。

  <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                 <version>2.1.1.RELEASE</version>
                 <!--没有 <parent>spring-boot-starter-parent 
						就必须要加下面的配置 不然 运行打包好的jar包就会提示没有清单。
						如果pom依赖了<parent>,那么 便不需要配置下面的。	
-->
                  <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>       	
 </plugin>


  • 2. 配置编译资源插件

    <build>
      <plugins>
        <plugin>
             <artifactId>maven-compiler-plugin</artifactId>
             <configuration>
                 <source>1.8</source>
                 <target>1.8</target>
                 <encoding>UTF-8</encoding>
                 <compilerArguments>
                     <extdirs>${project.basedir}/lib</extdirs>
    
                 </compilerArguments>
             </configuration>
             <version>3.8.0</version>
        </plugin>
      </plugins>
    </build>
    

    <extdirs>标签的值即就是 要编译的 第三方 jar包 所在的目录。

好了,以上即可完成 springboot 依赖第三方jar包的打包方法。

那么,maven默认打包资源是 文件夹是 src/main/resources,其他位置的资源我们怎么托 maven 给我们打包到 classes指定目录里呢?,这里提供三种方法。

  • 第一种: 使用标签

  • 值得一提的是,使用了resource标签,打包其他位置的资源,那么就要添加打包 src/main/resources,如果使用了resource标签,maven便不再默认帮我们打包src/main/resources的资源了,需要手动添加!

    </build>
    	<resources>
           <resource>
              <directory>${project.basedir}/lib</directory>
              <targetPath>BOOT-INF/lib/</targetPath>
              <includes>
              <include>**/*.jar</include>
              </includes>
            </resource>
            
             <resource>
    					<directory>src/main/resources</directory>
    					<includes>
    					<include>**/*</include>
    					</includes>
    				</resource>
    		</resources>
    </build>
    
  • 第二种:使用 资源打包 插件

    <build>
      <plugins>
         <plugin>
    	            <artifactId>maven-resources-plugin</artifactId>
    	            <version>2.5</version>
    	            <executions>
    	                <execution>
    	                    <id>copy-xmls</id>
    	                    <phase>process-sources</phase>
    	                    <goals>
    	                        <goal>copy-resources</goal>
    	                    </goals>
    	                    <configuration>
    	               <!-- 打包后的位置  -->             <outputDirectory>${basedir}/target/classes/lib</outputDirectory>
    	                        <resources>
    	                            <resource>
    	             <!-- 要打包的资源路径 -->                   <directory>${basedir}/lib</directory>
                                         <!-- 要打包的内容  --> 
    	                                <includes>
    	                                    <include>**/*.*</include>
    	                                </includes>
    	                            </resource>
    	                        </resources>
    	                    </configuration>
    	                </execution>
    	            </executions>
        </plugin> 
      </plugins>
    </build>
    
  • 第三种: 这个插件也有 打包资源的功能

<build>
  <plugins>
    <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>add-resource</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>add-resource</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
        <!-- 要打包的资源路径 -->
   <directory>${project.basedir}/lib</directory>
             <!-- 打包后的位置  -->                       
  <targetPath>BOOT-INF/lib/</targetPath>
                                <includes>
                                       <!-- 要打包的内容  -->  
                                    <include>**/*.xml</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
     </plugin> 
  </plugins>
</build>

我们也可以 右键项目 选择properties-->java build path-->Sources--->Add Folad添加 要打包到classes的资源,不过只能打包到 classes的根目录,而maven可以帮我们打包到classes下的自定义位置。如下图,我修改,这个资源输出的目录是会出错的。所以,我们尽量使用 maven的配置帮我们打包资源吧。
在这里插入图片描述

  • 打包时target/classes目录中的资源文件会和class字节码一起被打进jar包或war包中。有时候默认的情况不能完全满足需求,如target/classes目录中的一些文件不希望打入jar包中,就需要额外配置maven-jar-plugin插件
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <excludes>
                        <exclude>*.properties</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>xxxxxx.ConsoleLauncher</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

好了,今天的总结就先到这里了。

发布了63 篇原创文章 · 获赞 149 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/huijiaaa1/article/details/89304154
今日推荐