maven-assembly-plugin插件打包 jar、tar.gz

使用 maven-assembly-plugin 插件可以将我们的java application(java应用程序)打成可执行jar,下面简要介绍一下使用maven-assembly-plugin打包可执行jar和tar.gz。

前面我们已经介绍过maven 多环境打包配置;此处不再介绍

可执行jar包配置

打开pom.xml,在bulid->plugins中加入插件配置:

 

<plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.test.example</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

注意mainClass为主函数main所在的入口类

运行配置如下

 

运行后会在target目录下生成对应的jar,默认名称为xxx-1.0-SNAPSHOT-jar-with-dependencies.jar,直接java -jar 即可运行

 

tar.gz打包配置

以上我们成功了打包了一个可执行jar,它的关键就在于指定主函数的入口类即可;弊端就是对于一个应用我们只能指定一个主函数入口类,所以如果我们要启动多个进程就只能创建多个application了,这对于我来说是一件非常痛苦的事,所以为了解决一个项目中可以启动多个进程,我们可以将该类型应用打包成tar.gz(主要是为了在linux机器上运行),然后为每个入口类创建一个启动脚本,最后需要启动哪个应用就直接执行对应的脚本即可;

打开pom.xml配置更改为如下:

 

 <!--linux 下执行 打包方式为 tar.gz-->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <descriptors>
                        <descriptor>
                            distribution.xml
                        </descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                    </execution>
                </executions>
            </plugin>

 

distribution.xml内容如下:

 

<assembly>
    <id>dist</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>target/classes</directory>
            <outputDirectory>classes</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>bin</directory>
            <outputDirectory>bin</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

shell脚本示例如下:

 

#!/bin/bash

java_bin=$JAVA_HOME/bin/java
server_home=`dirname $0`/..
class_lib=$server_home/lib
log_home=$server_home/logs/example/
log_out=$log_home/stdout.log
log_err=$log_home/stderr.log

if [ ! -d $log_home ]; then
  mkdir -p $log_home
fi

#if [[ -e $server_home/example.pid ]]; then
#    kill `cat $server_home/example.pid`
#fi

java_opts="-Xmx512m -Dfile.encoding=utf8 -cp :${class_lib}/*"
$java_bin $java_opts com.test.example 1>>$log_out 2>>$log_err &
echo $! > $server_home/example.pid

注:log_home 为日志目录,example.pid为进程id,com.test.example为主函数入口类

打包后的jar包如下:

最后将该文件发到linux下解压进入bin目录,执行相应脚本即可运行(sh example.sh),然后可以到logs下查看对应的日志信息

另外,你可以在同一个项目中编写另外N个程序,只需要创建多个shell启动脚本,每个脚本中指定对应的函数入口类以及对应的进程id和日志目录即可

至此maven-assembly-plugin简单的打包方式介绍完毕!

整个项目结构如下:

Maven Assembly Plugin是一个强大的插件,它允许你在构建过程中将项目依赖项、源代码、文档等打包成一个或多个自定义的文件结构,比如传统的WAR、EAR、JAR等格式,或者创建更复杂的结构如Linux发行版风格的tar.gz文件。 当你想把外部jar包包含进你的项目部署包(例如,如果你的应用需要使用第三方库),你可以通过配置`maven-assembly-plugin`来完成。首先,你需要在pom.xml文件中添加`assembly-plugin`并指定相关的配置: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.example.ApplicationMain</mainClass> </manifest> </archive> <appendAssemblyId>false</appendAssemblyId> <dependencySets> <dependencySet> <excludes> <exclude>*:sources</exclude> <exclude>*:javadoc</exclude> </excludes> <outputDirectory>lib</outputDirectory> </dependencySet> </dependencySets> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 在这个例子中,`descriptorRef="jar-with-dependencies"`指定了生成的JAR会包含所有项目及其依赖的JAR。`<mainClass>`用于设置主入口。`dependencySets`部分告诉插件如何处理项目的依赖。 执行`mvn clean package`命令后,会在目标目录下生成一个新的包含所有依赖的JAR文件,方便部署。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值