1. 程式人生 > 實用技巧 >maven-shade-plugin~打包時過濾專案中某些包

maven-shade-plugin~打包時過濾專案中某些包

maven-shade-plugin可以用來進行打包,並實現在打包過程中的一些過濾、排除、包含、重新命名等一系列操作,當我們設計公用專案時,有時在專案時會有一些測試用例,如果在打包時想把這些測試包排除,使用maven-shade-plugin外掛是個不錯的選擇。

打包包含和排除

下面的程式碼實現了以下幾個功能:

  • 打包時排除com.lind.uaa.jwt.three包下的所有內容
  • 打包時排除專案的properties型別的配置檔案
  • 打包時,com.baomidou組織的包新增到當然JAR包裡,預設是不會新增到當前包的
  • createSourcesJar選項實現了打包時為原始碼再打一個包
 <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>*:*</artifact>
                                    <excludes>
                                        <exclude>**/*.properties</exclude>
                                        <exclude>com/lind/uaa/jwt/three/**</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <artifactSet>
                                <!-- 捆綁包含,目標專案不需要再手動引用這個包了 -->
                                <includes>
                                    <include>com.baomidou:*</include>
                                </includes>
     
                            </artifactSet>
                            <createSourcesJar>true</createSourcesJar>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>