maven三種打包方式

當你使用 Maven 對項目打包時,你需要了解以下 3 個打包 plugin,它們分別是:

plugin function
maven-jar-plugin maven 默認打包插件,用來創建 project jar
maven-shade-plugin 用來打可執行包,executable(fat) jar
maven-assembly-plugin 支持定製化打包方式,例如 apache 項目的打包方式

將依賴的某個 Jar 包內部的類或者資源 include/exclude 掉。

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <filters>
                <filter>
                  <artifact>junit:junit</artifact>
                  <includes>
                    <include>junit/framework/**</include>
                    <include>org/junit/**</include>
                  </includes>
                  <excludes>
                    <exclude>org/junit/experimental/**</exclude>
                    <exclude>org/junit/runners/**</exclude>
                  </excludes>
                </filter>
                <filter>
                  <artifact>*:*</artifact>
                   <excludes>
                        <exclude>META-INF/*.SF</exclude>
                        <exclude>META-INF/LICENSE</exclude>
                        <exclude>META-INF/*.DSA</exclude>
                        <exclude>META-INF/NOTICE</exclude>
                        <exclude>META-INF/services/*</exclude>
                   </excludes>
                </filter>
              </filters>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
</build>

maven-shade-plugin 自動將所有不使用的類全部排除掉,將 uber-jar 最小化

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

發佈了49 篇原創文章 · 獲贊 7 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章