方法一. 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. Codeforces Round #536 (Div. 2)

    前言 如您所见这又是一篇咕了的文章,直接咕了10天 好久没打CF了 所以还是个蓝名菜鸡 机房所有人都紫名及以上了,wtcl 这次前4题这么水虽然不知道为什么花了1h,结果不知道为什么搞到一半出锅了,后 ...

  2. 2017CCPC秦皇岛 M题Safest Buildings&&ZOJ3993【复杂模拟】

    题意: 给出两个半径R,r,R表示第一次的大圈半径,r表示第二次的小圈半径.第一次大圈的圆心位于(0,0),第二次小圈的圆心未知,但在大圈内,给你一个n,然后给出n个屋子的位置,问这些屋子中,第二次在 ...

  3. ubuntu安装GBK编码

    1 添加GBK编码 sudo vim /var/lib/locales/supported.d/local en_US.UTF-8 UTF-8 zh_CN.UTF-8 UTF-8 zh_CN.GBK ...

  4. url编码解码的问题(urlencode/quote)

    import urllib.parse params = { "wd":"hello人工智能" } # 将字典形式的进行编码 query_str = urlli ...

  5. 浏览器通知--window.Notification

    参考链接:http://blog.csdn.net/guoquanyou/article/details/51726571 Web Notifications是HTML5 的一个特性,目前我知道的有谷 ...

  6. ListBox、ListCtrl

    设置编辑框滚动条在最新的位置 //CEdit* editBox=(CEdit*)GetDlgItem(IDC_EDIT_RECV); //(editBox->LineScroll(editBox ...

  7. 【Git】在GitHub或OSChina上新建项目后,如何在本地第一次push代码到服务器

    场景1:将本地代码push到远程仓库上的master主分支 #初始化git,执行init命令后,默认新建本地分支master git init #关联远程仓库 git remote add origi ...

  8. CF1100B Build a Contest

    题目地址:CF1100B Build a Contest 水题,比赛时没想就敲了个 \(O(n^2)\) 的暴力上去,结果过了Pretest,然后被Hack了 两个数组: \(c_i\) 表示 \(i ...

  9. platform模块

    import platform ''' python中,platform模块给我们提供了很多方法去获取操作系统的信息 如: import platform platform.platform() #获 ...

  10. struts2框架之拦截器(参考第二天学习笔记)

    拦截器 1. 什么是拦截器 1). 与JavaWeb中的Filter比较相似. 2). 拦截器只能拦截Action!!! 2. Struts中定义了很多拦截器,其中defaultStack中的拦截器会 ...