最近需要解决Maven项目导入可执行的jar包的问题,如果项目不包含Spring,那么使用mvn assembly:assembly即可,详情可以参考:http://www.cnblogs.com/liqiu/p/3816068.html

可是如果包含Spring,那么这么方法就不可行,报错:

Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace

我在网上折腾了两天,这是assembly的一个bug。参见:http://jira.codehaus.org/browse/MASSEMBLY-360

据说原因是spring的多个jar包中都含有spring.handlers和spring.schemas文件,而assembly只会把第一次遇到的文件打入jar包,后面遇到的都会skip掉。

解决方法就是放弃assembly,使用shade插件来打包.在shade的打包配制中指明spring.handlers和spring.schemas文件会以append方式加入进来,从而确保其他spring的jar中的这两个文件的信息不会被遗漏。

下面是一个非常简单的例子,只有四个文件的Maven工程,代码再附件内:

1、pom.xml

<project 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/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javaee</groupId>
<artifactId>main-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.7</version> <executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>my-spring-app</finalName>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>jar-with-dependencies</shadedClassifierName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.test.Main</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.tooling</resource>
</transformer>
</transformers> </configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

2、applicationContext.xml(Spring的配置文件)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<context:annotation-config />
<context:component-scan base-package="com.*" />
</beans>

3、代码事例

package com.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
public String findUser() {
return "find liqiu";
}
}
package com.exec;

import org.springframework.context.support.GenericXmlApplicationContext;

import com.service.UserService;

public class Main {

    public static void main(String[] args) throws InterruptedException {
GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.setValidating(false);
context.load("classpath:applicationContext.xml");
context.refresh();
UserService userService = context.getBean(UserService.class);
while (true) {
System.out.println(userService.findUser());
Thread.sleep(10000);
}
}
}

在命令行运行:mvn package

然后在target目录会产出一个jar包:my-spring-app.jar

运行即可:java -jar target/my-spring-app.jar

五月 16, 2015 10:46:52 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
五月 16, 2015 10:46:53 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@4a5e88f7: startup date [Sat May 16 22:46:53 CST 2015]; root of context hierarchy
五月 16, 2015 10:46:53 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18fd54ec: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,userService]; root of factory hierarchy
find liqiu
find liqiu

当然也可以这么运行:java -classpath target/my-spring-app.jar com.exec.Main

源代码下载地址:http://files.cnblogs.com/files/liqiu/main-spring.tar.gz

Spring导出可以运行的jar包的更多相关文章

  1. 使用IDEA导出可运行的jar包,包含引用第三方jar包

    这里我使用的第三方jar包是数据库的JDBC jar包导出案例. 1.创建一个Module,名称为dataBase,在里面我们先创建一个folder用来包含所需要的jar包,命名为lib 2.从外界复 ...

  2. 奥展项目笔记04--Spring cloud 通过父工程打包多个子工程,导出可运行的Jar包

    在spring cloud微服务搭建过程中,我们创建了多个微服务模块,如图: 1.父工程Pom文件 <?xml version="1.0" encoding="UT ...

  3. Eclipse导出可运行的jar包并运行

    https://blog.csdn.net/kpchen_0508/article/details/49275407 程序运行的第二种方式:

  4. 将 java 项目打包成可运行的 jar 包(main 函数带参数),并上传到 linux 服务器上运行

    一.概述 java项目有两种架构,一种是 B/S 架构的,一种是 C/S 架构的. 对于 B/S 架构来说,我们常见的 java ee 即是 B/S 架构,通常,开发人员会在本地进行开发,然后将项目打 ...

  5. hadoop:将WordCount打包成独立运行的jar包

    hadoop示例中的WordCount程序,很多教程上都是推荐以下二种运行方式: 1.将生成的jar包,复制到hadoop集群中的节点,然后运行 $HADOOP_HOME/bin/hadoop xxx ...

  6. 【转】maven导出项目依赖的jar包

    本文转自:http://my.oschina.net/cloudcoder/blog/212648 一.导出到默认目录 targed/dependency 从Maven项目中导出项目依赖的jar包:进 ...

  7. IntelliJ IDEA导出Java 可执行Jar包

    原文:IntelliJ IDEA导出Java 可执行Jar包 保证自己的Java代码是没有问题的,在IDEA里面是可以正常运行的,然后,按下面步骤: 打开File -> Project Stru ...

  8. Maven生成可以直接运行的jar包的多种方式

    Maven可以使用mvn package指令对项目进行打包,如果使用Java -jar xxx.jar执行运行jar文件,会出现"no main manifest attribute, in ...

  9. eclipse maven 导出项目依赖的jar包

    转自:https://blog.csdn.net/andyliulin/article/details/46544555 一.导出到默认目录 targed/dependency 从Maven项目中导出 ...

随机推荐

  1. python创建__init.py__文件导入模块仍然报错ModuleNotFoundError: No module named 'name'

    今自定义模块后非相同目录导出提示找不到模块报错信息如下: ModuleNotFoundError: No module named 'name' 各方查找各位大神方法很多 参考链接 1.在需要导入的文 ...

  2. xxx is not in sudoers file 解决(转)

    解决方案:首需要切换到root身份$su -(注意有- ,这和su是不同的,在用命令"su"的时候只是切换到root,但没有把root的环境变量传过去,还是当前用户的环境变量,用& ...

  3. 【C++ Primer 第16章】1. 定义模板 (一)

    类模板 #include<iostream> #include<vector> #include<memory> using namespace std; temp ...

  4. SpringMVC异常处理器

    本节内容: 异常处理思路 自定义异常类 自定义异常处理器 异常处理器配置 错误页面 异常测试 springmvc在处理请求过程中出现异常信息交由异常处理器进行处理,自定义异常处理器可以实现一个系统的异 ...

  5. 第八章| 1. MySQL数据库|库操作|表操作

    1.初识数据库 我们在编写任何程序之前,都需要事先写好基于网络操作一台主机上文件的程序(socket服务端与客户端程序),于是有人将此类程序写成一个 专门的处理软件,这就是mysql等数据库管理软件的 ...

  6. 001.Keepalived简介

    一 Keepalived 定义 Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案,可以解决静态路由出现的单点故障问题.一个LVS服务会有2台服务器运行Keepalived,一台 ...

  7. js基础梳理-内存空间

    我估计有很多像我这样非计算机专业的人进入到前端之后,总是在写业务代码,思考什么什么效果如何实现,导致很多基础概念型的东西都理解得并不太清楚.经常一碰到群里讨论的些笔试题什么的,总觉得自己像是一个假前端 ...

  8. 【Java】同步阻塞式(BIO)TCP通信

    TCP BIO 背景 网络编程的基本模型是Clien/Server模型,也就是两个进程之间进行相互通信,其中服务端提供位置信息(绑定的IP地址和监听端口),客户端通过连接操作向服务端监听的地址发起连接 ...

  9. 安装JDK提示: 该项不适于在指定状态下使用的错误

    解决办法有两个,两个办法不相关,运用其中一个就能解决问题. 解决方法:http://www.360doc.com/content/15/0407/14/19179788_461278604.shtml

  10. 排序遇到问题 JDK7的Comparison method violates its general contract

    图解JDK7的Comparison method violates its general contract异常 楼主分析的很详细,能力有限,我看得迷迷糊糊的,不过大致知道这个错误的起因了.学习了,谢 ...