默认打包生成的jar是不能直接运行的,因为带有main方法的信息不会添加到mainifest中,需要借助maven-shade-plugin

  1. <project>
  2. ...
  3. <build>
  4. <plugins>
  5. <plugin>
  6. <groupId>org.apache.maven.plugins</groupId>
  7. <artifactId>maven-shade-plugin</artifactId>
  8. <version>2.0</version>
  9. <configuration>
  10. <transformers>
  11. <transformer implementation = "org.apache.maven.plugins.shade.resource.MainifestResourceTransformer">
  12. <mainClass>com.xun.pf.sayHello.HelloWorld</mainClass>
  13. </transformer>
  14. </transformers>
  15. </configuration>
  16. <executions>
  17. <execution>
  18. <phase>package</phase>
  19. <goals>
  20. <goal>shade</goal>
  21. </goals>
  22. </execution>
  23. </executions>
  24. </plugin>
  25. </plugins>
  26. </build>
  27. ...
  28. </project>

com.xun.pf.sayHello.HelloWorld为包含main方法的class就可以使用Java -jar xxx.jar执行了参考:徐晓斌原文地址:https://blog.csdn.net/xun_2008/article/details/16832151
01-16 22:16