第一步:使用maven自带骨架maven-archetype-quickstart生成一个普通的java项目;(eclispe里可以创建)
第二步:在pom文件中增加以下配置:红色部分
<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.mv</groupId>
<artifactId>DemoMV</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependencies>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.wnameless/workbook-accessor -->
<dependency>
<groupId>com.github.wnameless</groupId>
<artifactId>workbook-accessor</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
<configuration>
<!--lib下直接存放jar,没有路径文件夹(如com/apache),如果没有这个选项则放在lib下的jar包会被com/apache类似的多层文件夹包裹起来-->
<repositoryLayout>flat</repositoryLayout>
<configurationDirectory>conf</configurationDirectory>
<configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
<copyConfigurationDirectory>true</copyConfigurationDirectory>
<includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
<!--生成的项目的目录位置,这里的client是项目的名称,你可以根据你的需要自己随便命名-->
<assembleDirectory>${project.build.directory}/client</assembleDirectory>
<!--java启动参数-->
<extraJvmArguments>-Xms128m</extraJvmArguments>
<binFileExtensions>
<unix>.sh</unix>
</binFileExtensions>
<platforms>
<platform>windows</platform>
<platform>unix</platform>
</platforms>
<repositoryName>lib</repositoryName>
<programs>
<program>
<!--指定主类,脚本名。会生成shell/bat两种类型,也可用platforms指定运行平台-->
<mainClass>com.test.App</mainClass>
<!-- 生成的脚本文件的名称,比如start.sh,你也可以根据你的需要命名成其他名字 -->
<name>start</name>
</program>
</programs>
</configuration>
</plugin>
</plugins>
</build>
</project>
第三步:使用mvn clean package appassembler:assemble打包(在eclipse里可以在Run Configurations中的图形界面中配置打包命令,这个自己查,我这里不赘述),打好的包格式如图:
可以看到自动生成了启动脚本文件,被依赖的jar包放在项目的lib下。
第四步:将项目复制到其他地方,windows下使用cmd打开命令行,然后将start文件拖到命令行执行,linux下可以使用nohup ./start.sh &的方式启动,最后的&是表示项目以后台进程的方式启动,否则你关闭命令行,则系统自动退出;
到这里就介绍完了,另外需要注意的是:
1、如果pom文件中增加了新的依赖,或者减少的新的依赖,则需要重新使用第三步的命令打包,否则新加入的jar包无法引入到项目的classpath下,我们打开start.bat或者start.sh文件就可以看到,命令把每个jar包都加入到classpath下。
2、linux和windows下的启动文件虽然类似,但是语法细节严重不同,在修改时一定要注意。