1. 程式人生 > >maven 生成可執行jar並使用shell指令碼執行

maven 生成可執行jar並使用shell指令碼執行

建立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