spring 和hibernate项目制作可执行的jar包

spring 和hibernate项目制作可执行的jar包

如何把spring和hibernate项目制作可运行的jar包呢?

就是在命令行中运行 Java -jar  xxx.jar 就可以运行java程序.例如
spring 和hibernate项目制作可执行的jar包
 我的这个项目使用了hibernate和spring,不是web项目.

构建工具:maven

IDE:eclipse

目录结构如下:
spring 和hibernate项目制作可执行的jar包
 


spring 和hibernate项目制作可执行的jar包
 

上图中beans.xml是spring的配置文件,内容如下:

 

Xml代码  spring 和hibernate项目制作可执行的jar包
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  7.             http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  9.            http://www.springframework.org/schema/aop  
  10.            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
  11.            http://www.springframework.org/schema/tx   
  12.            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"  
  13.     default-lazy-init="false">  
  14.   
  15.   
  16. <bean id="dataSource" destroy-method="close"  
  17.         class="org.apache.commons.dbcp.BasicDataSource">  
  18.         <property name="driverClassName" value="${jdbc.driverClassName}" />  
  19.         <property name="url" value="${jdbc.url}" />  
  20.         <property name="username" value="${jdbc.username}" />  
  21.         <property name="password" value="${jdbc.password}" />  
  22.     </bean>  
  23.     <bean  
  24.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  25.         <property name="locations">  
  26.             <value>classpath:jdbc.properties</value>  
  27.         </property>  
  28.     </bean>  
  29.   
  30.     <bean id="sessionFactory"  
  31.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  32.         <property name="dataSource" ref="dataSource" />  
  33.         <!--<property name="packagesToScan"> <list> <value>com.pass.bean</value>   
  34.             </list> </property> -->  
  35.         <property name="hibernateProperties">  
  36.             <props>  
  37.                 <prop key="hibernate.dialect">  
  38.                     org.hibernate.dialect.Oracle10gDialect  
  39.                 </prop>  
  40.                 <prop key="hibernate.default_schema">whuang</prop>  
  41.                 <prop key="hibernate.cache.provider_class">  
  42.                     org.hibernate.cache.EhCacheProvider  
  43.                 </prop>  
  44.                 <prop key="hibernate.cache.provider_configuration_file_resource_path">  
  45.                     com/config/core/ehcache.xml  
  46.                 </prop>  
  47.                 <prop key="hibernate.cache.use_second_level_cache">true</prop>  
  48.                 <prop key="hibernate.cache.use_query_cache">false</prop>  
  49.                 <prop key="hibernate.cache.use_minimal_puts">true</prop>  
  50.                 <!-- Cache complete -->  
  51.   
  52.                 <prop key="hibernate.order_updates">true</prop>  
  53.                 <prop key="hibernate.generate_statistics">true</prop>  
  54.                   
  55.                 <!-- org.hibernate.dialect.PostgreSQLDialect -->  
  56.                 <prop key="hibernate.show_sql">false</prop>  
  57.                 <prop key="hibernate.format_sql">true</prop>  
  58.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  59.                 <prop key="hibernate.use_sql_comments">true</prop>  
  60.                 <prop key="current_session_context_class">thread</prop>  
  61.                 <prop key="javax.persistence.validation.mode">none</prop>  
  62.             </props>  
  63.         </property>  
  64.         <property name="mappingLocations">  
  65.             <list>  
  66.                 <value>classpath:com/provider/mapping/*.hbm.xml</value>  
  67.             </list>  
  68.         </property>  
  69.     </bean>  
  70.   
  71.   
  72.     <bean id="txManager"  
  73.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  74.         <property name="sessionFactory" ref="sessionFactory"></property>  
  75.     </bean>  
  76.     <!-- 事务的注解,如 @Transactional(readOnly=true<tx:annotation-driven transaction-manager="txManager"   
  77.         /> -->  
  78.   
  79.       
  80.   
  81.     <aop:config>  
  82.         <aop:pointcut id="bussinessService"  
  83.             expression="execution(public   
  84.         * com.dao..*.*(..)) || execution(public   
  85.         * com.common.dao.generic.*.*(..))" />  
  86.         <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />  
  87.   
  88.   
  89.     </aop:config>  
  90.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  91.         <tx:attributes>  
  92.             <tx:method name="load*" read-only="true" />  
  93.             <tx:method name="list*" read-only="true" />  
  94.             <tx:method name="get*" read-only="true" />  
  95.             <tx:method name="contain*" read-only="true" />  
  96.             <tx:method name="find*" read-only="true" />  
  97.             <tx:method name="test*" read-only="true" />  
  98.             <tx:method name="is*" read-only="true" />  
  99.             <tx:method name="show*" read-only="true" />  
  100.             <tx:method name="delete*" propagation="REQUIRED" />  
  101.             <tx:method name="update*" propagation="REQUIRED" />  
  102.             <tx:method name="save*" propagation="REQUIRED" />  
  103.             <tx:method name="add*" propagation="REQUIRED" />  
  104.             <tx:method name="add*" propagation="REQUIRED" />  
  105.             <tx:method name="set*" propagation="REQUIRED" />  
  106.             <tx:method name="verify*" read-only="true" />  
  107.             <tx:method name="max*" read-only="true" />  
  108.             <tx:method name="min*" read-only="true" />  
  109.             <tx:method name="dis*" read-only="true" />  
  110.         </tx:attributes>  
  111.     </tx:advice>  
  112.       
  113.   
  114. </beans>  

 

 

jdbc.properties内容:

 

Java代码  spring 和hibernate项目制作可执行的jar包
  1. jdbc.driverClassName=oracle.jdbc.driver.OracleDriver  
  2. jdbc.url=jdbc:oracle:thin:@localhost:1521:whuang  
  3. jdbc.username=whuang  
  4. jdbc.password=whuang  

 

 

如何打成可执行的jar包呢?

(1)修改读取spring配置文件的方式

在eclipse中使用

 

Java代码  spring 和hibernate项目制作可执行的jar包
  1. new ClassPathXmlApplicationContext("beans.xml""dao.xml");  

 打成jar包的话,要改成:

 

 

Java代码  spring 和hibernate项目制作可执行的jar包
  1. new FileSystemXmlApplicationContext("beans.xml""dao.xml");  

 

 

(2)修改beans.xml中读取jdbc.properties的方式

原来是:

 

Xml代码  spring 和hibernate项目制作可执行的jar包
  1. <bean  
  2.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  3.         <property name="locations">  
  4.             <value>classpath:jdbc.properties</value>  
  5.         </property>  
  6.     </bean>  

 打成jar,就需要改为:

 

 

Xml代码  spring 和hibernate项目制作可执行的jar包
  1. <bean  
  2.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  3.         <property name="locations">  
  4.             <value>jdbc.properties</value>  
  5.         </property>  
  6.     </bean>  

 

 

(3)maven中生成可执行jar的方式需要改maven plugin

原来使用

 

Xml代码  spring 和hibernate项目制作可执行的jar包
  1. <plugin>  
  2.                 <artifactId>maven-assembly-plugin</artifactId>  
  3.                 <configuration>  
  4.                     <appendAssemblyId>false</appendAssemblyId>  
  5.                     <descriptorRefs>  
  6.                         <descriptorRef>jar-with-dependencies</descriptorRef>  
  7.                     </descriptorRefs>  
  8.                     <archive>  
  9.                         <manifest>  
  10.                             <mainClass>com.jn.NotepadApp</mainClass>  
  11.                         </manifest>  
  12.                     </archive>  
  13.                 </configuration>  
  14.                 <executions>  
  15.                     <execution>  
  16.                         <id>make-assembly</id>  
  17.                         <phase>package</phase>  
  18.                         <goals>  
  19.                             <goal>assembly</goal>  
  20.                         </goals>  
  21.                     </execution>  
  22.                 </executions>  
  23.             </plugin>  

 打成jar,就需要改为:

 

 

Java代码  spring 和hibernate项目制作可执行的jar包
  1. <plugin>  
  2.                 <groupId>org.apache.maven.plugins</groupId>  
  3.                 <artifactId>maven-shade-plugin</artifactId>  
  4.                 <version>1.4</version>  
  5.                 <executions>  
  6.                     <execution>  
  7.                         <phase>package</phase>  
  8.                         <goals>  
  9.                             <goal>shade</goal>  
  10.                         </goals>  
  11.                         <configuration>  
  12.                             <transformers>  
  13.                                 <transformer  
  14.                                     implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  15.                                     <mainClass><span style="font-size: 1em; line-height: 1.5;">com.jn.NotepadApp</span><span style="font-size: 1em; line-height: 1.5;"></mainClass></span>  
  16.                                 </transformer>  
  17.                                 <transformer  
  18.                                     implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
  19.                                     <resource>META-INF/spring.handlers</resource>  
  20.                                 </transformer>  
  21.                                 <transformer  
  22.                                     implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
  23.                                     <resource>META-INF/spring.schemas</resource>  
  24.                                 </transformer>  
  25.                             </transformers>  
  26.                         </configuration>  
  27.                     </execution>  
  28.                 </executions>  
  29.             </plugin>  

 

 

打开cmd,进入项目所在目录

运行 mvn clean package -U

就可以生成jar包:original-hibernate_spring_executable-0.0.1-SNAPSHOT.jar和hibernate_spring_executable-0.0.1-SNAPSHOT.jar,不用管original-hibernate_spring_executable-0.0.1-SNAPSHOT.jar,

(4)此时还要删除jar包中的META-INF\BCKEY.DSA,否则报错:

Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

参考:http://hw1287789687.iteye.com/blog/2019501

注意:

(a)打成jar包后,需要把spring的配置文件拷贝到jar包同级目录,所以读取spring配置文件使用FileSystemXmlApplicationContext,而不是ClassPathXmlApplicationContext

spring 和hibernate项目制作可执行的jar包
 

 

(b)jdbc.properties也在jar同级目录,所以需要修改beans.xml中org.springframework.beans.factory.config.PropertyPlaceholderConfigurer配置,去掉classpath:.

(c)其实在生成的jar包中也有一份beans.xml,dao.xml.jdbc.properties,
spring 和hibernate项目制作可执行的jar包
 做上述的修改(蓝色标记)只是为了让jar读取文件系统(与jar同级目录)中的配置文件,而不是jar包里面的配置文件.

上一篇:iphone开发之获取系统磁盘大小和可用磁盘大小


下一篇:Android两层TAB嵌套Spinner点击异常