maven 可執行jar

今天作了個小項目,本地測試成功後想用maven-assembly-plugin插件打成個可執行jar包:java

pom.xml中添加插件以下:mysql

<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,而後執行 :spring

mvn assembly:assembly。這時在項目目錄下的target目錄裏會生成TestWithMaven-0.0.1-SNAPSHOT.jar。而後 cd E:\eclipse\SocStatistics\target,接着執行jar包:java -jar TestWithMaven-0.0.1-SNAPSHOT.jar,結果報異常以下:sql

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內容以下:apache

<?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包。session

2. http://blog.csdn.net/bluishglc/article/details/7596118 ,這裏的說法是:spring在加載xsd文件時老是先試圖在本地查找xsd文件(spring的jar包中已經包含了全部版本的xsd文件),若是沒有找到,纔會轉向去URL指定的路徑下載。關鍵我這項目是在外網作的,並且我把每個xsd文件的路徑都在外網試了下,都能找到。因此我推測是直接在本地找,找不到就報錯。app

無論如何,我按照第二種說法將xsd文件優先引用本地的,本地maven依賴包裏,都有個META-INF目錄,該目錄下有兩個文件spring.handlers和spring.schema,指明瞭xsd的處理類,以下:eclipse

因而我改用maven-shade-plugin,並把spring.handlers和spring.schemas文件以append方式加入到構建的jar包中將這兩件文件也打入jar包, maven

<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

相關文章
相關標籤/搜索