mavan 打包时候指定入口文件

333 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。​

一,背景

相信很多小伙伴都遇见过这个错误 ''没有主清单属性''。下面我们通过一个例子来重现这个问题,并且解决这个问题。

看着面这个例子,只有一个main入口 PassWordWin,里面是一个gui程序。

​编辑

pom 文件

<?xml version="1.0" encoding="UTF-8"?>

<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.zgxinlei</groupId>
    <artifactId>java-tool</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

</project>

用maven默认的命令打包

​编辑

打包完成,并重命名为fail

​编辑

尝试启动和我们预料的一样。

​编辑

二,传统解决方案(有点low)

解压jar包,进入META-INF目录

​编辑

看到如下,打开MANIFEST.MF,jar启动额时候回来这个文件夹内找Main-Class属性,但是这里没有。

​编辑

​编辑

我们给他加上,并且光标要移动到下一行才会生效。

​编辑

再次启动,已经正常了。

三,最终解决方案

我们通过上面已经明白了问题出现的根源,那么能不能从打包的时候开始就杜绝这个问题呢?

答案是肯定的。

添加打包插件,指定入口类

方案1,maven-jar-plugin插件

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <classesDirectory>target/classes/</classesDirectory>
                    <archive>
                        <manifest>
                            <mainClass>com.zgxinlei.tool.PassWordWin</mainClass>
                            <useUniqueVersions>false</useUniqueVersions>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>.</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

解决方案2,maven-shade-plugin插件

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                    <shadedArtifactAttached>true</shadedArtifactAttached>
                    <shadedClassifierName>shaded</shadedClassifierName>
                </configuration>
            </plugin>

当然还有其他插件这里只演示这两种。

spring boot 打包出现这个问题是解决方案参考

SpringBoot Maven 常见问题_浮生夢的博客-CSDN博客

再次尝试,问题已经解决。