详解Maven项目利用java service wrapper将Java程序生成Windows服务

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了详解Maven项目利用java service wrapper将Java程序生成Windows服务相关的知识,希望对你有一定的参考价值。

在项目的开发中,有时候需要将Java应用程序打包成Windows服务,我们就直接可以通过windows的服务来启动和关闭java程序了。

本博文将通过有两种方法实现该功能,手动创建法和Maven自动打包法。

一. 准备

下载java service wrapper

    网址:http://sourceforge.net/projects/wrapper/http://wrapper.tanukisoftware.com/doc/english/download.jsp

   我下载的版本是wrapper-windows-x86-64-3.5.26-st.zip。

二.手动创建法

1. 配置过程

    1)首先确定你的电脑上有java运行环境,没有的话请安装。

    2)将你的java程序打包成jar包。(我的jar的名称为wrapper-0.0.1-SNAPSHOT.jar,main方法所在类App)

    3)在硬盘上创建文件夹test,并在其下创建文件夹bin, conf, lib, logs。

    4)解压wrapper-windows-x86-64-3.5.26-st.zip, 并将其bin目录下的Wrapper.exe、src/bin目录下的App.bat.in、InstallApp-NT.bat.in、UninstallApp-NT.bat.in文件

          拷贝到test的bin目录中,并分别改名为App.bat、InstallApp-NT.bat、UninstallApp-NT.bat。

     5)将其lib目录下的Wrapper.DLL、wrapper.jar拷贝到test的lib目录中。并且将项目的jar和所用到的jar都拷贝到该目录(包括你自己的java程序jar)。

     6)将其src/conf目录下的wrapper.conf.in拷贝到workapp的conf目录中,并命名为wrapper.conf。

 2. 修改wrapper.conf文件

    主要修改下面几项:

    (1)JVM位置:

    wrapper.java.command=C:\jdk1.5.0_07\bin\java或者 wrapper.java.command=%JAVA_HOME%/bin/java(需要在系统的环境变量里配置JAVA_HOME)
   (2)MAIN CLASS 此处决定了使用Java Service Wrapper的方式
    wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
   (3)你的Java程序所需的jar包必须全部在此标明,注意路径准确:
     wrapper.java.classpath.1=../lib/wrapper-0.0.1-SNAPSHOT.jar
     wrapper.java.classpath.2=../lib/wrapper.jar
     wrapper.java.classpath.3=../lib/sqljdbc4.jar

      ……

    (4)你的Wrapper.DLL或wrapper.jar所在的目录
      wrapper.java.library.path.1=../lib
    (5)你的Java应用程序的运行类(主类)
      wrapper.app.parameter.1= window_wrapper.wrapper.App
    (6)注册为服务的名称和显示名,你可以随意进行设置
      wrapper.name=testwrapper
      wrapper.displayname= Test Wrapper Sample Application
    (7)服务描述信息
      wrapper.description= Test Wrapper Sample ApplicationDescription

    (8)服务的启动类型

      # Mode in which the service isinstalled.  AUTO_START, DELAY_START or DEMAND_START
      wrapper.ntservice.starttype=AUTO_START

3. 修改好了以后,运行MyApp.bat即可运行你的java程序,这里可以测试配置的是否正确,如果可以运行,证明配置ok。

4. 运行InstallApp-NT.bat可以进行服务的注册,UninstallApp-NT.bat为注销服务。

 5. 运行完注册服务InstallApp-NT.bat可以在 控制面板-管理程序-服务中看到你注册的服务名称。(如:Test Wrapper Sample Application)


三.Maven自动打包法

1.     pom.xml文件引入相关的Maven  plugins

  1. <span style="font-size:14px;"><build>  
  2.         <resources>  
  3.             <resource>  
  4.                 <directory>${project.basedir}/src/main/resources</directory>  
  5.             </resource>  
  6.             <resource>  
  7.                 <targetPath>../mywrapper/etc</targetPath>  
  8.                 <directory>${project.basedir}/src/main/resources/</directory>  
  9.                 <includes>  
  10.                     <include>**/*.xls</include>  
  11.                     <include>**/*.xml</include>  
  12.                     <include>**/*.properties</include>  
  13.                     <include>**/*.bat</include>  
  14.                 </includes>  
  15.             </resource>  
  16.                         <resource>  
  17.                 <targetPath>../wrapper/jsw/mywrapper/bin</targetPath>  
  18.                 <directory>${project.basedir}/src/main/resources/</directory>  
  19.                 <includes>  
  20.                     <include>**/*.bat</include>  
  21.                 </includes>  
  22.             </resource>  
  23.             <resource>  
  24.                 <targetPath>../wrapper/jsw/mywrapper/etc</targetPath>  
  25.                 <directory>${project.basedir}/src/main/resources/</directory>  
  26.                 <includes>  
  27.                     <include>**/*.xls</include>  
  28.                     <include>**/*.xml</include>  
  29.                     <include>**/*.properties</include>  
  30.                     <include>**/*.bat</include>  
  31.                 </includes>  
  32.             </resource>  
  33.         </resources>  
  34.    
  35.         <testResources>  
  36.             <testResource>  
  37.                 <directory>${project.basedir}/src/test/java</directory>  
  38.             </testResource>  
  39.             <testResource>  
  40.                 <directory>${project.basedir}/src/main/resources</directory>  
  41.             </testResource>  
  42.         </testResources>  
  43.    
  44.         <plugins>  
  45.             <plugin>  
  46.                 <groupId>org.apache.maven.plugins</groupId>  
  47.                 <artifactId>maven-compiler-plugin</artifactId>  
  48.                 <version>3.1</version>  
  49.                 <configuration>  
  50.                     <source>1.6</source>  
  51.                     <target>1.6</target>  
  52.                 </configuration>  
  53.             </plugin>  
  54.             <plugin>  
  55.                 <groupId>org.codehaus.mojo</groupId>  
  56.                 <artifactId>appassembler-maven-plugin</artifactId>  
  57.                 <version>1.3</version>  
  58.                 <executions>  
  59.                     <execution>  
  60.                         <id>generate start scripts</id>  
  61.                         <phase>package</phase>  
  62.                         <goals>  
  63.                             <goal>assemble</goal>  
  64.                         </goals>  
  65.                         <configuration>  
  66.                             <repositoryLayout>flat</repositoryLayout>  
  67.                             <programs>  
  68.                                 <program>  
  69.                                 <!-- 通过批处理 运行Main类 -->  
  70.                                     <mainClass>  
  71.                                         window_wrapper.wrapper.App  
  72.                                     </mainClass>  
  73.                                     <name>init_app</name>  
  74.                                 </program>  
  75.                             </programs>  
  76.                         </configuration>  
  77.                     </execution>  
  78.                     <execution>  
  79.                         <phase>package</phase>  
  80.                         <goals>  
  81.                             <goal>generate-daemons</goal>  
  82.                         </goals>  
  83.                         <configuration>  
  84.                             <repositoryLayout>flat</repositoryLayout>  
  85.                             <target>${project.build.directory}/wrapper</target>  
  86.                             <includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>  
  87.                             <useDaemonIdAsWrapperConfName>true</useDaemonIdAsWrapperConfName>  
  88.                             <daemons>  
  89.                                 <daemon>  
  90.                                     <id>mywrapper</id>  
  91.                                     <!-- 打包成windows服务的Main类 -->  
  92.                                     <mainClass>window_wrapper.wrapper.App</mainClass>  
  93.                                     <!-- <commandLineArguments<commandLineArgument>start</commandLineArgument>   
  94.                                         </commandLineArguments> -->  
  95.                                     <!-- <jvmSettings<initialMemorySize>20M</initialMemorySize<maxMemorySize>200M</maxMemorySize>   
  96.                                         <maxStackSize>128M</maxStackSize</jvmSettings> -->  
  97.                                     <platforms>  
  98.                                         <platform>jsw</platform>  
  99.                                     </platforms>  
  100.                                     <generatorConfigurations>  
  101.                                         <generatorConfiguration>  
  102.                                             <generator>jsw</generator>  
  103.                                             <includes>  
  104.                                                 <include>linux-x86-32</include>  
  105.                                                 <include>linux-x86-64</include>  
  106.                                                 <include>macosx-universal-32</include>  
  107.                                                 <include>macosx-universal-64</include>  
  108.                                                 <include>windows-x86-32</include>  
  109.                                                 <include>windows-x86-64</include>  
  110.                                             </includes>  
  111.                                             <configuration>  
  112.                                                 <property>  
  113.                                                     <name>configuration.directory.in.classpath.first</name>  
  114.                                                     <value>etc</value>  
  115.                                                 </property>  
  116.                                             </configuration>  
  117.                                         </generatorConfiguration>  
  118.                                     </generatorConfigurations>  
  119.                                 </daemon>  
  120.                             </daemons>  
  121.                         </configuration>  
  122.                     </execution>  
  123.                 </executions>  
  124.             </plugin>  
  125.             <plugin>  
  126.                 <groupId>org.codehaus.mojo</groupId>  
  127.                 <artifactId>cobertura-maven-plugin</artifactId>  
  128.                 <version>2.5.2</version>  
  129.                 <configuration>  
  130.                     <formats>  
  131.                         <format>html</format>  
  132.                         <format>xml</format>  
  133.                     </formats>  
  134.                 </configuration>  
  135.             </plugin>  
  136.         </plugins>  
  137.     </build>  
  138.     <reporting>  
  139.         <plugins>  
  140.             <plugin>  
  141.                 <groupId>org.codehaus.mojo</groupId>  
  142.                 <artifactId>cobertura-maven-plugin</artifactId>  
  143.                 <version>2.5.2</version>  
  144.             </plugin>  
  145.         </plugins>  
  146. </reporting>  
  147. </span>  


2.      解压wrapper-windows-x86-32-3.5.20.zip, src/bin目录下的InstallApp-NT.bat.in、UninstallApp-NT.bat.in文件,修改成InstallApp-Nt.bat和Uninstall-Nt.bat

3.      ..\target\appassembler\bin目录下的init_app.bat批处理文件 运行打包的main方法

4.      将2中的InstallApp-Nt.bat和Uninstall-Nt.bat复制到..\target\wrapper\jsw\mywrapper\bin目录,或者在程序resources目录中放入InstallApp-Nt.bat和Uninstall-Nt.bat。

同时将installApp-Nt.bat和Unistall-Nt.bat中的_WRAPPER_CONF_DEFAULT值替换成..\target\wrapper\jsw\mywrapper\bin目录mywrapper.bat中 _WRAPPER_CONF值


如果都为Maven项目,第二种比第一种简单,不需要手动的去添加生成架包和引用架包,maven会帮我们自动的打入。


示例项目下载地址:http://download.csdn.net/detail/a123demi/8390851





















以上是关于详解Maven项目利用java service wrapper将Java程序生成Windows服务的主要内容,如果未能解决你的问题,请参考以下文章

利用intellijidea创建maven多模块项目

Maven聚合开发实例详解---5555字

Java项目中常用Maven插件详解

Java Maven安装及配置,利用Maven创建项目

maven 详解

XML注入详解java中的xxe利用