最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2024年12月11日 09:24:32   作者:小小工匠  
MavenAssembly插件用于創(chuàng)建可分發(fā)包,如JAR、ZIP或TAR文件,通過(guò)配置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ā)布。

常見(jiàn)用途包括生成包含所有依賴的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插件來(lái)實(shí)現(xiàn)項(xiàng)目構(gòu)建過(guò)程中的特定任務(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作為描述符文件來(lái)定義打包規(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,則顯示使用說(shuō)明。

解析命令行參數(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文件重命名為L(zhǎng)ICENSE -->
        <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

常見(jiàn)問(wèn)題及解決方案

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

總結(jié)

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

相關(guān)文章

  • SpringBoot 接口防護(hù)(防重提交 + 限流)

    SpringBoot 接口防護(hù)(防重提交 + 限流)

    Guardian 是一個(gè)輕量級(jí) Spring Boot API 請(qǐng)求層防護(hù)框架,提供防重復(fù)提交和接口限流兩大能力,本文就來(lái)詳細(xì)的介紹一下SpringBoot 接口防護(hù)(防重提交 + 限流),感興趣的可以了解一下
    2026-03-03
  • JDK8 HashMap紅黑樹(shù)退化為鏈表的機(jī)制方式

    JDK8 HashMap紅黑樹(shù)退化為鏈表的機(jī)制方式

    這篇文章主要介紹了JDK8 HashMap紅黑樹(shù)退化為鏈表的機(jī)制方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-05-05
  • Java String字符串和Unicode字符相互轉(zhuǎn)換代碼

    Java String字符串和Unicode字符相互轉(zhuǎn)換代碼

    這篇文章主要介紹了Java String字符串和Unicode字符相互轉(zhuǎn)換代碼,需要的朋友可以參考下
    2014-10-10
  • 淺談Mybatis二級(jí)緩存的缺陷

    淺談Mybatis二級(jí)緩存的缺陷

    本文主要介紹了淺談Mybatis二級(jí)緩存的缺陷,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 解決運(yùn)行jar包出錯(cuò):ClassNotFoundException問(wèn)題

    解決運(yùn)行jar包出錯(cuò):ClassNotFoundException問(wèn)題

    這篇文章主要介紹了解決運(yùn)行jar包出錯(cuò):ClassNotFoundException問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • @Scheduled 如何讀取動(dòng)態(tài)配置文件

    @Scheduled 如何讀取動(dòng)態(tài)配置文件

    這篇文章主要介紹了@Scheduled 如何讀取動(dòng)態(tài)配置文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • SpringBoot對(duì)PDF進(jìn)行模板內(nèi)容填充與電子簽名合并詳解

    SpringBoot對(duì)PDF進(jìn)行模板內(nèi)容填充與電子簽名合并詳解

    這篇文章主要為大家詳細(xì)介紹了SpringBoot對(duì)PDF進(jìn)行模板內(nèi)容填充與電子簽名合并的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考下
    2023-12-12
  • SpringBoot整合Mybatis-plus關(guān)鍵詞模糊查詢結(jié)果為空

    SpringBoot整合Mybatis-plus關(guān)鍵詞模糊查詢結(jié)果為空

    SpringBoot整合Mybatis-plus使用關(guān)鍵詞模糊查詢的時(shí)候,數(shù)據(jù)庫(kù)中有數(shù)據(jù),但是無(wú)法查找出來(lái),本文就來(lái)介紹一下SpringBoot整合Mybatis-plus關(guān)鍵詞模糊查詢結(jié)果為空的解決方法
    2025-04-04
  • Java 實(shí)現(xiàn)多線程的幾種方式匯總

    Java 實(shí)現(xiàn)多線程的幾種方式匯總

    JAVA多線程實(shí)現(xiàn)方式主要有三種:繼承Thread類、實(shí)現(xiàn)Runnable接口、使用ExecutorService、Callable、Future實(shí)現(xiàn)有返回結(jié)果的多線程。其中前兩種方式線程執(zhí)行完后都沒(méi)有返回值,只有最后一種是帶返回值的。
    2016-03-03
  • Java8新特性之線程安全日期類

    Java8新特性之線程安全日期類

    這篇文章主要介紹了Java8新特性之線程安全日期類,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有一定的幫助,需要的朋友可以參考下
    2021-04-04

最新評(píng)論

策勒县| 成武县| 诸暨市| 邹平县| 松溪县| 铜山县| 贺兰县| 天柱县| 开远市| 广德县| 依安县| 若羌县| 泉州市| 河东区| 大洼县| 安福县| 兰溪市| 电白县| 同心县| 板桥市| 武夷山市| 宝山区| 灵璧县| 青河县| 河西区| 乌拉特中旗| 定兴县| 南木林县| 申扎县| 辽宁省| 巴林左旗| 肃宁县| 黄大仙区| 崇左市| 长春市| 忻州市| 永胜县| 汤阴县| 黄浦区| 乌鲁木齐县| 保定市|