maven 打包问题

385 阅读1分钟

提示找不到hutool的类

maven打包后,运行 java -jar xxx.jar 提示找不到hutool的NumberUtil

NoClassDefFoundError: cn/hutool/core.util/NumberUtil

解决方法,maven中添加如下插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version>
    <configuration>
        <!--第三方的包,统一打入jar包-->
        <createDependencyReducedPom>true</createDependencyReducedPom>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

提示no main manifest attribute

no main manifest attribute, in xxx.jar

解决方法, maven中指定程序入口

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <!--main函数的全路径-->
                <mainClass>com.xxx.xxx.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

总结

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <!--main函数的全路径-->
                        <mainClass>com.xxx.xxx.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <configuration>
                <!--第三方的包,统一打入jar包-->
                <createDependencyReducedPom>true</createDependencyReducedPom>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>