4-Maven-plugins之打包方式

csdn推荐

4-Maven-plugins之打包方式 方式一【推荐】:使用maven-jar-plugin和maven-dependency-plugin

打成的最终jar包中没有所依赖的jar包。依赖跟自己的代码不在一个jar包中。

传说中的"瘦Jar"

<build>
    <directory>${project.basedir}/target</directory>
</build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
            <archive>
                <manifest>
                    
                    <mainClass>com.bocloud.cop.booter.Application</mainClass>
                    <addClasspath>true</addClasspath>
                    <useUniqueVersions>false</useUniqueVersions>
                    <classpathPrefix>libs/</classpathPrefix>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.6.1</version>
        <executions>
            <execution>
                <goals>
                    
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <type>jar</type>
                    <includeTypes>jar</includeTypes>
                    <includeScope>runtime</includeScope>
                    
                    <outputDirectory>${project.build.directory}/libs</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

方法二【推荐】:使用maven-assembly-plugin

maven-assembly-plugin可以将所有的东西都打包到一个jar包中。

传说中的"胖jar"

执行mvn package后,会在target文件夹下生成两个jar包,一个是不带依赖的jar包,一个是后缀有-dependencies带有依赖的jar包

直接执行:

java -jar XXXXX-jar-with-dependencies.jar

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                <manifest>
                    
                    <mainClass>com.bocloud.cop.booter.Application</mainClass>
                </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>

方法三:使用maven-shade-plugin

跟maven-assembly-plugin类似,都可以将所有的东西都打包到一个jar包中。

传说中的"胖jar"

执行mvn package后,会在target文件夹下生成两个jar包,一个是不带依赖的jar包,一个是后缀有-shaded带有依赖的jar包

直接执行:

java -jar XXXXXX-jar-with-shaded.jar

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <transformers>
                    <transformer implementation=
                      "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        
                        <mainClass>com.bocloud.cop.booter.Application</mainClass>
                </transformer>
            </transformers>
        </configuration>
        </execution>
    </executions>
</plugin>

方法四【推荐】:使用spring-boot-maven-plugin

可以将所有的东西都打包到一个jar包中。传说中的"胖jar"。

能同时打可执行jar包和war包

两个重点:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <classifier>spring-boot</classifier>
                <mainClass>com.bocloud.cop.booter.Application</mainClass>
            </configuration>
        </execution>
    </executions>
</plugin>

文章来源:https://blog.csdn.net/qq_36458574/article/details/139500930



微信扫描下方的二维码阅读本文

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容