Package and distribute a Scala jar to run on the command line

Add this to pom.xml – (update the mainClass to match your program):

<plugin&gt;
    <artifactId&gt;maven-jar-plugin</artifactId&gt;
    <version&gt;3.1.2</version&gt;
    <configuration&gt;
        <archive&gt;
            <manifest&gt;
                <mainClass&gt;com.myCorp.testJar.App</mainClass&gt;
                <addClasspath&gt;true</addClasspath&gt;
                <classpathLayoutType&gt;custom</classpathLayoutType&gt;
                <customClasspathLayout&gt;lib/${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}
                </customClasspathLayout&gt;
            </manifest&gt;
        </archive&gt;
    </configuration&gt;
</plugin&gt;
<plugin&gt;
    <artifactId&gt;maven-dependency-plugin</artifactId&gt;
    <version&gt;3.1.1</version&gt;
    <configuration&gt;
        <outputDirectory&gt;${project.build.directory}/lib</outputDirectory&gt;
    </configuration&gt;
    <executions&gt;
        <execution&gt;
            <phase&gt;package</phase&gt;
            <goals&gt;
                <goal&gt;copy-dependencies</goal&gt;
            </goals&gt;
        </execution&gt;
    </executions&gt;
</plugin&gt;

In Eclipse right click project and run as Maven build with goal=package

Sample output for a project in D:\06repo Eclipse workspace:
Building jar: D:\06repo\testJar\target\testJar-0.0.1-SNAPSHOT.jar
Creates lib directory under testJar\target directory.

Execute the jar file.

D:\06repo\testJar\target>java -jar testJar-0.0.1-SNAPSHOT.jar

Hello World!

Zip the jar file along with the lib directory.

D:\06repo\testJar\target>f:\7-Zip\7z a -tzip testJar.zip testJar-0.0.1-SNAPSHOT.jar lib
Copy the zip file to a client that has Java installed.

D:\06repo\testJar\target>cp testJar.zip d:/tmp

Extract the zip file in the new client location.
d:\tmp>f:\7-Zip\7z x testJar.zip
Run the jar file in the new client location.

d:\tmp>java -jar testJar-0.0.1-SNAPSHOT.jar

Hello World!

Stack Overflow Article: https://stackoverflow.com/questions/6758258/running-a-maven-scala-project

Advertisements