且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

错误:找不到或加载主类...-Eclipse之外的Maven项目

更新时间:2022-06-24 19:29:08

您的主要问题来自pom.xml的build部分,因为 assembly插件不知道您的主要类是什么,您也忘记指出应该运行程序集插件的阶段(请参阅第二个解决方案以解决此问题),第一个解决方案是摆脱程序集插件并使用下面的内容(改编主类的名称).第二种解决方案是在此代码段之后:

Your main issue comes from the build section of your pom.xml, because the assembly plugin does not know what is your main class, and you also forgot to indicate the phase in which the assembly plugin should run (see the 2nd solution to fix this) the first solution is get rid of the assembly plugin and use what is below (adapt the name of the main class). The second solution is after this code snippet:

<build>
  <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <target>1.8</target>
                    <source>1.8</source>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
<!--                Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>org.test.whatever.pomtester.App</mainClass>
                        </manifest>
                    </archive>

                </configuration>
            </plugin>
    </plugins>
  </build>

另一种解决方案是仅使用Assembly插件,如下所示(与之前修改主类的名称一样):

Another solution is to only use the assembly plugin, with something like below (as before adapt the name of the main class):

<build>
        <plugins>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>org.test.whatever.pomtester.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>

                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>