Maven 專案生成jar執行時提示“沒有主清單屬性”
阿新 • • 發佈:2020-03-16
新建了一個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>
到此這篇關於Maven 專案生成jar執行時提示“沒有主清單屬性”的文章就介紹到這了,更多相關Maven jar沒有主清單屬性內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!