Search code examples
javauberjar

Creating an uberjar that has executable class file


I used the maven-shade-plugin for the build, did a mvn-clean-package and was able to execute the code from within the target directory as follows: java -cp uber-weather-lookup-1.0-SNAPSHOT.jar Weather

However, Im a little confused on how would I create an executable jar with everything in it so that this jar file would run on a separate machine - with everything bundled in it The build portion of my pom file is as follows

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <finalName>uber-${artifactId}-${version}</finalName>
        </configuration>
    </plugin>
</plugins>

And then I do the maven package like this: mvn clean package. Running java -jar target/uber-weather-lookup-1.0-SNAPSHOT.jar generates the following error no main manifest attribute, in target/uber-weather-lookup-1.0-SNAPSHOT.jar. I am new to this, so could someone please tell me what I am doing incorrectly?


Solution

  • Specify the main class using the maven-jar-plugin

    You may also need to use the maven-assembly-plugin to package the your dependencies (I’ve always had a hard time figuring out when to use assembly vs shade plugins)

    Take a look at this answer for examples - https://stackoverflow.com/a/15990345