您的位置:首页 > 编程语言 > Java开发

非web项目,maven工程整合spring+mabits,并打包为可运行jar包

2017-06-30 18:08 1056 查看
废话不多说,直接开干吧。

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运行的入口类以及依赖包位置



大功告成,可以自由的搞事情了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java mybatis spring maven
相关文章推荐