如果我有一个带有 Maven 文件夹的项目,其中一个包含应该启动的主类(并且可以从那里启动),当我从头文件夹中选择 Run as
- Java Application
时,我应该如何让 Eclipse 和 Maven 启动那个类?
编辑:
maven-exec-plugin 没有帮助:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>use_annotations.UseAnnotationsLaunch</mainClass>
</configuration>
</execution>
</executions> </plugin>
没有显示任何错误,但头项目无法启动use_annotations.UseAnnotationsLaunch.main。我收到“选择不包含主要类型”
注意,Maven 编译命令正确调用 Maven 模块的编译。是eclipse和maven模块的问题。
最佳答案
如果我没记错的话,Eclipse 和 Maven 使用它们自己的类路径/运行配置,因此您可以使用 Eclipse 的运行配置,也可以设置 maven-exec-plugin在 pom.xml 中并设置运行配置以调用 maven。
如果你的类路径设置正确,使用 Eclipse 内置的“运行”可能会更舒服,但是如果你想在其他机器上编译和运行它,使用 Maven 运行配置将确保你的项目设置正确。
这是我旧项目的设置,有 exec 插件和带有依赖项的 jar(可执行 jar)。请不要介意插件版本...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>deploy</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>my.old.project.package.gui.MainWindow</mainClass>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>my.old.project.package.gui.MainWindow</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
关于java - 如何从项目本身开始启动 Maven 模块中的主类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55822869/