方法一. mvn assembly 或 mvn package (一个jar包)

把依赖包和自己项目的文件打包如同一个jar包(这种方式对spring的项目不支持)

<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文件夹)

(1)conf/package.xml (conf 文件夹和pom.xml在同一级目录)

<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>

mvn install后,此时会在target目录下生成一个zip文件,如:
 youArtifactId-0.0.1-bin.zip 解压后有:
conf 文件夹, lib文件夹,  youArtifactId-0.0.1-bin.jar
博客来源:http://blog.csdn.net/qq804702802/article/details/47838241

将Maven项目打包成可执行jar文件(引用第三方jar)的更多相关文章

  1. 如何将maven项目打包成可执行的jar

    如何将maven项目打包成可执行的jar 分类: maven2010-12-17 10:18 10411人阅读 评论(2) 收藏 举报 jarmavenassemblyjava 方法一:将项目及所依赖 ...

  2. Maven项目打包成可执行Jar文件

    在使用Maven完成项目以后,如果需要打包成可执行的Jar文件,我们通过eclipse的导出很麻烦,还得指定入口文件的位置,还得说明依赖的jar包,既然都使用Maven了,很重要的一个目的就是让这些繁 ...

  3. maven项目打包成可执行的jar

    编写功能类: package com.hpay.FileToZkUtil; import java.io.File; import java.io.FileInputStream; import ja ...

  4. 将Maven项目打包成可执行 jar文件(引用第三方jar)

    使用maven assembly插件完成打包 修改pom: <build> <pluginManagement> <plugins> <!--设置jdk版本, ...

  5. maven 把spring项目打包成可执行的文件

    转载自http://www.mamicod.e.com/info-detail-635726.html 最近需要解决Maven项目导入可执行的jar包的问题,如果项目不包含Spring,那么使用mvn ...

  6. 通过idea 打包 spring maven项目打包为可执行jar包

    用assembly打包一直报错: shangyanshuodeMacBook-Pro:target shangyanshuo$ java -jar jobscrawler-1.0-SNAPSHOT-j ...

  7. 怎么将maven项目打包成war包

    问题:我在eclipse上能运行,然后我想将这个maven项目打包成war然后放到另外一台机子上(其实是手动放在tomcat的webapp目录中),提供外部访问.现在问题是,一直maven项目打包一直 ...

  8. (3)Maven快速入门_3在Eclipse中创建Maven项目打包成jar

    Eclipse中创建Maven项目 new ---> maven project  ----> next 如下 普通java项目 选择  如下 quickstart 创建项目 : 输入 G ...

  9. java普通项目打包成可执行jar文件时如何添加第三包

    在java的web项目中,引用第三方包的时候非常简单.因为在web项目上中,默认有一个web-inf文件夹.web-inf文件夹下有一个lib文件夹,如果有用到第三方包,直接丢进去就行了.但是对于普通 ...

随机推荐

  1. Nginx下配置网站ssl实现https访问

    第一步:服务器环境,lnmp即Linux+Nginx+PHP+MySQL,本文中以我的博客为例,使用的是阿里云最低档的vps+免费的Linux服务器管理系统WDCP快速搭建的lnmp环境(同类产品还有 ...

  2. luogu P3238 [HNOI2014]道路堵塞

    传送门 这什么题啊,乱搞就算了,不知道SPFA已经死了吗 不对那个时候好像还没死 暴力就是删掉边后跑Dijkstra SPFA 然后稍微分析一下,可以发现题目中要求的不经过最短路某条边的路径,一定是先 ...

  3. mysql存储过程及拼接字符串的用法

    DROP PROCEDURE IF EXISTS insert_historytable;DELIMITER //CREATE PROCEDURE insert_historytable()BEGIN ...

  4. 分页插件通用处理,以asp.net mvc为例

    Model: public class PaggerModel { public PaggerModel() { BarSize = ; } public PaggerModel(int total, ...

  5. Python 21 Django 实用小案例1

    实用案例 验证码与验证   KindEditor      组合搜索的实现 单例模式      beautifulsoup4 验证码与验证 需要安装Pillow模块 pip stall pillow ...

  6. Ubuntu16.04安装最新版nodejs

    原文链接:https://www.jianshu.com/p/2b24cd430a7d

  7. ovs常用操作

    1.添加网桥:ovs-vsctl add-br 交换机名 2.删除网桥:ovs-vsctl del-br 交换机名 3.添加端口:ovs-vsctl add-port 交换机名 端口名(网卡名) 4. ...

  8. java jvm和android DVM区别

      本文转自:http://blog.csdn.net/yujun411522/article/details/45932247   1.Android dvm的进程和Linux的进程, 应用程序的进 ...

  9. 876. Middle of the Linked List

    1. 原始题目 Given a non-empty, singly linked list with head node head, return a middle node of linked li ...

  10. hibernate框架学习第二天:核心API、工具类、事务、查询、方言、主键生成策略等

    核心API Configuration 描述的是一个封装所有配置信息的对象 1.加载hibernate.properties(非主流,早期) Configuration conf = new Conf ...