前言:本文將告訴你如何將程式Jar與與依賴Jar及組態檔分離打包,以下列舉了兩種不同Maven打包方式,其打包效果一致!
一、第一種Maven打包方式,將jar及resources下全部組態檔,拷貝到指定目錄:
<!--配置項-->
<properties>
<!--自定義配置-->
<project.jar.output.directory>E:/IDEAFile/file-copy/target/project</project.jar.output.directory>
</properties>
<build>
<plugins>
<!--專案依賴的jar檔案,放置默認配置目錄下-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 設定jar的入口類 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.filecopy.FileCopyApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- 使用maven-resources-plugin插件復制resources目錄下所有檔案到指定的路徑-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/project</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!--使用maven-antrun-plugin插件將jar復制到指定的目錄下-->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<!-- 在maven進行package的時候執行-->
<phase>package</phase>
<configuration>
<tasks>
<!--todir:是將要復制jar包到的地方,overwrite:是否重寫-->
<copy todir="${project.jar.output.directory}" overwrite="true">
<!--獲取父目錄下的target檔案夾中的jar-->
<fileset dir="${project.build.directory}">
<include name="*.jar"/>
</fileset>
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
第二種Maven打包方式使用 assembly插件,將jar及組態檔進行壓縮打包到指定目錄:
<plugins>
<!-- 專案依賴的jar檔案,放置默認配置目錄下-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 設定jar的入口類-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.filecopy.FileCopyApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!--assembly插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<!--指定壓縮包名稱-->
<finalName>project</finalName>
<!--指定assembly組態檔配置-->
<descriptors>
<descriptor>/assembly/assembly.xml</descriptor>
</descriptors>
<!--打包tar.gz輸出target檔案夾中-->
<outputDirectory>${project.build.directory}</outputDirectory>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
assembly檔案:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd"> <id>leaves</id> <formats> <!--壓縮檔案形式 可選 zip tar.gz等 --> <format>zip</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <!-- 專案檔案處理 --> <fileSets> <!--組態檔輸出位置根目錄檔案夾下--> <fileSet> <directory>${basedir}/src/main/resources</directory> <includes> <include>**</include> </includes> <filtered>true</filtered> <outputDirectory>${file.separator}</outputDirectory> </fileSet> <!-- 專案代碼生成的jar檔案放在根目錄 --> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>${file.separator}</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> </fileSets> </assembly>
個人總結:
我是南國以南i記錄點滴每天成長一點點,學習是永無止境的!轉載請附原文鏈接!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/241724.html
標籤:Java
上一篇:JAVA語言基礎隨堂筆記
下一篇:PHP設計模式之中介者模式
