详解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

<span style="font-size:14px;"><build>
		<resources>
			<resource>
				<directory>${project.basedir}/src/main/resources</directory>
			</resource>
			<resource>
				<targetPath>../mywrapper/etc</targetPath>
				<directory>${project.basedir}/src/main/resources/</directory>
				<includes>
					<include>**/*.xls</include>
					<include>**/*.xml</include>
					<include>**/*.properties</include>
					<include>**/*.bat</include>
				</includes>
			</resource>
						<resource>
				<targetPath>../wrapper/jsw/mywrapper/bin</targetPath>
				<directory>${project.basedir}/src/main/resources/</directory>
				<includes>
					<include>**/*.bat</include>
				</includes>
			</resource>
			<resource>
				<targetPath>../wrapper/jsw/mywrapper/etc</targetPath>
				<directory>${project.basedir}/src/main/resources/</directory>
				<includes>
					<include>**/*.xls</include>
					<include>**/*.xml</include>
					<include>**/*.properties</include>
					<include>**/*.bat</include>
				</includes>
			</resource>
		</resources>

		<testResources>
			<testResource>
				<directory>${project.basedir}/src/test/java</directory>
			</testResource>
			<testResource>
				<directory>${project.basedir}/src/main/resources</directory>
			</testResource>
		</testResources>

		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>appassembler-maven-plugin</artifactId>
				<version>1.3</version>
				<executions>
					<execution>
						<id>generate start scripts</id>
						<phase>package</phase>
						<goals>
							<goal>assemble</goal>
						</goals>
						<configuration>
							<repositoryLayout>flat</repositoryLayout>
							<programs>
								<program>
								<!-- 通过批处理 运行Main类 -->
									<mainClass>
										window_wrapper.wrapper.App
									</mainClass>
									<name>init_app</name>
								</program>
							</programs>
						</configuration>
					</execution>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>generate-daemons</goal>
						</goals>
						<configuration>
							<repositoryLayout>flat</repositoryLayout>
							<target>${project.build.directory}/wrapper</target>
							<includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
							<useDaemonIdAsWrapperConfName>true</useDaemonIdAsWrapperConfName>
							<daemons>
								<daemon>
									<id>mywrapper</id>
									<!-- 打包成windows服务的Main类 -->
									<mainClass>window_wrapper.wrapper.App</mainClass>
									<!-- <commandLineArguments> <commandLineArgument>start</commandLineArgument>
										</commandLineArguments> -->
									<!-- <jvmSettings> <initialMemorySize>20M</initialMemorySize> <maxMemorySize>200M</maxMemorySize>
										<maxStackSize>128M</maxStackSize> </jvmSettings> -->
									<platforms>
										<platform>jsw</platform>
									</platforms>
									<generatorConfigurations>
										<generatorConfiguration>
											<generator>jsw</generator>
											<includes>
												<include>linux-x86-32</include>
												<include>linux-x86-64</include>
												<include>macosx-universal-32</include>
												<include>macosx-universal-64</include>
												<include>windows-x86-32</include>
												<include>windows-x86-64</include>
											</includes>
											<configuration>
												<property>
													<name>configuration.directory.in.classpath.first</name>
													<value>etc</value>
												</property>
											</configuration>
										</generatorConfiguration>
									</generatorConfigurations>
								</daemon>
							</daemons>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>cobertura-maven-plugin</artifactId>
				<version>2.5.2</version>
				<configuration>
					<formats>
						<format>html</format>
						<format>xml</format>
					</formats>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<reporting>
		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>cobertura-maven-plugin</artifactId>
				<version>2.5.2</version>
			</plugin>
		</plugins>
</reporting>
</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

时间: 2024-08-28 07:20:23

详解Maven项目利用java service wrapper将Java程序生成Windows服务的相关文章

利用JAVA Service Wrapper把JAVA程序做成windows服务

今天复习了一下Java的基础知识,做了一个读取数据入库的程序.由于读取的数据每天都更新,于是就想把程序做成一个服务,每天定时执行.研究了一下,发现有几种方式可以做.下面我主要记录一下JAVA Service Wrapper方式.除此之外,我会把一些基本内容也贴上,以后复习所用. 一.下面是整个程序的功能部分: 1.连接数据库的功能.我写了一个DBConnecter类,是一个单例. public class DBConnecter { private static DBConnecter inst

Java Service Wrapper 发布Java程序为Windows服务

下载Windows版本:http://nchc.dl.sourceforge.net/sourceforge/wrapper/wrapper-windows-x86-32-3.2.3.zip 现在目前只要32位有免费的,64位免费版目前还没有做出来.官网上(社区)那一列是免费的. 1.先把你的项目用MANIFEST.MF打成jar包,例如bb.jar.新建目录,例如:dist,放在D盘下面.把bb.jar放到dist目录下. 2.在dist目录下新建conf,lib,log三个文件夹.conf是

使用java service wrapper将java程序注册为windows服务

转载自 http://blog.csdn.net/coolcoffee168/article/details/9980009 1. 下载java service wrapper 网址:http://sourceforge.net/projects/wrapper/ 或者 http://wrapper.tanukisoftware.com/doc/english/download.jsp 我下载的版本是wrapper-windows-x86-32-3.5.20.zip. 2. 配置过程 1)首先确

使用Java Service Wrapper将java程序作为linux服务并且开机自动启动

以java应用程序为例,打包为tianlong.jar,程序入口为tianlong.QueueTest. 下面在linux的/opt下建立tianlong目录,复制tianlong.jar到/opt/tianlong/lib目录下. 1.下载Java Service Wrapper(目前版本wrapper-linux-x86-32-3.3.2), 解压. http://wrapper.tanukisoftware.org/ 复制src/bin/sh.script.in到/opt/tianlong

Java Service Wrapper配置详解

1 #encoding=UTF-8 2 # Configuration files must begin with a line specifying the encoding 3 # of the the file. 4 5 #******************************************************************** 6 # Wrapper License Properties (Ignored by Community Edition) 7 #*

Java Service Wrapper

项目需要把java的application打成jar包单独跑,在linux环境下 存在问题:部署上去之后,java -jar xxx.jar之后 ,因为用ssh进去的,所以这个窗口关掉之后,他的这个进程也就关掉了,这个是不能接收的 目标:开机自动启动,不用人为干预,是守护进程.普遍做法是写shell脚本,网上查了一些,推荐用Java Service Wrapper 优点:各个系统大部分都支持,可以做成服务,可以开机启动 下面说结合自身和网络说一下: 参考文章地址:http://blog.csdn

Java Service Wrapper将jar包安装成Windows服务

刚接触java,第一次使用Java开发windows服务,也是刚不久看了SSM框架 简直也是一头雾水,不过只要用心理解,其实很简单,下面有详细的步骤,包学包会 在windows上运行jar包,需要在工作目录下使用命令行运行jar包,这样会出现一个命令行窗口 而且这个命令行窗口有可能会不小心被关闭,且服务器启动后需要人为去点击开启 对于有些服务性的程序来说,我们需要将其部署为windows服务,在系统启动的时候自动启动 后来研究了两种方式: 1.使用 JavaService.exe 安装jar包

Java Service Wrapper 使用(windows)

1       简介 最近项目中需要做一个Windows系统服务,记录一下使用过程. Java Service Wrapper 可以将Java程序包装成系统服务,这样就可以随着系统的运行而自动运行.Java Service Wrapper分为Professional.Standard.Community三个版本,并支持32位和64位的系统,其中Community版本为免费的社区版本. 2       配置过程 (1)安装java运行环境 (2)java程序打包成jar包 (3)在硬盘上创建文件夹

Java Service Wrapper简介与使用

Wrapper可以将java application 编程一个系统服务器,非常好用! 之前有做过整理,但是公司上不了博客园,所以这边转下下做下记录! 1. 下载java service wrapper 网址:http://sourceforge.net/projects/wrapper/ 或者 http://wrapper.tanukisoftware.com/doc/english/download.jsp 我下载的版本是wrapper-windows-x86-32-3.5.20.zip. 2