如何基于Maven Shade插件创建可跨机器运行的完整可执行UberJar?
How to Create an Executable Uber JAR with Maven Shade Plugin
Got it! The issue here is that your current shaded JAR doesn’t have the Main-Class entry in its manifest, which is why you have to use java -cp instead of the simpler java -jar command. Let’s fix that by updating your maven-shade-plugin configuration to include this critical manifest attribute.
Here’s how to modify your pom.xml’s build section:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.5.1</version> <!-- Use the latest stable version available --> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <!-- This transformer adds the Main-Class to the JAR's manifest --> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>Weather</mainClass> <!-- Replace with full package path if needed, e.g., com.yourpackage.Weather --> </transformer> </transformers> <!-- Optional: Add additional transformers here if you need to handle duplicate resources or services --> </configuration> </execution> </executions> </plugin> </plugins> </build>
Key Notes:
- The
ManifestResourceTransformerinjects theMain-Classentry into the JAR’sMETA-INF/MANIFEST.MFfile—this is what lets Java know which class to run when usingjava -jar. - Double-check that the
<mainClass>value matches your actual main class (include the full package name if your class isn’t in the root package). - Specifying a plugin version ensures you’re using a stable, up-to-date release instead of relying on Maven’s default (which might be outdated).
After Updating:
- Re-run your build command:
mvn clean package - Now you can execute the JAR directly on any machine with Java installed using:
java -jar target/uber-weather-lookup-1.0-SNAPSHOT.jar
Quick Verification:
To confirm the manifest is set correctly, unzip the JAR and check the META-INF/MANIFEST.MF file—you should see a line like:
Main-Class: Weather
That’s all you need to make your shaded JAR fully standalone!
内容的提问来源于stack exchange,提问作者Fat Owl




