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

Maven Shade Plugin 插件使用小結(jié)

 更新時(shí)間:2026年07月15日 09:44:47   作者:prcyang  
本文詳細(xì)解析maven-shade-plugin的用法,教你如何打包可執(zhí)行jar包、排除不需要的依賴,以及通過class relocation解決包名沖突,具有一定的參考價(jià)值,感興趣的可以了解一下

1 Maven Shade Plugin 地址

https://maven.apache.org/plugins/maven-shade-plugin/index.html

2 Maven Shade Plugin 特點(diǎn)

  • maven-shade-plugin ,它可以在構(gòu)建過程中創(chuàng)建 一個(gè)包含所有依賴項(xiàng)的Jar文件。
  • 能夠?qū)δ承┮蕾図?xiàng)的包進(jìn)行遮蔽處理——即對(duì)其進(jìn)行重命名。
  • 可打包可執(zhí)行的jar包(配置Main Class)和不可執(zhí)行的jar包(不配置 Main Class)

3 maven-shade-plugin 的用法

3.1 maven-shade-plugin 的基本用法

3.1.1 不配置 Main Class,打包成不可執(zhí)行的jar包

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.6.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

不可執(zhí)行的 JAR 文件主要用作 Java 類庫(Library JAR),它不包含可以直接運(yùn)行的主類。這樣的 JAR 文件通常被引入到其他
Java 項(xiàng)目中作為依賴項(xiàng)使用

3.1.1 配置 Main Class,打包可執(zhí)行的jar包

官方原文地址: https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html

在configuration->transformers->transformer->mainClass 節(jié)點(diǎn)中設(shè)置主類,使生成的 JAR 文件可以直接通過 java -jar 命令運(yùn)行,

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.6.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.sonatype.haven.HavenCli</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

可執(zhí)行的 JAR 文件包含一個(gè)可以直接運(yùn)行的 Java 應(yīng)用程序。要使 JAR 文件可執(zhí)行,它必須包含一個(gè) Main-Class
屬性,該屬性指定要運(yùn)行的主類(main class)

3.2 選擇、排除依賴Jar包的文件

官網(wǎng)原文:https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html
下面的 POM 代碼片段展示了如何控制哪些項(xiàng)目依賴項(xiàng)應(yīng)被包含/排除在超集 JAR 文件中:

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.6.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <artifactSet>
                <excludes>
                  <exclude>classworlds:classworlds</exclude>
                  <exclude>junit:junit</exclude>
                  <exclude>jmock:*</exclude>
                  <exclude>*:xml-apis</exclude>
                  <exclude>org.apache.maven:lib:tests</exclude>
                  <exclude>log4j:log4j:jar:</exclude>
                </excludes>
              </artifactSet>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

3.3 修改Class文件路徑

使用 relocations 可以將某些類的路徑修改,以防止不同依賴中的類沖突:
官網(wǎng)地址:https://maven.apache.org/plugins/maven-shade-plugin/examples/class-relocation.html

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.6.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <relocations>
                <relocation>
                  <pattern>org.codehaus.plexus.util</pattern>
                  <shadedPattern>org.shaded.plexus.util</shadedPattern>
                  <excludes>
                    <exclude>org.codehaus.plexus.util.xml.Xpp3Dom</exclude>
                    <exclude>org.codehaus.plexus.util.xml.pull.*</exclude>
                  </excludes>
                </relocation>
              </relocations>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

這會(huì)指示插件將位于“org.codehaus.plexus.util”及其子包中的類移動(dòng)到“org.shaded.plexus.util”這個(gè)包中。而類“Xpp3Dom”以及pull.*相關(guān)文件保留在其原始的包中。

還可以通過使用“include”標(biāo)簽來縮小該模式的范圍:

<project>
  ...
                <relocation>
                  <pattern>org.codehaus.plexus.util</pattern>
                  <shadedPattern>org.shaded.plexus.util</shadedPattern>
                  <includes>
                    <include>org.codehaud.plexus.util.io.*</include>
                  </includes>
                </relocation>
  ...
</project>

4 maven-shade-plugin 會(huì)自動(dòng)創(chuàng)建 dependency-reduced-pom.xml

在使用 Maven 的 maven-shade-plugin 插件時(shí),確實(shí)會(huì)生成一個(gè)名為 dependency-reduced-pom.xml 的文件。這個(gè)文件是 Maven 項(xiàng)目的一個(gè)特殊版本,主要用于解決依賴沖突和簡化最終的依賴結(jié)構(gòu)。

4.1 為什么需要 dependency-reduced-pom.xml

依賴沖突解決:在大型項(xiàng)目中,可能會(huì)有多個(gè)庫包含相同版本的依賴,但由于不同的作用域(compile, provided, runtime 等)或不同的版本,這些依賴可能會(huì)沖突。maven-shade-plugin 在創(chuàng)建 uber-jar 時(shí),會(huì)嘗試解決這些沖突,并生成一個(gè)簡化后的 POM 文件,該文件只包含實(shí)際需要的依賴版本。

簡化依賴結(jié)構(gòu):生成的 dependency-reduced-pom.xml 文件只包含最終項(xiàng)目中實(shí)際使用的依賴項(xiàng)和它們的版本,這對(duì)于確保構(gòu)建的可重復(fù)性和清晰性非常有用。

4.1.1 生成

當(dāng)你運(yùn)行帶有 maven-shade-plugin 的 Maven 構(gòu)建時(shí),插件會(huì)自動(dòng)生成這個(gè)文件,在構(gòu)建過程中,dependency-reduced-pom.xml 會(huì)被創(chuàng)建在 target/ 目錄下。

4.1.2 使用

雖然通常不需要手動(dòng)操作這個(gè)文件,但它對(duì)于理解項(xiàng)目的最終依賴結(jié)構(gòu)非常有幫助。你可以在需要時(shí)查看這個(gè)文件來了解項(xiàng)目中實(shí)際包含哪些庫和它們的版本。例如,如果你需要將項(xiàng)目部署到不支持完整 Maven 倉庫的環(huán)境中,這個(gè)文件可以幫助你手動(dòng)管理依賴。
注意事項(xiàng)

  • 不要修改:通常不建議手動(dòng)修改 dependency-reduced-pom.xml,因?yàn)樗砹藰?gòu)建過程中的一個(gè)中間產(chǎn)物。任何修改都可能影響構(gòu)建的一致性和可重復(fù)性。
  • 版本控制:如果你需要保留這個(gè)文件以便將來參考,可以考慮將其添加到項(xiàng)目的版本控制中,盡管這通常是可選的。
    通過使用 maven-shade-plugin 和其生成的 dependency-reduced-pom.xml,你可以有效地管理和分發(fā)包含所有必需依賴的單一 JAR 文件,這對(duì)于部署和分發(fā)應(yīng)用程序非常有用。

4.2 配置不生成

在 maven-shade-plugin所在的 標(biāo)簽下(或者 plugin-executions-execution 標(biāo)簽狹隘)添加

<configuration>
	<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>

到此這篇關(guān)于Maven Shade Plugin 插件使用小結(jié)的文章就介紹到這了,更多相關(guān)Maven Shade Plugin 插件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

沐川县| 尼勒克县| 富阳市| 宁武县| 二连浩特市| 宾川县| 永年县| 平原县| 宜兰市| 防城港市| 盐池县| 黄冈市| 海安县| 肇东市| 广丰县| 太康县| 醴陵市| 大英县| 富阳市| 兴海县| 绥江县| 南木林县| 阿克陶县| 永清县| 通道| 布尔津县| 寻乌县| 哈密市| 阿拉善右旗| 巴彦县| 红桥区| 全州县| 依安县| 吉水县| 蓬安县| 乳源| 德化县| 团风县| 泾阳县| 滦平县| 鞍山市|