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

项目有个需求,要把当前项目的代码和依赖打包成一个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




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。