Java 如何在 Eclipse 中创建一个使用运行配置正确配置的 Spring Boot Starter 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33484363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:43:02  来源:igfitidea点击:

How do I create a Spring Boot Starter Project in Eclipse that is properly configured with a Run Configuration?

javaeclipsemavenspring-tool-suite

提问by Richard Plana

I followed the instructions in Spring Boot Support in Spring Tool Suite 3.6.4and ended up with an Eclipse project which has several issues. The number one issue is that when I right-click on the class containing the "main" entry method and select the "Run As" option, the only entry I get is the fallback "Run Configurations..." method. I neither get the option to run it as a "Spring Boot App" or as a "Java Application".

我按照Spring Tool Suite 3.6.4中的Spring Boot Support 中的说明进行操作,最终得到了一个有几个问题的 Eclipse 项目。第一个问题是,当我右键单击包含“main”条目方法的类并选择“Run As”选项时,我得到的唯一条目是后备“Run Configurations...”方法。我没有选择将它作为“Spring Boot App”或“Java 应用程序”运行。

My question is how do I create the project or what do I do after it's created following the instructions in that site to get that Run As option?

我的问题是如何创建项目或按照该站点中的说明创建项目后我该怎么做才能获得该运行方式选项?

In addition to the above information, I should add the following:

除了上述信息之外,我还应该添加以下信息:

  • I'm using Eclipse 4.4.2 (Luna SR2) and STS 3.7.1
  • I've tried it on both Windows and Linux (Fedora with OpenJDK)
  • Used Java 8 (Sun Hotspot 64-bit 1.8.0_65)
  • When the pom.xml initially gets created, it has an error apparently due to a missing config the m2e wants for which I needed to add the following:

    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-compiler-plugin</artifactId>
                                    <versionRange>[3.1,)</versionRange>
                                    <goals>
                                        <goal>compile</goal>
                                        <goal>testCompile</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
    
  • It doesn't look like the Eclise project is properly configured for a Java application either. There's no configuration for a Java src tree. Below is the .project file:

    <?xml version="1.0" encoding="UTF-8"?>
    <projectDescription>
            <name>demo</name>
            <comment></comment>
            <projects>
            </projects>
            <buildSpec>
                    <buildCommand>
                            <name>org.eclipse.m2e.core.maven2Builder</name>
                            <arguments>
                            </arguments>
                    </buildCommand>
            </buildSpec>
            <natures>
                    <nature>org.eclipse.m2e.core.maven2Nature</nature>
            </natures>
    </projectDescription>
    
  • I can manually run the app as per How to run Spring Boot web application in Eclipse itself?(by executing [Project] --> Run As --> Maven Build... --> Goal: spring-boot:run
  • 我正在使用 Eclipse 4.4.2 (Luna SR2) 和 STS 3.7.1
  • 我在 Windows 和 Linux(Fedora with OpenJDK)上都试过了
  • 使用 Java 8 (Sun Hotspot 64-bit 1.8.0_65)
  • 当 pom.xml 最初被创建时,它有一个错误显然是由于缺少 m2e 想要的配置,我需要添加以下内容:

    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-compiler-plugin</artifactId>
                                    <versionRange>[3.1,)</versionRange>
                                    <goals>
                                        <goal>compile</goal>
                                        <goal>testCompile</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
    
  • 看起来 Eclise 项目也没有为 Java 应用程序正确配置。Java src 树没有配置。下面是 .project 文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <projectDescription>
            <name>demo</name>
            <comment></comment>
            <projects>
            </projects>
            <buildSpec>
                    <buildCommand>
                            <name>org.eclipse.m2e.core.maven2Builder</name>
                            <arguments>
                            </arguments>
                    </buildCommand>
            </buildSpec>
            <natures>
                    <nature>org.eclipse.m2e.core.maven2Nature</nature>
            </natures>
    </projectDescription>
    
  • 我可以按照如何在 Eclipse 中运行 Spring Boot Web 应用程序手动运行该应用程序?(通过执行 [Project] --> Run As --> Maven Build... --> Goal: spring-boot:run

回答by jstuartmilne

To create a new spring-boot project in sts just click new spring-starter project, that will create the project for you. New->Project->Spring->Spring starter project

要在 sts 中创建一个新的 spring-boot 项目,只需单击 new spring-starter 项目,它将为您创建该项目。新建->项目->Spring->Spring 启动项目

You'll run throught the wizard select 'Web' checkbox to have the web app selected this will create an application class like this

您将通过向导选择“Web”复选框来选择 Web 应用程序,这将创建一个这样的应用程序类

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

My auto generated POM

我的自动生成的 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.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

回答by Paulo Merson

You'll need to install the Spring Tool Suite plug-in for Eclipse. Then you'll see the "Spring Boot App" run configuration (and other STS features).

您需要为 Eclipse安装Spring Tool Suite 插件。然后您将看到“Spring Boot App”运行配置(以及其他 STS 功能)。

回答by Paulo Merson

After creating a Spring Boot application via the Spring Starter project via the menu need to do additional steps: Go into project properties and enable the Java facet. Then, right-click project and Maven > Update Project.

通过菜单通过 Spring Starter 项目创建 Spring Boot 应用程序后,需要执行其他步骤: 进入项目属性并启用 Java 方面。然后,右键单击项目和 Maven > 更新项目。