运行java-jar时加载主类清单属性失败
我已经成功地用运行java-jar时加载主类清单属性失败,java,spring,maven,spring-mvc,Java,Spring,Maven,Spring Mvc,我已经成功地用mvn clean package构建了我的Spring MVC项目 现在,我正在尝试使用以下工具运行该服务: mvn clean package && java -jar target/gs-serving-web-content-0.1.0.jar 但我得到了这个错误: 未能从加载主类清单属性 target/gs-service-web-content-0.1.0.jar 我遗漏了什么吗?您遗漏了需要添加清单标记的maven jar插件 您必须在pom.xml
mvn clean package
构建了我的Spring MVC项目
现在,我正在尝试使用以下工具运行该服务:
mvn clean package && java -jar target/gs-serving-web-content-0.1.0.jar
但我得到了这个错误:
未能从加载主类清单属性
target/gs-service-web-content-0.1.0.jar
我遗漏了什么吗?您遗漏了需要添加
清单
标记的maven jar插件 您必须在pom.xml中指定它-这将使您的jar具有所有依赖项的可执行性(替换your.main.class
):
maven汇编插件
带有依赖项的jar
你的主课
包裹
单一的
如果您正在使用Spring Boot,这将解决您的问题:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.2.5.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
org.springframework.boot
您可能缺少Spring Boot Maven插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
org.springframework.boot
springbootmaven插件
对于spring boot,我在META-INF文件夹中创建了一个MANIFEST.MF文件
在我的STS IDE中,我将META-INFO文件夹放在src/main/resources文件夹中
像这样:
MANIFEST.MF文件的内容:
Manifest-Version: 1.0
Implementation-Title: bankim
Implementation-Version: 1.5.6.RELEASE
Archiver-Version: Plexus Archiver
Built-By: Yourname
Implementation-Vendor-Id: com.bankim
Spring-Boot-Version: 1.5.6.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.bankim.BankimApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_131
Implementation-URL: http://projects.spring.io/spring-boot/bankim/
Main-Class: com.example.MainClassApplication
每次提到“bankim”/“bankim”都是指我的项目名称,所以请用您的项目名称替换它
特别注意“开始类”值。它应该包含有main方法的类的“路径”
行:
主类:org.springframework.boot.loader.jarLauncher应该保持原样
****上面的清单是使用“Mradul Pandey”提到的“spring boot maven插件”为我创建的(于2015年9月2日4:50回答)
希望这有助于检查
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
org.springframework.boot
springbootmaven插件
应该在上面
dockerfile maven插件,否则将发生重新打包
这解决了我在清单中没有主属性的问题。我有spring boot maven插件
,但我仍然得到了关于主类的错误
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
很高兴能去 使用springbootmaven插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
或者,可以在pom.xml的plugin部分将main类定义为spring boot maven插件的mainClass元素:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>${start-class}</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
org.springframework.boot
springbootmaven插件
重新包装
弹簧靴
${start class}
java-jar目标/gs-service-web-content-0.1.0.jar
命令假定提供的jar是可执行的。在可执行jar中,main
方法在MANIFEST.MF
文件中定义。如果没有MANIFEST.MF
文件,可以传递一个附加的命令行参数来指定包含main
方法的类
像java-cp-target/gs-serving-web-content-0.1.0.jar com.pkg.subpkg.MainClass这样的命令可以达到这个目的。如果你的插件配置和我的一样(下面的),那么,使用mvn-package-spring-boot:repackage
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
org.springframework.boot
springbootmaven插件
重新包装
快乐学习。您需要在MANIFEST.MF文件中设置主类:
Manifest-Version: 1.0
Implementation-Title: bankim
Implementation-Version: 1.5.6.RELEASE
Archiver-Version: Plexus Archiver
Built-By: Yourname
Implementation-Vendor-Id: com.bankim
Spring-Boot-Version: 1.5.6.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.bankim.BankimApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_131
Implementation-URL: http://projects.spring.io/spring-boot/bankim/
Main-Class: com.example.MainClassApplication
请参阅简单添加上述代码和使用mvn包
对我不起作用。我不得不使用mvn包spring boot:repackage,它创建了独立的应用程序。。。将集成后测试添加到执行中解决了我的问题mvn包spring boot:repackage
解决了我的问题。感谢上面所附的文档。mvn软件包对我有用。我不必做mvn spring boot:重新打包。我在mac os上使用SpringBoot 1.5.9.RELEASE+Java8。如果您有domvnspringboot:repackage,那么就不需要在pom.xml中添加重新打包目标。在pom.xml中添加重新打包目标的要点是,当您调用mvn包时,maven将自动调用spring boot:package。尤其是在大型项目中,开发人员自带了自己的父pom来管理依赖项和控制jar地狱。您不应该手动执行此操作。如果配置正确,SpringBoot插件会为您创建此功能。但令人费解的是,并非每个人都适用相同的解决方案。我不得不将执行部分添加到插件中,例如,它失败了。我没有手动创建它(参见第3步中提到的)。但是是的,不确定是什么原因造成的。已经有一段时间了。。。
Main-Class: com.example.MainClassApplication