maven打可执行jar包及依赖jar包

时间:2022-04-13 12:31:39

maven打可执行jar包,包含依赖jar包

maven安装本地jar包到仓库:

mvn install:install-file -DgroupId=com.alipay -DartifactId=alipay-pay-sdk -Dversion=1.0 -Dpackaging=jar -Dfile=C:\Users\Administrator\Desktop\alipay-sdk-java.jar

1、 准备一个package.xml文件

package.xml文件位置放在与pom.xml文件同等级目录下,
package.xml文件内容如下:

<assembly xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/assembly-1.0.0.xsd">

<id>distribution</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/bin</directory>
</fileSet>
</fileSets>

</assembly>

2、配置pomx.ml文件插件

pom.xml文件里面加上插件配置,内容如下:

<build>
<finalName>leo-count-push</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[2.0,)</versionRange>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>make-bin</id>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
<configuration>
<assembleDirectory>${project.build.directory}/bin</assembleDirectory>
<binFileExtensions>
<unix>.sh</unix>
</binFileExtensions>
<platforms>
<platform>windows</platform>
<platform>unix</platform>
</platforms>
<repositoryName>lib</repositoryName>
<programs>
<program>
<mainClass>com.leo.main.Run</mainClass>
<name>run-leo-count</name>
</program>
</programs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- not append assembly id in release file name -->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>${project.basedir}/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

到此为止,执行mvn clean package 命令就可以打可执行jar包了,打出来的包是一个zip包,只要把zip包解压,然后进入bin目录下可以看到有两个脚本,一个是*.sh,一个是*.bat,linux下执行sh脚本就可以,windows执行bat脚本就行。

注:这个方法打的jar包配置文件目录有点深入,且不能分环境打包,所以特意提供了一篇可以分环境打可执行jar包的文章,并且配置文件目录也在外层,方便操作。如果需要可以参考第二篇文章:

http://blog.csdn.net/u012204058/article/details/52986329