欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用Maven打包发布可执行的jar包

程序员文章站 2022-04-28 19:14:47
...

项目有个需求,要把当前项目的代码和依赖打包成一个jar包,再把jar包和一些相关文件一起打包成一个tar

这里用到了两个插件,shade 和assembly,shade负责生成jar包,assembly负责打包成tar。pom.xml配置如下:

    <build>
        <plugins>
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <finalName>abc-service</finalName>  //打包成的包名
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.abc.Main</mainClass>    //主类名
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                </configuration>
            </plugin>
            .......
        </plugins>
</build>

assembly.xml里定义打包成tar的动作

<assembly>
    <id>beta</id>
    <formats>
        <!-- zip,tar,tar.gz,tar.bz2,jar,dir,war -->
        <format>tar.gz</format>
    </formats>
    <!-- 需要打包的文件集 -->
    <fileSets>
        <fileSet>
            <directory>resource</directory>       //相关文件目录
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
    <files>
        <file>
            <source>${project.build.directory}/abc-service.jar</source>   //源jar包名
            <outputDirectory>/</outputDirectory>
            <destName>abc-service.jar</destName>     //tar内jar包名
        </file>
    </files>  
</assembly>

在maven的执行goals里填写如下:

clean test package assembly:assembly




转载于:https://my.oschina.net/piorcn/blog/388255