解決Spring導(dǎo)出可以運(yùn)行的jar包問題
最近需要解決Maven項(xiàng)目導(dǎo)入可執(zhí)行的jar包的問題,如果項(xiàng)目不包含Spring,那么使用mvn assembly:assembly即可,詳情可以參考:
http://www.dbjr.com.cn/article/276771.htm
可是如果包含Spring,那么這么方法就不可行,報(bào)錯(cuò):
Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace
我在網(wǎng)上折騰了兩天,這是assembly的一個(gè)bug。參見:http://jira.codehaus.org/browse/MASSEMBLY-360
據(jù)說原因是spring的多個(gè)jar包中都含有spring.handlers和spring.schemas文件,而assembly只會(huì)把第一次遇到的文件打入jar包,后面遇到的都會(huì)skip掉。
解決方法就是放棄assembly,使用shade插件來打包.在shade的打包配制中指明spring.handlers和spring.schemas文件會(huì)以append方式加入進(jìn)來,從而確保其他spring的jar中的這兩個(gè)文件的信息不會(huì)被遺漏。
下面是一個(gè)非常簡(jiǎn)單的例子,只有四個(gè)文件的Maven工程,代碼再附件內(nèi):
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);
}
}
}在命令行運(yùn)行:mvn package
然后在target目錄會(huì)產(chǎn)出一個(gè)jar包:my-spring-app.jar
運(yùn)行即可: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
當(dāng)然也可以這么運(yùn)行:java -classpath target/my-spring-app.jar com.exec.Main
源代碼下載地址:http://xiazai.jb51.net/202303/yuanma/main_spring_jb51.rar
到此這篇關(guān)于Spring導(dǎo)出可以運(yùn)行的jar包的文章就介紹到這了,更多相關(guān)Spring導(dǎo)出jar包內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot整合Netty實(shí)現(xiàn)RPC服務(wù)器的示例代碼
這篇文章主要介紹了Springboot整合Netty實(shí)現(xiàn)RPC服務(wù)器的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java數(shù)據(jù)結(jié)構(gòu)之LinkedList從鏈表到實(shí)現(xiàn)
LinkedList是Java中常用的數(shù)據(jù)結(jié)構(gòu)之一,實(shí)現(xiàn)了鏈表的特性,支持快速添加、刪除元素,可以用于實(shí)現(xiàn)隊(duì)列、棧、雙向隊(duì)列等數(shù)據(jù)結(jié)構(gòu)。LinkedList的內(nèi)部實(shí)現(xiàn)采用了雙向鏈表,其中每個(gè)節(jié)點(diǎn)都包含前驅(qū)節(jié)點(diǎn)和后繼節(jié)點(diǎn)的引用,可以直接訪問鏈表的頭尾元素2023-04-04
itextpdf提取PDF文件中的任意頁(yè)碼實(shí)現(xiàn)示例
這篇文章主要為大家介紹了itextpdf提取PDF文件中的任意頁(yè)碼實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Java中的super關(guān)鍵字_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java中的super關(guān)鍵字的相關(guān)知識(shí),需要的朋友參考下2017-04-04
解決IDEA克隆代碼后在右下角沒有g(shù)it分支的問題
這篇文章主要介紹了解決IDEA克隆代碼后在右下角沒有g(shù)it分支的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02

