创建maven项目,利用maven项目生成可执行jar,需要使用maven-assembly-plugin插件来完成,pom.xml文件配置如下
<?xml version="1.0" encoding="UTF-8"?>
<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.test</groupId>
<artifactId>demo</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<finalName>helloworld</finalName>
<archive>
<manifest>
<mainClass>com.test.HelloWorld</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
按如上配置完成后,将项目打包成war,再cmd命令中使用如下命令执行即可
java -Xms512m -Xmx1024m -jar D:/jar/helloworld-jar-with-dependencies.jar
如果有参数可以再后面追加你的参数,笔者是将jar包上传到了linux服务器,并使用shell脚本的方式来执行,shell脚本如下
#!/bin/bash
start_time=`date +%s`
java -Xms512m -Xmx1024m -jar /root/jar/helloworld-jar-with-dependencies.jar
end_time=`date +%s`
elapse_time=$((${end_time}-${start_time}))
echo -e "\n exec jar takes ${elapse_time} seconds\n"
执行如下sh命令运行shell脚本,运行结果如下
Hello World...
exec jar takes 0 seconds