maven generates executable jar s and runs them using shell scripts

To create maven project and use maven project to generate executable jar, you need to use maven assembly plugin plug-in to complete. The configuration of pom.xml file is as follows

<?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>

After the above configuration is completed, package the project into war, and then execute it with the following command in the cmd command

java -Xms512m -Xmx1024m -jar D:/jar/helloworld-jar-with-dependencies.jar

If there are parameters, you can add your parameters later. The author uploaded the jar package to the linux server and executed it in the way of shell script, which is as follows

#!/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"

Execute the following sh command to run the shell script, and the result is as follows

Hello World...

 exec jar takes 0 seconds

Tags: Maven Apache xml Java

Posted on Sat, 02 May 2020 10:15:02 -0700 by SuisydeKing