Maven jar没有主清单属性 Maven 项目生成jar运行时提示“没有主清单属性”
肖老板 人气:0新建了一个Maven的项目,mvn compile和mvn package后生成了jar文件,然后直接到target目录下去执行java -jar xxx.jar命令运行jar包时出现了“xxx.jar没有主清单属性”的错误提示。
根据我的经验,肯定是没有找到入口类(即main函数所在的类),在pom.xml里面加上入口类的配置就好了!第一次直接用maven来打包jar,记录一下。
修改pom.xml文件,添加如下配置项即可:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.cloume.project.App</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>
其中com.cloume.project是入口类所在的包名,也是我的groupId和artifactId拼接而成的结果。添加好了后再执行mvn clean package就打包了一个新的jar文件,然后再到target目录下去java -jar xxx.jar运行就可以了!完整的pom.xml文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cloume</groupId> <artifactId>project</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>App</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.cloume.project.App</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>
加载全部内容