一、IDEA中
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.familyxiao</groupId>
<artifactId>GetGoodWeb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>GetGoodWeb</name>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 引入thymelaf 则不需要引入web依赖,若不需要thymelaf则需要添加spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier>
</dependency>
<!-- mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- <scope>provided</scope>-->
<!-- provided 表明该包只在编译和测试的时候使用,去除默认的tomcat -->
<!--<scope>provided</scope>-->
<version>1.5.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>javax.servlet</groupId>-->
<!-- <artifactId>javax.servlet-api</artifactId>-->
<!-- <version>3.1.0</version>-->
<!-- </dependency>-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<useUniqueVersions>false</useUniqueVersions>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.familyxiao.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- 要使生成的jar可运行,需要加入此插件 -->
<!-- <plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!-- </plugin>-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.1.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
将springboot打包成的jar文件做成windows服务
首先使用 java -jar *.jar 看一下有没有报错。
1.在idea中用maven将程序打成jar,放到运行的目录中。
2.去github上面下载winsw: https://github.com/kohsuke/winsw/releases
3. 将WinSW.NET4.exe文件复制到java程序所在文件夹中
4.将java程序重命名,去掉名称中的“.”。例如test-1.0.jar ----> test.jar
5.将WinSW.exe重命名为test.exe(和jar同名)
6. 新建一个xml文件,命名为test.xml,写入以下内容(还有一些参数自己去看github说明):
<service>
<id>test</id>
<name> test </name>
<description>This is test service.</description>
<!-- java环境变量 -->
<env name="JAVA_HOME" value="%JAVA_HOME%"/>
<executable>java</executable>
<arguments>-jar "E:\springboot\ test.jar"</arguments>
<!-- 开机启动 -->
<startmode>Automatic</startmode>
<!-- 日志配置 -->
<logpath>%BASE%\log</logpath>
<logmode>rotate</logmode>
</service>
如果没有配置环境变量,直接将三个文件扔到java的bin目录下运行。去掉标签<env name="JAVA_HOME" value="%JAVA_HOME%"/>
7.命令行定位到当前目录,执行:
test.exe install
8. 去windows服务列表中启动程序。
(如果需要更新程序,只需要先将服务停止,再将新文件重命名为test.jar,最后启动服务就行了)