将Maven项目打包成可执行jar文件(引用第三方jar)
方法一. mvn assembly 或 mvn package (一个jar包)
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.tang.CSVUtils</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
</configuration>
<!-- 加上下面这一段,使用 mvn package命令,不加则使用mvn assembly-->
<!-- <executions>
<execution>
<id>make-assemble</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions> -->
</plugin>
</plugins>
</build>
Eclipse 中 Run As--- Maven clean ---Maven assembly:assembly(或 Maven package)
Target目录生成如下文件:
其中,
testLog4j-0.1.jar 是不可直接运行的,因为没有带第三方包。
testLog4j-0.1-jar-with-dependencies.jar 就是带有第三方包的可执行 jar 包,
在命令行执行 java -jar testLog4j-0.1-jar-with-dependencies.jar 即可运行
方法二:mvn package(一个jar包和一个lib文件夹)
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.tang.CSVUtils</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
执行maven package 进行打包进入target目录。
其中lib就是第三方jar包的目录,在其他项目引入testLog4j-0.1.jar会自动找到并引入lib里面的包.
只需要拷贝这两个(lib和testLog4j-0.1.jar)即可放到其他地方用了。
在命令行执行:java -jar testLog4j-0.1.jar
方法三:mvn package(推荐)(一个zip包,里面有一个jar包,一个lib文件夹,和一个conf文件夹)
<assembly>
<id>bin</id>
<!-- 最终打包成一个用于发布的zip文件 -->
<formats>
<format>zip</format>
</formats>
<!-- Adds dependencies to zip package under lib directory -->
<dependencySets>
<dependencySet>
<!--
不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录
-->
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
<fileSets>
<!-- 把项目相关的说明文件,打包进zip文件的根目录 -->
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>README*</include>
<include>LICENSE*</include>
<include>NOTICE*</include>
</includes>
</fileSet>
<!-- 把项目的配置文件,打包进zip文件的config目录 -->
<fileSet>
<directory>${project.basedir}\conf</directory>
<outputDirectory>conf</outputDirectory>
<includes>
<include>*.xml</include>
<include>*.properties</include>
<include>*.key</include>
</includes>
</fileSet>
<!-- 把项目的脚本文件目录( src/main/scripts )中的启动脚本文件,打包进zip文件的跟目录 -->
<fileSet>
<directory>${project.build.scriptSourceDirectory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>startup.*</include>
</includes>
</fileSet>
<!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
(2)pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerVersion>1.6</compilerVersion>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- The configuration of maven-jar-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<!-- The configuration of the plugin -->
<configuration>
<!-- Configuration of the archiver -->
<archive>
<!-- do not include pom.xml and pom.properties in the jar package -->
<addMavenDescriptor>false</addMavenDescriptor>
<!-- Manifest specific configuration -->
<manifest>
<!-- put third party jar package into the classpath of manifest -->
<addClasspath>true</addClasspath>
<!-- the prefix of the jar items in the classpath, it depends on the location(folder) of jar files -->
<classpathPrefix>lib/</classpathPrefix>
<!-- main class of the jar package-->
<mainClass>com.tang.your-Main-class</mainClass>
</manifest>
</archive>
<!-- excludes some files -->
<excludes>
<exclude>${project.basedir}/xml/*</exclude>
</excludes>
</configuration>
</plugin>
<!-- The configuration of maven-assembly-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<!-- The configuration of the plugin -->
<configuration>
<!-- Specifies the configuration file of the assembly plugin -->
<descriptors>
<descriptor>conf/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
将Maven项目打包成可执行jar文件(引用第三方jar)的更多相关文章
- 如何将maven项目打包成可执行的jar
如何将maven项目打包成可执行的jar 分类: maven2010-12-17 10:18 10411人阅读 评论(2) 收藏 举报 jarmavenassemblyjava 方法一:将项目及所依赖 ...
- Maven项目打包成可执行Jar文件
在使用Maven完成项目以后,如果需要打包成可执行的Jar文件,我们通过eclipse的导出很麻烦,还得指定入口文件的位置,还得说明依赖的jar包,既然都使用Maven了,很重要的一个目的就是让这些繁 ...
- maven项目打包成可执行的jar
编写功能类: package com.hpay.FileToZkUtil; import java.io.File; import java.io.FileInputStream; import ja ...
- 将Maven项目打包成可执行 jar文件(引用第三方jar)
使用maven assembly插件完成打包 修改pom: <build> <pluginManagement> <plugins> <!--设置jdk版本, ...
- maven 把spring项目打包成可执行的文件
转载自http://www.mamicod.e.com/info-detail-635726.html 最近需要解决Maven项目导入可执行的jar包的问题,如果项目不包含Spring,那么使用mvn ...
- 通过idea 打包 spring maven项目打包为可执行jar包
用assembly打包一直报错: shangyanshuodeMacBook-Pro:target shangyanshuo$ java -jar jobscrawler-1.0-SNAPSHOT-j ...
- 怎么将maven项目打包成war包
问题:我在eclipse上能运行,然后我想将这个maven项目打包成war然后放到另外一台机子上(其实是手动放在tomcat的webapp目录中),提供外部访问.现在问题是,一直maven项目打包一直 ...
- (3)Maven快速入门_3在Eclipse中创建Maven项目打包成jar
Eclipse中创建Maven项目 new ---> maven project ----> next 如下 普通java项目 选择 如下 quickstart 创建项目 : 输入 G ...
- java普通项目打包成可执行jar文件时如何添加第三包
在java的web项目中,引用第三方包的时候非常简单.因为在web项目上中,默认有一个web-inf文件夹.web-inf文件夹下有一个lib文件夹,如果有用到第三方包,直接丢进去就行了.但是对于普通 ...
随机推荐
- kettle mysql 乱码
在数据库连接上添加字符集编码参数characterEncoding, 指定UTF8或GBK
- Kaldi中的Chain模型
Chain模型的训练流程 链式模型的训练过程是MMI的无网格的版本,从音素级解码图生成HMM,对其使用前向后向算法,获得分母状态后验,通过类似的方式计算分子状态后验,但限于对应于转录的序列. 对于神经 ...
- Python 17 web框架&Django
本节内容 1.html里面的正则表达式 2.web样式简介 3.Django创建工程 Html里的正则表达式 test 用来判断字符串是否符合规定的正则 rep.test('....') ...
- 【try..catch..】【判断输入是否为空】【onchange事件】【onmouseover和onmouseout事件】【onmousedown和onmouseup事件】
1.try..catch.. <body><script>function myFunction(){try{ var x=document.getElementById(&q ...
- 实验吧 smali文件分析
题目链接:http://www.shiyanbar.com/ctf/1871 好吧,这题真是基础中的基础,基础中的战斗机. 不光没有任何绕弯的地方,逻辑有且仅有一个.... 多说无益,使用工具jadx ...
- 匿名内部类可以访问的变量---静态成员变量和final修饰的局部变量
在学习多线程的时候用到了匿名内部类,匿名内部类可以访问static静态成员变量或者final修饰的局部变量. 匿名内部类在编译之后会生成class文件,比如Test内的第一个匿名内部类编译之后就是Te ...
- linux下 gdb+coredump 调试偶发crash的程序
1. 打开 core dump 查看是否打开 ulimit -c 如果输出0, 说明没有打开. 方法一:使用命令 ulimit -c unlimited 可以打开,但是只对当前终端有效, 方法二: 配 ...
- Linux 网卡驱动学习(五)(收发包具体过程)【转】
转自:https://blog.csdn.net/xy010902100449/article/details/47362787 版权声明:本文为博主原创文章,未经博主允许不得转载. https:// ...
- requests库入门13-会话对象
会话对象可以在跨请求保持某些参数,会话对象有requests api的大部分方法,我理解会话对象就是一个资源共享池 使用requests.Session()可以创建会话对象的实例 还是以之前GitHu ...
- 设计模式C++学习笔记之十一(Bridge桥梁模式)
桥梁模式,将抽象部分与它的实现部分分离,使它们都可以独立地变化.实现分离的办法就是增加一个类, 11.1.解释 main(),客户 IProduct,产品接口 CHouse,房子 CIPod,ip ...