和项目制作可执行的jar包
如何把spring和hibernate项目制作可运行的jar包呢?
就是在命令行中运行 -jar xxx.jar 就可以运行java程序.例如
我的这个项目使用了hibernate和spring,不是web项目.
构建工具:maven
IDE:eclipse
目录结构如下:
上图中beans.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: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-3.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.2.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
- default-lazy-init="false">
-
-
- <bean id="dataSource" destroy-method="close"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="${jdbc.driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
- <bean
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <value>classpath:jdbc.properties</value>
- </property>
- </bean>
-
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <!--<property name="packagesToScan"> <list> <value>com.pass.bean</value>
- </list> </property> -->
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.Oracle10gDialect
- </prop>
- <prop key="hibernate.default_schema">whuang</prop>
- <prop key="hibernate.cache.provider_class">
- org.hibernate.cache.EhCacheProvider
- </prop>
- <prop key="hibernate.cache.provider_configuration_file_resource_path">
- com/config/core/ehcache.xml
- </prop>
- <prop key="hibernate.cache.use_second_level_cache">true</prop>
- <prop key="hibernate.cache.use_query_cache">false</prop>
- <prop key="hibernate.cache.use_minimal_puts">true</prop>
-
-
- <prop key="hibernate.order_updates">true</prop>
- <prop key="hibernate.generate_statistics">true</prop>
-
-
- <prop key="hibernate.show_sql">false</prop>
- <prop key="hibernate.format_sql">true</prop>
- <prop key="hibernate.hbm2ddl.auto">update</prop>
- <prop key="hibernate.use_sql_comments">true</prop>
- <prop key="current_session_context_class">thread</prop>
- <prop key="javax.persistence.validation.mode">none</prop>
- </props>
- </property>
- <property name="mappingLocations">
- <list>
- <value>classpath:com/provider/mapping/*.hbm.xml</value>
- </list>
- </property>
- </bean>
-
-
- <bean id="txManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- <!-- 事务的注解,如 @Transactional(readOnly=true) <tx:annotation-driven transaction-manager="txManager"
- /> -->
-
-
-
- <aop:config>
- <aop:pointcut id="bussinessService"
- expression="execution(public
- * com.dao..*.*(..)) || execution(public
- * com.common.dao.generic.*.*(..))" />
- <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
-
-
- </aop:config>
- <tx:advice id="txAdvice" transaction-manager="txManager">
- <tx:attributes>
- <tx:method name="load*" read-only="true" />
- <tx:method name="list*" read-only="true" />
- <tx:method name="get*" read-only="true" />
- <tx:method name="contain*" read-only="true" />
- <tx:method name="find*" read-only="true" />
- <tx:method name="test*" read-only="true" />
- <tx:method name="is*" read-only="true" />
- <tx:method name="show*" read-only="true" />
- <tx:method name="delete*" propagation="REQUIRED" />
- <tx:method name="update*" propagation="REQUIRED" />
- <tx:method name="save*" propagation="REQUIRED" />
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="set*" propagation="REQUIRED" />
- <tx:method name="verify*" read-only="true" />
- <tx:method name="max*" read-only="true" />
- <tx:method name="min*" read-only="true" />
- <tx:method name="dis*" read-only="true" />
- </tx:attributes>
- </tx:advice>
-
-
- </beans>
jdbc.properties内容:
- jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
- jdbc.url=jdbc:oracle:thin:@localhost:1521:whuang
- jdbc.username=whuang
- jdbc.password=whuang
如何打成可执行的jar包呢?
(1)修改读取spring配置文件的方式
在eclipse中使用
- new ClassPathXmlApplicationContext("beans.xml", "dao.xml");
打成jar包的话,要改成:
- new FileSystemXmlApplicationContext("beans.xml", "dao.xml");
(2)修改beans.xml中读取jdbc.properties的方式
原来是:
- <bean
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <value>classpath:jdbc.properties</value>
- </property>
- </bean>
打成jar,就需要改为:
- <bean
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <value>jdbc.properties</value>
- </property>
- </bean>
(3)maven中生成可执行jar的方式需要改maven plugin
原来使用
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <configuration>
- <appendAssemblyId>false</appendAssemblyId>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- <archive>
- <manifest>
- <mainClass>com.jn.NotepadApp</mainClass>
- </manifest>
- </archive>
- </configuration>
- <executions>
- <execution>
- <id>make-assembly</id>
- <phase>package</phase>
- <goals>
- <goal>assembly</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
打成jar,就需要改为:
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-shade-plugin</artifactId>
- <version>1.4</version>
- <executions>
- <execution>
- <phase>package</phase>
- <goals>
- <goal>shade</goal>
- </goals>
- <configuration>
- <transformers>
- <transformer
- implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
- <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>
- </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>
- </transformers>
- </configuration>
- </execution>
- </executions>
- </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
参考:
注意:
(a)打成jar包后,需要把spring的配置文件拷贝到jar包同级目录,所以读取spring配置文件使用FileSystemXmlApplicationContext,而不是ClassPathXmlApplicationContext
(b)jdbc.properties也在jar同级目录,所以需要修改beans.xml中org.springframework.beans.factory.config.PropertyPlaceholderConfigurer配置,去掉classpath:.
(c)其实在生成的jar包中也有一份beans.xml,dao.xml.jdbc.properties, 做上述的修改(蓝色标记)只是为了让jar读取文件系统(与jar同级目录)中的配置文件,而不是jar包里面的配置文件.