非web项目,maven工程整合spring+mabits,并打包为可运行jar包
2017-06-30 18:08
1056 查看
废话不多说,直接开干吧。
spring和mybatis如何整合这里就不多说了,主要说如何在非web项目中用到这两种,其中主要用到了
想必做过单元测试的同学对这句话很熟悉吧,在ApplicationContext实例化后,通过getBean方法从ApplicationContext容器中获取装配好的Bean实例以供使用,这里整合了mybatis,所以mybatis的bean也被装载了进来,也就可以使用mybatis对数据库操作了。这就和tomcat启动web应用一样,读取spring配置文件装载bean,在通过@resource等标签得到bean的实例,然后就可以做一些操作了。
项目结构如下:
和web工程没什么区别。接下来讲一下重点是如何打成可运行的jar包,在Linux下也可执行。
此处用到了以下maven插件
在pom文件下<build><plugins></plugins></build>中引入这两个插件,并做如下配置:
maven-assembly-plugin插件目的是提供一个把工程依赖元素、模块、网站文档等其他文件存放到单个归档文件里。
使用任何一个预定义的描述符你可以轻松的构建一个发布包。这些描述符能处理一些常用的操作,如:把依赖的元素的归档到一个jar文件. 当然, 你可以自定义描述符来更灵活的控制依赖,模块,文件的归档方式。
代表配置文件的位置,如下图所示,与src同级
package.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>bin</id>
<!-- 最终打包成一个zip文件 -->
<formats>
<format>zip</format>
</formats>
<!-- Adds dependencies to zip package under lib directory -->
<dependencySets>
<dependencySet>
<!-- 不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录 -->
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
<fileSets>
<!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
<fileSet&g
ab79
t;
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
做好如上配置后,直接可执行 mvn clean install即可打包为可执行jar包,并把依赖的lib放到指定位置。通过jar命令即可运行。项目最终如下:
META-INF下配置文件的内容,主要告诉Java运行的入口类以及依赖包位置
大功告成,可以自由的搞事情了
spring和mybatis如何整合这里就不多说了,主要说如何在非web项目中用到这两种,其中主要用到了
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"spring.xml"});
SecService service = ctx.getBean("secServiceImpl", SecService.class);
想必做过单元测试的同学对这句话很熟悉吧,在ApplicationContext实例化后,通过getBean方法从ApplicationContext容器中获取装配好的Bean实例以供使用,这里整合了mybatis,所以mybatis的bean也被装载了进来,也就可以使用mybatis对数据库操作了。这就和tomcat启动web应用一样,读取spring配置文件装载bean,在通过@resource等标签得到bean的实例,然后就可以做一些操作了。
public class Run { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"spring.xml"}); SecService service = ctx.getBean("secServiceImpl", SecService.class); //BaseConfig.getInstence("/app/cuishilei/secSh-0.0.1/baseConf.properties"); //BaseConfig.getInstence("F:\\MyIDEA\\secSh\\src\\main\\resources\\baseConf.properties"); while (true) { String x = printMenu(); switch (x) { case "1": service.insert4Trans(); break; case "2": service.getBgIdCardRepo(); break; case "3": service.insertMsgTrans(); break; case "4": testBs(service); break; default: System.out.println("无效的选项,请重新选择!"); break; } } }
项目结构如下:
和web工程没什么区别。接下来讲一下重点是如何打成可运行的jar包,在Linux下也可执行。
此处用到了以下maven插件
maven-jar-plugin
maven-assembly-plugin
在pom文件下<build><plugins></plugins></build>中引入这两个插件,并做如下配置:
<!-- The configuration of maven-jar-plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <!-- do not include pom.xml and pom.properties in the jar package --> <addMavenDescriptor>false</addMavenDescriptor> <manifest> <!-- put third party jar package into the classpath of manifest --> <addClasspath>true</addClasspath> <!-- the prefix of the jar items in the classpath, it depends on the location(folder) of jar files -->这个是打完jar包后依赖的jar包所处的位置 <classpathPrefix>lib/</classpathPrefix> <!-- main class of the jar package -->这个是jar包需要运行的入口类。 <mainClass>com.msds.secsh.Run</mainClass> </manifest> </archive> </configuration> </plugin> <!-- The configuration of maven-assembly-plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <!-- The configuration of the plugin --> <configuration> <!-- Specifies the configuration file of the assembly plugin --> <descriptors> <descriptor>conf/package.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
maven-assembly-plugin插件目的是提供一个把工程依赖元素、模块、网站文档等其他文件存放到单个归档文件里。
使用任何一个预定义的描述符你可以轻松的构建一个发布包。这些描述符能处理一些常用的操作,如:把依赖的元素的归档到一个jar文件. 当然, 你可以自定义描述符来更灵活的控制依赖,模块,文件的归档方式。
<descriptor>conf/package.xml</descriptor>
代表配置文件的位置,如下图所示,与src同级
package.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>bin</id>
<!-- 最终打包成一个zip文件 -->
<formats>
<format>zip</format>
</formats>
<!-- Adds dependencies to zip package under lib directory -->
<dependencySets>
<dependencySet>
<!-- 不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录 -->
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
<fileSets>
<!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
<fileSet&g
ab79
t;
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
做好如上配置后,直接可执行 mvn clean install即可打包为可执行jar包,并把依赖的lib放到指定位置。通过jar命令即可运行。项目最终如下:
META-INF下配置文件的内容,主要告诉Java运行的入口类以及依赖包位置
大功告成,可以自由的搞事情了
相关文章推荐
- 发现一个问题,十分蛋疼。 我们项目是由N个工程组成的,外围工程是web工程,内部的工程打包成jar,放入外围工程的WEB-INF/lib 内部的工程用到了spring的注解,例如@Service、
- Spring-boot web 工程构建,并把maven子项目模块依赖jar打包,笔记
- Spring boot框架项目,使用maven命令将配置文件打包到jar包外,项目运行读取jar外配置文件
- maven 打包web项目成jar, 可放在服务器上运行
- 转: 使用maven给spring项目打可直接运行的jar包(配置文件内置外置的打法)
- maven web 项目 没有spring-web.jar的问题解决方法
- maven打包web工程中一部分代码为jar
- maven打包web项目时同时打包为war和jar文件(备忘用)
- maven项目运行报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- eclipse整合tcmcat,构建并运行maven web项目
- 如何将maven项目包含工程依赖打包成可执行的jar
- Spring+SpringMVC+Mybatis整合系列(二)Eclipse新建Maven web项目
- spring和hibernate整合开发web项目的创建及所需的jar包的添加
- maven项目使用maven-assembly-plugin打包成可运行的jar
- 解决eclipse中maven web工程打包成war(发布到tomcar)时lib中没有jar包的解决方法
- 使用maven给spring项目打可直接运行的jar包(配置文件内置外置的打法)
- 将java项目打包成jar,war包,等 web工程打包成jar war
- maven把项目打包成可运行的jar包
- webservice之cxf实现[web项目中基于maven与spring整合]
- 使用maven给spring项目打可直接运行的jar包(配置文件内置外置的打法)