Maven打包可执行Jar的几种方法

28 篇文章 0 订阅

引用地址: https://blog.csdn.net/xiaojiesu/article/details/51871705

 

 

 

一、无依赖其他任何jar

 

  1.  
    <build>
  2.  
    <plugins>
  3.  
    <plugin>
  4.  
    <groupId>org.apache.maven.plugins </groupId>
  5.  
    <artifactId>maven-jar-plugin </artifactId>
  6.  
    <version>2.4 </version>
  7.  
    <configuration>
  8.  
    <archive>
  9.  
    <manifest>
  10.  
    <addClasspath>true </addClasspath>
  11.  
    <classpathPrefix>lib/ </classpathPrefix>
  12.  
    <mainClass>com.think.TestMain </mainClass>
  13.  
    </manifest>
  14.  
    </archive>
  15.  
    </configuration>
  16.  
    </plugin>
  17.  
    </plugins>
  18.  
    </build>


运行:mvn clean package,在target中找到打包出来的,命令后运行java -jar xxx.jar即可,但是如果程序有依赖其他包,比如程序依赖jdbc去查询db,这时候再执行就会出现找不到jdbc依赖,因为我们并没有将依赖包打进去

 

 

二、解决依赖其他包时,可执行jar的打包

1、

  1.  
    <build>
  2.  
    <plugins>
  3.  
    <plugin>
  4.  
    <groupId>org.apache.maven.plugins </groupId>
  5.  
    <artifactId>maven-assembly-plugin </artifactId>
  6.  
    <version>2.3 </version>
  7.  
    <configuration>
  8.  
    <appendAssemblyId>false </appendAssemblyId>
  9.  
    <descriptorRefs>
  10.  
    <descriptorRef>jar-with-dependencies </descriptorRef>
  11.  
    </descriptorRefs>
  12.  
    <archive>
  13.  
    <manifest>
  14.  
    <mainClass>com.think.TestMain </mainClass>
  15.  
    </manifest>
  16.  
    </archive>
  17.  
    </configuration>
  18.  
    <executions>
  19.  
    <execution>
  20.  
    <id>make-assembly </id>
  21.  
    <phase>package </phase>
  22.  
    <goals>
  23.  
    <goal>assembly </goal>
  24.  
    </goals>
  25.  
    </execution>
  26.  
    </executions>
  27.  
    </plugin>
  28.  
    </plugins>
  29.  
    </build>


但以上方式用的比较少,因为我们依赖的jar,也会打进到我们最终生成的jar,这样不太好,假如你生成的jar要给别人使用,最好给一个纯净的。

 

一般用assembly会再用他另外一个功能,将我们的jar归档,打包成一个zip

2、打成一个zip包,发布项目的时候,将zip包copy到服务器上,直接unzip xxx.zip,里面包含要运行到jar以及依赖的lib,还有配置的config文件,即可直接启动服务

 

  1.  
    <build>
  2.  
    <resources>
  3.  
    <!-- 控制资源文件的拷贝 -->
  4.  
    <resource>
  5.  
    <directory>src/main/resources </directory>
  6.  
    <targetPath>${project.build.directory}/classes </targetPath>
  7.  
    </resource>
  8.  
    </resources>
  9.  
    <plugins>
  10.  
    <!-- 设置源文件编码方式 -->
  11.  
    <plugin>
  12.  
    <groupId>org.apache.maven.plugins </groupId>
  13.  
    <artifactId>maven-compiler-plugin </artifactId>
  14.  
    <configuration>
  15.  
    <source>1.6 </source>
  16.  
    <target>1.6 </target>
  17.  
    <encoding>UTF-8 </encoding>
  18.  
    </configuration>
  19.  
    </plugin>
  20.  
    <!-- The configuration of maven-jar-plugin -->
  21.  
    <plugin>
  22.  
    <groupId>org.apache.maven.plugins </groupId>
  23.  
    <artifactId>maven-jar-plugin </artifactId>
  24.  
    <version>2.4 </version>
  25.  
    <!-- The configuration of the plugin -->
  26.  
    <configuration>
  27.  
    <!-- Configuration of the archiver -->
  28.  
    <archive>
  29.  
     
  30.  
    <!--
  31.  
    生成的jar中,不要包含pom.xml和pom.properties这两个文件
  32.  
    -->
  33.  
    <addMavenDescriptor>false </addMavenDescriptor>
  34.  
     
  35.  
    <!-- Manifest specific configuration -->
  36.  
    <manifest>
  37.  
    <!--
  38.  
    是否要把第三方jar放到manifest的classpath中
  39.  
    -->
  40.  
    <addClasspath>true </addClasspath>
  41.  
    <!--
  42.  
    生成的manifest中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前缀是lib/
  43.  
    -->
  44.  
    <classpathPrefix>lib/ </classpathPrefix>
  45.  
    <!--
  46.  
    应用的main class
  47.  
    -->
  48.  
    <mainClass>com.think.TestMain </mainClass>
  49.  
    </manifest>
  50.  
    </archive>
  51.  
    <!--
  52.  
    过滤掉不希望包含在jar中的文件
  53.  
    -->
  54.  
    <!--<excludes>-->
  55.  
    <!--<exclude>${project.basedir}/xml/*</exclude>-->
  56.  
    <!--</excludes>-->
  57.  
    </configuration>
  58.  
    </plugin>
  59.  
     
  60.  
    <!-- The configuration of maven-assembly-plugin -->
  61.  
    <plugin>
  62.  
    <groupId>org.apache.maven.plugins </groupId>
  63.  
    <artifactId>maven-assembly-plugin </artifactId>
  64.  
    <version>2.4 </version>
  65.  
    <!-- The configuration of the plugin -->
  66.  
    <configuration>
  67.  
    <!-- Specifies the configuration file of the assembly plugin -->
  68.  
    <descriptors>
  69.  
    <descriptor>src/main/assembly/assembly.xml </descriptor>
  70.  
    </descriptors>
  71.  
    </configuration>
  72.  
    <executions>
  73.  
    <execution>
  74.  
    <id>make-assembly </id>
  75.  
    <phase>package </phase>
  76.  
    <goals>
  77.  
    <goal>single </goal>
  78.  
    </goals>
  79.  
    </execution>
  80.  
    </executions>
  81.  
    </plugin>
  82.  
    </plugins>
  83.  
    </build>

 

还有一个重要的文件,名字可以随便起,我上面用的是 src/main/assembly/assembly.xml

 

  1.  
    <assembly>
  2.  
    <id>bin </id>
  3.  
    <includeBaseDirectory>false </includeBaseDirectory>
  4.  
    <!-- 最终打包成一个用于发布的zip文件 -->
  5.  
    <formats>
  6.  
    <format>zip </format>
  7.  
    </formats>
  8.  
     
  9.  
    <!-- Adds dependencies to zip package under lib directory -->
  10.  
    <dependencySets>
  11.  
    <dependencySet>
  12.  
    <!--
  13.  
    不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录
  14.  
    -->
  15.  
    <useProjectArtifact>false </useProjectArtifact>
  16.  
    <!--<outputDirectory>lib</outputDirectory>-->
  17.  
    <unpack>false </unpack>
  18.  
    </dependencySet>
  19.  
    </dependencySets>
  20.  
     
  21.  
    <fileSets>
  22.  
    <!-- 把项目相关的说明文件,打包进zip文件的根目录 -->
  23.  
    <!--<fileSet>-->
  24.  
    <!--<directory>${project.basedir}</directory>-->
  25.  
    <!--<outputDirectory>/</outputDirectory>-->
  26.  
    <!--</fileSet>-->
  27.  
     
  28.  
    <!-- 把项目的配置文件,打包进zip文件的config目录 -->
  29.  
    <fileSet>
  30.  
    <directory>${deploy.dir}/classes/ </directory>
  31.  
    <outputDirectory>/conf </outputDirectory>
  32.  
    <includes>
  33.  
    <include>*.xml </include>
  34.  
    <include>*.properties </include>
  35.  
    </includes>
  36.  
    </fileSet>
  37.  
    <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
  38.  
    <fileSet>
  39.  
    <directory>${project.build.directory} </directory>
  40.  
    <outputDirectory> </outputDirectory>
  41.  
    <includes>
  42.  
    <include>*.jar </include>
  43.  
    </includes>
  44.  
    </fileSet>
  45.  
    </fileSets>
  46.  
    </assembly>


最终执行命令:mvn clean package,出来的是这样的

解压zip包,我们看到我们想要的,good

 

3、还有一种打包方式,上面相当于把我们想要的东西打成一个zip包,全部放到一起,看着整洁好看,但有点繁琐,我们其实可以用另外一个插件来完成,不打包,即看到上图解压后的文件

 

  1.  
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.  
    xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.  
    <modelVersion>4.0.0 </modelVersion>
  4.  
     
  5.  
    <groupId>com.test </groupId>
  6.  
    <artifactId>myTestJar </artifactId>
  7.  
    <version>1.0-SNAPSHOT </version>
  8.  
    <packaging>jar </packaging>
  9.  
     
  10.  
    <name>myTestJar </name>
  11.  
    <url>http://maven.apache.org </url>
  12.  
     
  13.  
    <properties>
  14.  
    <deploy.dir>./target/ </deploy.dir>
  15.  
    <project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
  16.  
    </properties>
  17.  
     
  18.  
    <dependencies>
  19.  
    <dependency>
  20.  
    <groupId>junit </groupId>
  21.  
    <artifactId>junit </artifactId>
  22.  
    <version>3.8.1 </version>
  23.  
    <scope>test </scope>
  24.  
    </dependency>
  25.  
     
  26.  
    <dependency>
  27.  
    <groupId>com.alibaba </groupId>
  28.  
    <artifactId>druid </artifactId>
  29.  
    <version>1.0.9 </version>
  30.  
    </dependency>
  31.  
     
  32.  
    <dependency>
  33.  
    <groupId>mysql </groupId>
  34.  
    <artifactId>mysql-connector-java </artifactId>
  35.  
    <version>5.1.32 </version>
  36.  
    </dependency>
  37.  
    </dependencies>
  38.  
     
  39.  
    <build>
  40.  
    <finalName>myTest </finalName>
  41.  
    <sourceDirectory>src/main/java </sourceDirectory>
  42.  
    <resources>
  43.  
    <!-- 控制资源文件的拷贝 -->
  44.  
    <resource>
  45.  
    <directory>src/main/resources </directory>
  46.  
    <targetPath>${project.build.directory} </targetPath>
  47.  
    </resource>
  48.  
    </resources>
  49.  
    <plugins>
  50.  
    <!-- 设置源文件编码方式 -->
  51.  
    <plugin>
  52.  
    <groupId>org.apache.maven.plugins </groupId>
  53.  
    <artifactId>maven-compiler-plugin </artifactId>
  54.  
    <configuration>
  55.  
    <defaultLibBundleDir>lib </defaultLibBundleDir>
  56.  
    <source>1.6 </source>
  57.  
    <target>1.6 </target>
  58.  
    <encoding>UTF-8 </encoding>
  59.  
    </configuration>
  60.  
    </plugin>
  61.  
    <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
  62.  
    <plugin>
  63.  
    <groupId>org.apache.maven.plugins </groupId>
  64.  
    <artifactId>maven-jar-plugin </artifactId>
  65.  
    <configuration>
  66.  
    <archive>
  67.  
    <manifest>
  68.  
    <addClasspath>true </addClasspath>
  69.  
    <classpathPrefix>lib/ </classpathPrefix>
  70.  
    <mainClass>com.think.TestMain </mainClass>
  71.  
    </manifest>
  72.  
    </archive>
  73.  
    </configuration>
  74.  
    </plugin>
  75.  
    <!-- 拷贝依赖的jar包到lib目录 -->
  76.  
    <plugin>
  77.  
    <groupId>org.apache.maven.plugins </groupId>
  78.  
    <artifactId>maven-dependency-plugin </artifactId>
  79.  
    <executions>
  80.  
    <execution>
  81.  
    <id>copy </id>
  82.  
    <phase>package </phase>
  83.  
    <goals>
  84.  
    <goal>copy-dependencies </goal>
  85.  
    </goals>
  86.  
    <configuration>
  87.  
    <!-- ${project.build.directory}是maven变量,内置的,表示target目录,如果不写,将在跟目录下创建/lib -->
  88.  
    <outputDirectory>${project.build.directory}/lib </outputDirectory>
  89.  
    <!-- excludeTransitive:是否不包含间接依赖包,比如我们依赖A,但是A又依赖了B,我们是否也要把B打进去 默认不打-->
  90.  
    <excludeTransitive>true </excludeTransitive>
  91.  
    <!-- 复制的jar文件去掉版本信息 -->
  92.  
    <stripVersion>true </stripVersion>
  93.  
    </configuration>
  94.  
    </execution>
  95.  
    </executions>
  96.  
    </plugin>
  97.  
    <!-- 解决资源文件的编码问题 -->
  98.  
    <plugin>
  99.  
    <groupId>org.apache.maven.plugins </groupId>
  100.  
    <artifactId>maven-resources-plugin </artifactId>
  101.  
    <version>2.3 </version>
  102.  
    <configuration>
  103.  
    <encoding>UTF-8 </encoding>
  104.  
    </configuration>
  105.  
    </plugin>
  106.  
    <!-- 打包source文件为jar文件 -->
  107.  
    <plugin>
  108.  
    <artifactId>maven-source-plugin </artifactId>
  109.  
    <version>2.1 </version>
  110.  
    <configuration>
  111.  
    <attach>true </attach>
  112.  
    <encoding>UTF-8 </encoding>
  113.  
    </configuration>
  114.  
    <executions>
  115.  
    <execution>
  116.  
    <phase>compile </phase>
  117.  
    <goals>
  118.  
    <goal>jar </goal>
  119.  
    </goals>
  120.  
    </execution>
  121.  
    </executions>
  122.  
    </plugin>
  123.  
    </plugins>
  124.  
    </build>
  125.  
     
  126.  
    </project>


这里采用的是maven-dependency-plugin插件,进行资源的copy。

4、

  1.  
    <build>
  2.  
    <resources>
  3.  
    <resource>
  4.  
    <targetPath>${project.build.directory}/classes </targetPath>
  5.  
    <directory>src/main/resources </directory>
  6.  
    <filtering>true </filtering>
  7.  
    <includes>
  8.  
    <include>**/*.xml </include>
  9.  
    </includes>
  10.  
    </resource>
  11.  
    </resources>
  12.  
    <plugins>
  13.  
    <plugin>
  14.  
    <groupId>org.apache.maven.plugins </groupId>
  15.  
    <artifactId>maven-compiler-plugin </artifactId>
  16.  
    <version>3.0 </version>
  17.  
    <configuration>
  18.  
    <source>1.6 </source>
  19.  
    <target>1.6 </target>
  20.  
    <encoding>UTF-8 </encoding>
  21.  
    </configuration>
  22.  
    </plugin>
  23.  
    <plugin>
  24.  
    <groupId>org.apache.maven.plugins </groupId>
  25.  
    <artifactId>maven-shade-plugin </artifactId>
  26.  
    <version>2.0 </version>
  27.  
    <executions>
  28.  
    <execution>
  29.  
    <phase>package </phase>
  30.  
    <goals>
  31.  
    <goal>shade </goal>
  32.  
    </goals>
  33.  
    <configuration>
  34.  
    <transformers>
  35.  
    <transformer
  36.  
    implementation= "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  37.  
    <mainClass>com.think.TestMain </mainClass>
  38.  
    </transformer>
  39.  
    <!--<transformer-->
  40.  
    <!--implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">-->
  41.  
    <!--<resource>applicationContext.xml</resource>-->
  42.  
    <!--</transformer>-->
  43.  
    </transformers>
  44.  
    <shadedArtifactAttached>true </shadedArtifactAttached>
  45.  
    <shadedClassifierName>executable </shadedClassifierName>
  46.  
    </configuration>
  47.  
    </execution>
  48.  
    </executions>
  49.  
    </plugin>
  50.  
    </plugins>
  51.  
    </build>


这种方式打出来是柔和到一起,成为一个jar,

可以直接java -jar xxx.jar运行。

 

我们可以根据不同需要来打包,如果暴露给外面,可以采用第4种,如果是自己公司项目打包,建议2,3种,因为有时候只是改了个配置文件,不需要打包,直接把配置文件复制进去即可

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值