首页 > 代码库 > 详解 集成Maven Spring Mybatis项目打包生成Bat文件

详解 集成Maven Spring Mybatis项目打包生成Bat文件

在项目中有时候需要将Maven项目打包生成bat文件,单独运行。本文将详解利用maven-assembly-plugin插件实现bat文件打包。

1.首先看一下项目结构


2.配置pom.xml文件,在节点build中加入以下配置内容

<span style="font-family:KaiTi_GB2312;font-size:18px;"><resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<excludes>
					<exclude>src/main/resources/bin</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>src/main/resources/bin</directory>
				<filtering>true</filtering>
				<targetPath>bin</targetPath>
			</resource>
		</resources>

		<plugins>
			<plugin>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.uih.anyi.mnis.drugbag.service.main.TestMain</mainClass>
							<addClasspath>true</addClasspath>
							<classpathPrefix>${lib.output.dir}</classpathPrefix>
							<useUniqueVersions>false</useUniqueVersions>
						</manifest>
						<manifestEntries>
							<Class-Path>.</Class-Path>
						</manifestEntries>
					</archive>
					<excludes>
						<exclude>properties/</exclude>
					</excludes>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<descriptors>
						<descriptor>assembly.xml</descriptor>
					</descriptors>
				</configuration>
			</plugin>
		</plugins>
	</build></span>
2.assembly.xml文件内容

<span style="font-family:KaiTi_GB2312;font-size:18px;"><assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
	<id>distribution</id>
	<formats>
		<format>zip</format>
	</formats>
	<files>
		<file>
			<source>${project.build.directory}/${project.build.finalName}.jar</source>
			<outputDirectory>/</outputDirectory>
		</file>
	</files>
	<fileSets>
		<fileSet>
			<directory>${project.basedir}</directory>
			<outputDirectory>/</outputDirectory>
			<includes>
				<include>README*</include>
				<include>LICENSE*</include>
				<include>NOTICE*</include>
			</includes>
		</fileSet>
		<!-- 对应src/java/resources/相关文件 -->
		<fileSet>
			<directory>${project.build.outputDirectory}/bin/</directory>
			<outputDirectory>/bin/</outputDirectory>
		</fileSet>
		<fileSet>
			<directory>${project.build.outputDirectory}/properties/</directory>
			<outputDirectory>/properties/</outputDirectory>
		</fileSet>
		<fileSet>
			<directory>${project.build.outputDirectory}/db/</directory>
			<outputDirectory>/db/</outputDirectory>
		</fileSet>
	</fileSets>
	<dependencySets>
		<dependencySet>
			<outputDirectory>/${lib.output.dir}</outputDirectory>
			<scope>runtime</scope>
		</dependencySet>
	</dependencySets>
</assembly></span>

3.编写bat批处理文件

<span style="font-family:KaiTi_GB2312;font-size:18px;">@title Lachesis Synchronous_DrugBagRec_Datas %1

@echo %~dp0
echo off
rem Guess MNIS_HOME if not defined
set "CURRENT_DIR=%cd%"
if not "%MNIS_HOME%" == "" goto gotHome
set "MNIS_HOME=%CURRENT_DIR%"
if exist "%MNIS_HOME%\bin\run_midware.bat" goto okHome
set "MNIS_HOME=%~dp0.."
cd "%CURRENT_DIR%"

:gotHome
if exist "%MNIS_HOME%\bin\run_midware.bat" goto okHome
echo The MNIS_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end

:okHome
if "%1" == "" goto startup

:startup
@set classpath=%JAVA_HOME%\lib;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;%MNIS_HOME%\lib;
java -Xms32m -Xmx128m -XX:MaxNewSize=32m -XX:MaxPermSize=128m  -jar %MNIS_HOME%\${project.build.finalName}.${project.packaging} %1 &

echo on
:end
pause</span>

4.选择项目,执行Maven Install

在target下会生成zip包,解压后,执行bin下的bat文件,就可以运行了。



详解 集成Maven Spring Mybatis项目打包生成Bat文件