problem scenario
IDEA executes
mvn clean package -DskipTest=true
the command or uses tools toMaven菜单
perform packaging operations, and thenjava -jar app.jar
after executing the command, it prompts that there is no main manifest attribute in the jar.
D:\WorkSpace\demo\target>java -jar demo-SNAPSHOT.jar
demo-SNAPSHOT.jar中没有主清单属性
Cause Analysis
This error is usually caused by the generated JAR file missing a main manifest attribute. When running a JAR file using a command, the JVM needs to look for a manifest file
java -jar
containing properties in the JAR file .Main-Class
If this property is not found, this error will occur.
Confirmation analysis
Open the Jar package and view the jar directory in which it was built. You can see that there is a MANIFEST.MF file. This file is the manifest directory to be searched for when the jar is running. The main manifest data is the main class to be run, that is, the class where the main function entry is located.
Normally, there will be one in the manifest file:
Main-Class:cn.ybzy.demo.mainDemo
solution
AddMain-Class
Manually create a file containing
Main-Class
manifest propertiesMANIFEST.MF
and add it to the JAR file
Open the Jar package and MANIFEST.MF
add Main-Class to the file.
Main-Class:cn.ybzy.demo.mainDemo
Use plugins
1. Use maven-jar-plugin
plugins
The plugin is used in the pom.xml file
maven-jar-plugin
and<mainClass>
the properties are specified in it.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.example.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
2. Use maven-shade-plugin
plugins
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.MainClass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>