maven 可执行jar

原创
2016/05/31 14:14
阅读数 78

今天做了个小项目,本地测试成功后想用maven-assembly-plugin插件打成个可执行jar包:

pom.xml中添加插件如下:

<build>
		<plugins>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<appendAssemblyId>false</appendAssemblyId>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<archive>
						<manifest>
							<mainClass>socStatistics.StatisticsMain</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>assembly</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

		</plugins>
</build>

然后转到项目的目录下,cmd中执行命令: cd E:\eclipse\SocStatistics,然后执行 :

mvn assembly:assembly。这时在项目目录下的target目录里会生成TestWithMaven-0.0.1-SNAPSHOT.jar。然后 cd E:\eclipse\SocStatistics\target,接着执行jar包:java -jar TestWithMaven-0.0.1-SNAPSHOT.jar,结果报异常如下:

Configuration problem: Unable to locate Spring NamespaceHa ndler for XML schema namespace [http://www.springframework.org/schema/context] Offending resource: class path resource [beans.xml]

我的beans.xml内容如下:

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop   
   		http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
   		http://www.springframework.org/schema/tx   
    	http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
        
    <context:annotation-config></context:annotation-config>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
	<bean id="statisticsDao" class="socStatistics.StatisticsDao">
	</bean>
	
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
	     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
	     <property name="url" value="jdbc:mysql://localhost:3306/statistics"/> 
	     <property name="username" value="root"/> 
	     <property name="password" value="java"/> 
  	</bean> 
  	<bean id="transactionManager"  
    	class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
    	<property name="sessionFactory" ref="sessionFactory" />
	</bean> 
	
	 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <property name="hibernateProperties">  
            <props>  
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>  
                <prop key="hibernate.format_sql">true</prop>  
            </props>  
        </property> 
        
        <property name="packagesToScan">  
            <list>  
                <value>socStatistics.entity</value>  
            </list>  
        </property>    
    </bean>  
	    <!-- 配置JdbcTemplate -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    	 <property name="dataSource" ref="dataSource"></property>
    </bean>
      
</beans>

1. http://www.baeldung.com/unable-to-locate-spring-namespacehandler-for-xml-schema-namespace ,这篇文章里解释此原因是beans.xml里的namespace 找不到相应的namespace handler。(a namespace handler for one of the Spring namespaces is not found.)关键我项目里namespace提到的都有相应的jar包。

2. http://blog.csdn.net/bluishglc/article/details/7596118 ,这里的说法是:spring在加载xsd文件时总是先试图在本地查找xsd文件(spring的jar包中已经包含了所有版本的xsd文件),如果没有找到,才会转向去URL指定的路径下载。关键我这项目是在外网做的,而且我把每一个xsd文件的路径都在外网试了下,都能找到。所以我推测是直接在本地找,找不到就报错。

不管如何,我按照第二种说法将xsd文件优先引用本地的,本地maven依赖包里,都有个META-INF目录,该目录下有两个文件spring.handlers和spring.schema,指明了xsd的处理类,如下:

于是我改用maven-shade-plugin,并把spring.handlers和spring.schemas文件以append方式加入到构建的jar包中将这两件文件也打入jar包, 

<build>
<plugins>
<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>shade</goal>
						</goals>
						<configuration>
							<transformers>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.handlers</resource>
								</transformer>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
									<mainClass>socStatistics.StatisticsMain</mainClass>
								</transformer>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.schemas</resource>
								</transformer>

							</transformers>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

这时在项目目录下,不再执行 mvn assembly:assembly,而是mvn clean install,其余与第一种方案同,但这时会在target目录下生成两个jar包,original-TestWithMaven-0.0.1-SNAPSHOT.jar和estWithMaven-0.0.1-SNAPSHOT.jar,执行后者即成功。

这里有两篇参考,写的很详细:

http://chenzhou123520.iteye.com/blog/1706242

https://issues.apache.org/jira/browse/MASSEMBLY-360

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部