zoukankan      html  css  js  c++  java
  • 使用maven-compiler-plugin以及maven-shade-plugin完成maven项目打包

    最近负责一个纯maven项目(项目需求尽量轻量化),需要自己完成打包工作.
    因此,基于maven-compiler-plugin以及maven-shade-plugin完成项目的打包工作.

    其中:

    • maven-compiler-plugin负责项目编译;
    • maven-shade-plugin负责最终的打包操作.

    以下所示操作,均在pom.xml文件中进行.

    项目基本属性

        <groupId>com.test</groupId>
        <artifactId>app</artifactId> //
        <version>0.1.0</version>
        <packaging>jar</packaging>
        
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    

    添加插件

    <build>
            <plugins>
    
                <!-- 编译java项目-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
    
                <!-- 创建一个fat jar,包含所有必须的依赖 -->
                <!-- 根据自己的main函数替换<mainClass>...</mainClass>中的配置 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.0.0</version>
                    <executions>
                        <!-- Run shade goal on package phase -->
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <artifactSet>
                                    <!-- 指定不需要打包的文件(可以使用通配符) -->
                                    <excludes>
                                        <exclude>org.apache.flink:force-shading</exclude>
                                        <exclude>com.google.code.findbugs:jsr305</exclude>
                                        <exclude>org.slf4j:*</exclude>
                                        <exclude>log4j:*</exclude>
                                    </excludes>
                                </artifactSet>
                                <filters>
                                    <filter>
                                        <!-- Do not copy the signatures in the META-INF folder.
                                        Otherwise, this might cause SecurityExceptions when using the JAR. -->
                                        <artifact>*:*</artifact>
                                        <excludes>
                                            <exclude>META-INF/*.SF</exclude>
                                            <exclude>META-INF/*.DSA</exclude>
                                            <exclude>META-INF/*.RSA</exclude>
                                        </excludes>
                                    </filter>
                                </filters>
                                <transformers>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>com.test.app.Application</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
    

    Tips:

    maven-shade-plugin比较强大,还可以解决打包文件中的依赖冲突,读者可以自行寻找相关文章.

    PS:
    如果您觉得我的文章对您有帮助,请关注我的微信公众号,谢谢!
    程序员打怪之路

  • 相关阅读:
    HDU4405(期望DP)
    hdu4165(简单递推,实则卡特兰数的应用)
    hdu4576(概率DP)
    期望DP(2013山东省赛)
    Nim游戏及其相关拓展
    nginx ubuntu环境下配置 path_info
    iOS 瀑布流的简单实现 UICollectionView
    ThPullRefresh (Swift 版)下拉上拉刷新
    用php搭建博客系统-待续
    面试收录-php面试题
  • 原文地址:https://www.cnblogs.com/jason1990/p/11696504.html
Copyright © 2011-2022 走看看