非web專案,maven工程整合spring+mabits,並打包為可執行jar包
阿新 • • 發佈:2019-01-03
廢話不多說,直接開幹吧。
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>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
做好如上配置後,直接可執行 mvn clean install即可打包為可執行jar包,並把依賴的lib放到指定位置。通過jar命令即可執行。專案最終如下:
META-INF下配置檔案的內容,主要告訴Java執行的入口類以及依賴包位置
大功告成,可以自由的搞事情了