欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Maven Assembly實(shí)戰(zhàn)教程

 更新時(shí)間:2024年12月11日 09:24:32   作者:小小工匠  
MavenAssembly插件用于創(chuàng)建可分發(fā)包,如JAR、ZIP或TAR文件,通過配置pom.xml,可以生成包含所有依賴的JAR文件或自定義格式的歸檔文件,示例展示了如何使用默認(rèn)描述符和自定義描述符創(chuàng)建JAR包,以及在多模塊項(xiàng)目中使用Assembly插件

Assembly插件

Maven Assembly插件用于創(chuàng)建項(xiàng)目的可分發(fā)包,如JAR、ZIP或TAR文件。

它可以將項(xiàng)目的代碼、依賴項(xiàng)、資源文件打包在一起,方便部署和發(fā)布。

常見用途包括生成包含所有依賴的JAR文件、創(chuàng)建特定格式的歸檔文件等。

基本配置

pom.xml中添加Maven Assembly插件的配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <descriptors>
                    <descriptor>src/assembly/your-assembly.xml</descriptor>
                </descriptors>
                <finalName>${project.artifactId}-${project.version}</finalName>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

重要配置項(xiàng):

  • descriptors:指定自定義描述符文件的路徑,允許更靈活的打包方式。
  • finalName:定義生成包的最終名稱。

使用示例

示例1:創(chuàng)建包含依賴的JAR包

使用默認(rèn)描述符生成包含所有依賴的JAR:

<descriptorRefs>
    <descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>

運(yùn)行命令:

mvn clean package

示例2:自定義描述符

創(chuàng)建src/assembly/your-assembly.xml文件:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>custom</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>${project.build.finalName}.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

示例3:多模塊項(xiàng)目打包

在父模塊的pom.xml中配置Assembly插件,并為每個(gè)子模塊定義打包策略。

實(shí)戰(zhàn) _qiwenfile

結(jié)構(gòu)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.qiwenshare</groupId>
        <artifactId>qiwenshare</artifactId>
        <version>1.2.8</version>
    </parent>

    <artifactId>qiwen-file</artifactId>
    <version>1.2.8-SNAPSHOT</version>
    <name>qiwen-file</name>
    <packaging>jar</packaging>
    <properties>
        <release-path>target/../release</release-path>
        <app-name>${project.artifactId}-${project.version}</app-name>
    </properties>

    <dependencies>
         ......省略
    </dependencies>

    <build>
        <plugins>
            <!--排除靜態(tài)文件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <!-- 添加index則不從mainfest中讀取classpath,而是從Index.list中讀取 -->
                        <!-- <index>true</index> -->
                        <manifest>
                            <mainClass>com.qiwenshare.file.FileApplication</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>./</Class-Path>
                        </manifestEntries>
                    </archive>

                    <excludes>
                        <exclude>static/**</exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <!-- not append assembly id in release file name -->
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>src/main/resources/build/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>


            <!--ant插件執(zhí)行自定義動(dòng)作-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <delete dir="${release-path}" />
                                <copy todir="${release-path}" >
                                    <fileset dir="target/${app-name}/${app-name}">
                                        <exclude name="**/*-android-*.jar"/>
                                    </fileset>
                                </copy>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>


    </build>

</project>

主要配置了三個(gè)Maven插件來實(shí)現(xiàn)項(xiàng)目構(gòu)建過程中的特定任務(wù):

maven-jar-plugin:

  • 配置生成的JAR文件的MANIFEST文件,指定主類為com.qiwenshare.file.FileApplication,并添加類路徑前綴lib/。
  • 排除靜態(tài)文件(static/**)不被打包進(jìn)JAR文件。

maven-assembly-plugin:

  • 配置在打包階段執(zhí)行,生成發(fā)布文件時(shí)不在文件名后追加assembly ID。
  • 使用src/main/resources/build/assembly.xml作為描述符文件來定義打包規(guī)則。

maven-antrun-plugin:

  • 在打包階段執(zhí)行自定義的Ant任務(wù),刪除${release-path}目錄,然后將目標(biāo)目錄中的文件(排除特定的JAR文件)復(fù)制到${release-path}目錄

  • assembly.xml
<assembly>
    <!-- 定義組裝標(biāo)識(shí)符 -->
    <id>assembly</id>
    <!-- 指定輸出格式,此處為目錄格式 -->
    <formats>
        <format>dir</format>
    </formats>
    <!-- 是否包含基礎(chǔ)目錄 -->
    <includeBaseDirectory>true</includeBaseDirectory>
    <!-- 定義文件集合 -->
    <fileSets>
        <!-- 定義第一個(gè)文件集 -->
        <fileSet>
            <!-- 源目錄為src/main/script -->
            <directory>src/main/script</directory>
            <!-- 輸出目錄為bin,并設(shè)置文件模式為0755 -->
            <outputDirectory>bin</outputDirectory>
            <fileMode>0755</fileMode>
            <!-- 包含所有文件和目錄 -->
            <includes>
                <include>*.*</include>
            </includes>
        </fileSet>
        <!-- 定義第二個(gè)文件集 -->
        <fileSet>
            <!-- 源目錄為src/main/resources -->
            <directory>src/main/resources</directory>
            <!-- 輸出目錄為conf,并設(shè)置文件模式為0644 -->
            <outputDirectory>conf</outputDirectory>
            <fileMode>0644</fileMode>
            <!-- 排除static目錄下的所有內(nèi)容 -->
            <excludes>
                <exclude>static/**</exclude>
            </excludes>
        </fileSet>
        <!-- 定義第三個(gè)文件集 -->
        <fileSet>
            <!-- 源目錄為src/main/resources/static -->
            <directory>src/main/resources/static</directory>
            <!-- 輸出目錄為static,并設(shè)置文件模式為0644 -->
            <outputDirectory>static</outputDirectory>
            <fileMode>0644</fileMode>
        </fileSet>
        <!-- 定義第四個(gè)文件集,用于復(fù)制本工程的jar文件 -->
        <fileSet>
            <!-- 源目錄為target -->
            <directory>target</directory>
            <!-- 輸出目錄為lib,并包含所有jar文件 -->
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
    <!-- 定義依賴集合 -->
    <dependencySets>
        <dependencySet>
            <!-- 依賴輸出目錄為lib -->
            <outputDirectory>lib</outputDirectory>
            <!-- 不使用項(xiàng)目自身的主要工件 -->
            <useProjectArtifact>false</useProjectArtifact>
            <!-- 使用項(xiàng)目附件 -->
            <useProjectAttachments>true</useProjectAttachments>
            <!-- 僅包含運(yùn)行時(shí)范圍的依賴 -->
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>


格式設(shè)置:指定輸出格式為目錄(dir)。

基礎(chǔ)目錄:包含基礎(chǔ)目錄(includeBaseDirectory)。

文件集:

  • 將src/main/script目錄下的所有文件復(fù)制到bin目錄,文件模式為0755。
  • 將src/main/resources目錄下的文件(排除static目錄)復(fù)制到conf目錄,文件模式為0644。
  • 將src/main/resources/static目錄下的文件復(fù)制到static目錄,文件模式為0644。
  • 將target目錄下的JAR文件復(fù)制到lib目錄。

依賴集:

將運(yùn)行時(shí)依賴項(xiàng)復(fù)制到lib目錄,不包含項(xiàng)目自身的JAR文件,但包含項(xiàng)目的附件。

觸發(fā)腳本

  • install.bat
set settingDir=src/main/resources/build/settings.xml
mvn clean install -s %settingDir%
pause

自行這個(gè)BAT腳本,就會(huì)生成

  • install.sh
#/*************************************************
#*  install.sh write by echo at Changsha. Hunan, 2021年 05月 24日 星期一 11:33:25 CST
#*************************************************/
#!/bin/sh
function echo_dbg_p(){
  echo "echo_dbg, $@"
}
function usage(){
echo -e "usages: $0 [H|h|help] [-h] [-s]
  [H|h|help]: check the usages\n
  []"
}

#main
#maven install check
cmd_package=yum
if ! mvn -v >/dev/null;then
  sudo $cmd_package install -y maven
fi
#java install check
if ! java -version &>/dev/null;then 
  sudo $cmd_package install -y java
fi
if ! mysql -V>/dev/null;then 
  sudo wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm;
  sudo rpm -ivh mysql57-community-release-el7-9.noarch.rpm
  sudo yum install -y mysql-server
fi
#build path check
#build_root_path=./
settingDir=file-common/src/main/resources/conf/settings.xml

mvn clean install -s $settingDir
sed -i "s#D:/temp_db#/tmp/#g" release/conf/config/application-dev.properties
echo_dbg_p "warning, PLS create mysql with name file, and set the password follow the file qiwen-file/file-web/src/main/resources/config/application-prod.properties"

case $1 in
  H|h|help)
    usage
    ;;
  *)
# getopts :s:h表示這個(gè)命令接受2個(gè)帶參數(shù)選項(xiàng),分別是-h和-s
    while getopts :s:h opt
    do  
      case $opt in
        s)
          echo "-s=$OPTARG"
          ;;
        :)
          echo "-$OPTARG needs an argument"
          ;;
        h)
          echo "-h is set"
          ;;
        *)
          echo "-$opt not recognized"
          ;;
      esac
    done
    ;;
esac

檢查并安裝Maven:

  • 使用mvn -v命令檢查Maven是否已安裝。
  • 如果未安裝,使用sudo yum install -y maven命令安裝Maven。

檢查并安裝Java:

  • 使用java -version命令檢查Java是否已安裝。
  • 如果未安裝,使用sudo yum install -y java命令安裝Java。

檢查并安裝MySQL:

  • 使用mysql -V命令檢查MySQL是否已安裝。
  • 如果未安裝,下載MySQL的社區(qū)版本RPM包并安裝,然后使用sudo yum install -y mysql-server命令安裝MySQL服務(wù)器。

構(gòu)建項(xiàng)目:

  • 使用Maven清理并安裝項(xiàng)目,指定設(shè)置文件路徑。
  • 修改配置文件release/conf/config/application-dev.properties中的路徑。

幫助信息:

  • 如果第一個(gè)參數(shù)為H, h, 或help,則顯示使用說明。

解析命令行參數(shù):

  • 使用getopts解析命令行參數(shù)-s和-h。
  • 根據(jù)解析結(jié)果執(zhí)行相應(yīng)的操作。

實(shí)戰(zhàn) _nacos

  • release-nacos.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright 1999-2018 Alibaba Group Holding Ltd.
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->
<assembly>
    <!-- 定義組裝標(biāo)識(shí),使用項(xiàng)目版本號(hào) -->
    <id>server-${project.version}</id>
    <!-- 是否包含基礎(chǔ)目錄 -->
    <includeBaseDirectory>true</includeBaseDirectory>
    <!-- 定義打包格式 -->
    <formats>
        <format>dir</format>
        <format>tar.gz</format>
        <format>zip</format>
    </formats>
    <!-- 定義文件集合 -->
    <fileSets>
        <!-- 包含plugins目錄下的所有內(nèi)容 -->
        <fileSet>
            <includes>
                <include>plugins/**</include>
            </includes>
        </fileSet>
        <!-- 包含conf目錄下的所有內(nèi)容 -->
        <fileSet>
            <includes>
                <include>conf/**</include>
            </includes>
        </fileSet>
        <!-- 包含bin目錄下的所有文件,并設(shè)置文件權(quán)限 -->
        <fileSet>
            <includes>
                <include>bin/*</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>
    </fileSets>
    <!-- 定義單獨(dú)的文件 -->
    <files>
        <!-- 將LICENSE-BIN文件重命名為LICENSE -->
        <file>
            <source>LICENSE-BIN</source>
            <destName>LICENSE</destName>
        </file>
        <!-- 將NOTICE-BIN文件重命名為NOTICE -->
        <file>
            <source>NOTICE-BIN</source>
            <destName>NOTICE</destName>
        </file>
        <!-- 打好的jar包名稱和放置目錄 -->
        <file>
            <source>../console/target/nacos-server.jar</source>
            <outputDirectory>target/</outputDirectory>
        </file>
    </files>
    <!-- 定義模塊集合 -->
    <moduleSets>
        <moduleSet>
            <!-- 是否使用所有反應(yīng)堆項(xiàng)目 -->
            <useAllReactorProjects>true</useAllReactorProjects>
            <!-- 定義包含的模塊 -->
            <includes>
                <include>com.alibaba.nacos:nacos-console</include>
            </includes>
        </moduleSet>
    </moduleSets>
</assembly>

輸出 zip / tar.gz

常見問題及解決方案

  • 插件未執(zhí)行:確保在executions中定義了正確的phasegoal
  • 依賴沖突:檢查依賴版本,確保沒有沖突,必要時(shí)使用dependencyManagement來管理版本。
  • 文件未打包:確認(rèn)fileSets配置的路徑和規(guī)則是否正確。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 學(xué)生視角手把手帶你寫Java?線程池初版

    學(xué)生視角手把手帶你寫Java?線程池初版

    作者是一個(gè)來自河源的大三在校生,以下筆記都是作者自學(xué)之路的一些淺薄經(jīng)驗(yàn),如有錯(cuò)誤請指正,將來會(huì)不斷的完善筆記,幫助更多的Java愛好者入門
    2022-03-03
  • 詳解Spring Boot 自定義PropertySourceLoader

    詳解Spring Boot 自定義PropertySourceLoader

    這篇文章主要介紹了詳解Spring Boot 自定義PropertySourceLoader,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • Java基本語法之內(nèi)部類示例詳解

    Java基本語法之內(nèi)部類示例詳解

    本文帶大家認(rèn)識(shí)Java基本語法——內(nèi)部類,將一個(gè)類定義放在另一類的定義的內(nèi)部,這個(gè)就是內(nèi)部類,內(nèi)部類允許將一些邏輯相關(guān)的類組織在一起,并能夠控制位于內(nèi)部的類的可視性,感興趣的可以了解一下
    2022-03-03
  • MyBatis-Plus里面的增刪改查詳解(化繁為簡)

    MyBatis-Plus里面的增刪改查詳解(化繁為簡)

    這篇文章主要給大家介紹了關(guān)于MyBatis-Plus里面的增刪改查的相關(guān)資料,Mybatis-Plus是一個(gè)基于Mybatis的增強(qiáng)工具,可以簡化Mybatis的開發(fā),提高開發(fā)效率,需要的朋友可以參考下
    2023-07-07
  • Java 中的字符串常量池詳解

    Java 中的字符串常量池詳解

    本文主要介紹Java中的字符串常量池的知識(shí),這里整理了相關(guān)資料及簡單示例代碼幫助大家學(xué)習(xí)理解此部分的知識(shí),有需要的小伙伴可以參考下
    2016-09-09
  • Java ArrayList與Vector和LinkedList的使用及源碼分析

    Java ArrayList與Vector和LinkedList的使用及源碼分析

    ArrayList、Vector、LinkedList類均在java.util包中,均為可伸縮數(shù)組,即可以動(dòng)態(tài)改變長度的數(shù)組。ArrayList 和 Vector都是基于存儲(chǔ)元素的Object[] array來實(shí)現(xiàn)的,它們會(huì)在內(nèi)存中開辟一塊連續(xù)的內(nèi)存來存儲(chǔ)
    2022-11-11
  • 淺析Java中Future接口的使用方法

    淺析Java中Future接口的使用方法

    在Java開發(fā)中,異步編程是提高系統(tǒng)性能和響應(yīng)能力的重要手段之一。本文將深入探討Future接口的原理和源碼解讀,幫助讀者更好地理解Future接口的工作機(jī)制和使用方法
    2023-05-05
  • Java截取中英文混合字符串的方法

    Java截取中英文混合字符串的方法

    這篇文章主要為大家詳細(xì)介紹了Java截取中英文混合字符串的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • 使用gRPC微服務(wù)的內(nèi)部通信優(yōu)化

    使用gRPC微服務(wù)的內(nèi)部通信優(yōu)化

    這篇文章主要為大家介紹了微服務(wù)優(yōu)化之使用gRPC做微服務(wù)的內(nèi)部通信,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • Mybatis?Plus插入數(shù)據(jù)后獲取新數(shù)據(jù)id值的踩坑記錄

    Mybatis?Plus插入數(shù)據(jù)后獲取新數(shù)據(jù)id值的踩坑記錄

    在某些情況下,需要在執(zhí)行新增后,需要獲取到新增行的id,這篇文章主要給大家介紹了關(guān)于Mybatis?Plus插入數(shù)據(jù)后獲取新數(shù)據(jù)id值的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08

最新評論