Java 我可以将 Maven 项目转换为 SpringBoot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29360631/
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 07:50:32  来源:igfitidea点击:

Can I convert Maven Project to SpringBoot

javaspringeclipsemavenspring-boot

提问by SSI

I have a Maven Project in Eclipse and now I need to add database connectivity. My textbook did all json tutorials in Maven. Now in this chapter on JDBC they are using SpringBoot.

我在 Eclipse 中有一个 Maven 项目,现在我需要添加数据库连接。我的教科书在 Maven 中做了所有 json 教程。现在在有关 JDBC 的这一章中,他们使用的是 SpringBoot。

Can I convert the project to SpringBoot? Or start a SpringBoot and import my previous Maven classes.

我可以将项目转换为 SpringBoot 吗?或者启动一个 SpringBoot 并导入我以前的 Maven 类。

回答by Simulant

Here is describedhow to use maven for a SpringBoot Project.

这里介绍一个SpringBoot项目如何使用maven。

You will need to modify your existing pom.xmlto add something like this to make it a SpringBoot Project:

您需要修改现有内容pom.xml以添加如下内容以使其成为 SpringBoot 项目:

<!-- Inherit defaults from Spring Boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.2.RELEASE</version>
</parent>

<!-- Add typical dependencies for a web application -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<!-- Package as an executable jar -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

回答by NeeruKSingh

Convert Maven Project to SpringBoot

将 Maven 项目转换为 SpringBoot

  • Ist option :Spring Boot Using parent POM
  • Ist 选项:使用父 POM 的 Spring Boot

Add following dependencies in pom.xml file**:-

在 pom.xml 文件中添加以下依赖项**:-

1.) Inheriting the starter parent(spring-boot-starter-parent): spring-boot-starter-parent is a special starter that provides useful Maven defaults.

1.)继承 starter parent(spring-boot-starter-parent): spring-boot-starter-parent 是一个特殊的 starter,提供有用的 Maven 默认值。

2.) spring-boot-starter-web:- Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.

2.) spring-boot-starter-web:- 用于使用 Spring MVC 构建 Web(包括 RESTful)应用程序的启动器。使用 Tomcat 作为默认的嵌入式容器。

3.) spring-boot-maven-plugin:- Spring Boot includes a Maven plugin that can package the project as an executable jar.

3.) spring-boot-maven-plugin:- Spring Boot 包含一个 Maven 插件,可以将项目打包为可执行 jar。

here is pom.xml :-

这是 pom.xml :-

<!-- Inherit defaults from Spring Boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.7.RELEASE</version>
</parent>

<!-- Add typical dependencies for a web application -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<!-- Package as an executable jar -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<!-- Inherit defaults from Spring Boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.7.RELEASE</version>
</parent>

<!-- Add typical dependencies for a web application -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<!-- Package as an executable jar -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

  • 2nd option:Using Spring Boot without the Parent POM
  • 第二个选项:在没有父 POM 的情况下使用 Spring Boot

Not everyone likes inheriting from the spring-boot-starter-parent POM. You may have your own corporate standard parent that you need to use or you may prefer to explicitly declare all your Maven configuration.

不是每个人都喜欢从 spring-boot-starter-parent POM 继承。您可能有自己需要使用的公司标准父级,或者您可能更喜欢显式声明所有 Maven 配置。

If you do not want to use the spring-boot-starter-parent, you can still keep the benefit of the dependency management (but not the plugin management) by using a scope=import dependency, as follows:

如果您不想使用spring-boot-starter-parent,您仍然可以通过使用scope=import dependency保持依赖管理(但不是插件管理)的好处,如下所示:

< dependency> < !-- Import dependency management from Spring Boot -->
< groupId>org.springframework.boot< /groupId>
< artifactId>spring-boot-dependencies< /artifactId>
< version>2.0.0.BUILD-SNAPSHOT< /version> < type>pom< /type>
< scope>import< /scope> < /dependency>

<dependency> <!-- 从Spring Boot导入依赖管理-->
<groupId>org.springframework.boot< /groupId>
<artifactId>spring-boot-dependencies< /artifactId>
<version>2.0.0.BUILD-SNAPSHOT </version> <type>pom< /type>
<scope>import< /scope> </dependency>

Fore more details find here in Spring Boot Docs:- https://docs.spring.io/spring-boot/docs/1.4.7.RELEASE/reference/htmlsingle/#getting-started-maven-installation

有关更多详细信息,请参阅 Spring Boot 文档:- https://docs.spring.io/spring-boot/docs/1.4.7.RELEASE/reference/htmlsingle/#getting-started-maven-installation

回答by Praneeth

1) Add Spring boot starter Parent and Web in Pom.xmlfile

1) 在Pom.xml文件中添加 Spring boot starter Parent 和 Web

2) Add @SpringBootApplication in the mainclass

2)在类中添加@SpringBootApplication

3) Add SpringApplication.run(App.class, args); in main method.

3) 添加 SpringApplication.run(App.class, args); 在主要方法中。

回答by Shweta Shaw

Yes you can use Springboot with Maven as dependency management. You will have to add spring boot dependencies

是的,您可以将 Springboot 与 Maven 一起用作依赖项管理。您将不得不添加 spring boot 依赖项

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

Also add @SpringBootApplication and @EnableAutoConfiguration in the main class

还要在主类中添加@SpringBootApplication 和@EnableAutoConfiguration

回答by chennakesava

I will face this question in two more interviews so it can help full your self also. Converting the standard alone application to spring boot

我将在另外两次采访中面对这个问题,这样它也可以帮助你充实自己。 将标准的单独应用程序转换为 Spring Boot

Simply right-click on your project select "Configure" and choose on "convert to Maven Project" then it will create or application as maven and explicitly add maven dependencies which we want to required.

只需右键单击您的项目,选择“配置”并选择“转换为 Maven 项目”,然后它将创建或应用程序为 Maven 和显式添加我们想要的 maven 依赖项.