【问题标题】:How to create spring maven project in intellij IDEA [closed]如何在 intellij IDEA 中创建 spring maven 项目 [关闭]
【发布时间】:2013-12-12 07:40:11
【问题描述】:

我正在尝试在 IntelliJ IDEA 中创建 spring + maven 项目。 我已经阅读了these official wiki,但是当我完成步骤时,我的项目中没有 maven。 我试图用我的双手添加它,但似乎我的双手不够好:(,因为 maven 依赖项、lib 文件夹和类,我想使用它们自己的生活(依赖项不在lib 文件夹,我尝试从依赖项中键入类,没有自动完成)。 有人有分步指南或链接吗?

【问题讨论】:

    标签: java spring maven intellij-idea


    【解决方案1】:

    我想这就是你要找的东西?

    创建新项目文件 > 新建 > 项目

    • 点击新建项目对话框左侧的 Maven
    • 检查从原型创建
    • 点击添加原型按钮
    • 将组 ID 设置为 pl.codeleak
    • 将工件 ID 设置为 spring-mvc-quickstart
    • 将版本设置为 1.0.0
    • 将存储库设置为http://kolorobot.github.io/spring-mvc-quickstart-archetype
    • 点击下一步并创建项目

    更多信息请参考以下链接。

    https://github.com/kolorobot/spring-mvc-quickstart-archetype

    但是我会参考经典的方式。使用arch-type生成maven项目,将maven项目导入Intellij,然后在pom.xml中添加spring maven依赖。很清楚

    【讨论】:

      【解决方案2】:

      首先通过转到File->New->Project并选择spring来创建一个spring项目。它将创建一个 spring 项目。然后要添加 maven 支持,您可以右键单击项目并选择“添加框架支持”。它会给你一个弹出窗口,然后从中选择“maven”。

      【讨论】:

        【解决方案3】:

        转到https://start.spring.io,让 Initializr 为您生成一个具有所需依赖项的 Maven 项目。

        您将获得一个 zip 文件,然后您可以将其解压缩到您的 dev 文件夹中。

        然后打开 Intellij 并选择 File |新 |来自现有资源的项目。 (或从欢迎屏幕导入项目)。

        选择解压后的文件夹,然后按照向导进行操作,在提示时选择 Maven。

        请看这里:https://www.jetbrains.com/help/idea/2016.2/importing-project-from-maven-model.html

        【讨论】:

        • 这个start.spring.io 仅适用于基于 Spring Boot 的应用程序,对吗?
        • @MenukaIshan 是的,你是对的。
        • 您指的是哪个开发文件夹?我可以在任何地方提取它吗?
        • @JayDangar 是的,你喜欢的任何地方。
        • 感谢兄弟,这个答案真的帮了我大忙。赞成,应该被接受。
        【解决方案4】:
        1. 如果你想用 Maven 创建 Spring-boot:大多数使用 Spring Initilzr。

          • 使用任一网站:https://start.spring.io/ - 你会得到打包好的东西 - 最好的方法
          • 或者在 IntelliJ Ultimate Edition 中(个人最佳工具):文件 > 新建 > 项目... > Spring Initilzr
          • 最后你可以自己创建 Maven + 添加带有 spring-boot 依赖的 POM.xml
        2. 如果您想从头开始创建 使用 Maven 的 Spring 非启动

          • 只需简单的文件 > 新建 > 项目... > Maven ... [任何原型]
          • 编辑 pom.xml,使其包含 SPring 依赖项:

        在这种情况下(更难的方法)- 对于从 POM.xml 开始的 Spring 测试:
        [ 整个代码 + 整个 IntelliJ 转储在这里:https://github.com/wkaczurba/stackoverflow/tree/springmaveninintellij ]

        <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.stackoverflow</groupId>
          <artifactId>someartifact</artifactId>
          <version>1.0-SNAPSHOT</version>
          <packaging>jar</packaging>
        
          <name>someartifact</name>
          <url>http://maven.apache.org</url>
        
          <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
          </properties>
        
          <dependencies>
            <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-context</artifactId>
              <version>4.3.13.RELEASE</version>
            </dependency>
            <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-test</artifactId>
              <version>4.3.13.RELEASE</version>
            </dependency>
        
            <!-- Test-related stuff -->
            <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.12</version>
              <scope>test</scope>
            </dependency>
          </dependencies>
        </project>
        

        ` 然后你可以创建你的第一个测试,例如:

        package com.stackoverflow;
        
        import org.junit.Test;
        import org.junit.runner.RunWith;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.test.context.ContextConfiguration;
        import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
        import static org.junit.Assert.assertTrue;
        
        @RunWith(SpringJUnit4ClassRunner.class)
        @ContextConfiguration(classes = { AppConfig.class })
        public class WhateverTest {
        
            @Autowired
            YourInterface objectUnderTest;
        
            @Test
            public void test1() {
                assertTrue(objectUnderTest.func());
            }
        }
        

        还有你的配置 + 接口 + bean:

        package com.stackoverflow;
        
        import com.stackoverflow.YourInterface;
        import org.springframework.context.annotation.Bean;
        import org.springframework.context.annotation.ComponentScan;
        import org.springframework.context.annotation.Configuration;
        
        @Configuration
        @ComponentScan(value="com.stackoverflow")
        public class AppConfig {
            // Add whatever is needed.
        }
        

        界面:

        package com.stackoverflow;
        
        import org.springframework.stereotype.Component;
        
        @Component
        public interface YourInterface {
            public boolean func();
        }
        

        及实施:

        package com.stackoverflow;
        
        import org.springframework.stereotype.Component;
        
        @Component
        class YourInterfaceImpl implements YourInterface {
        
            public boolean func() {
                System.out.println("func called..."); // Always use logger in real world...
                return true;
            }
        }
        

        运行:

        1. 运行:mvn clean test
        2. 或在 IntelliJ 配置中创建以运行 JUnit 测试并运行。

        【讨论】:

        • @Configuration(classes={whatever.class }) 不在这一行编译
        • @Kuldeep Yadav - 你必须在那里指定你的配置类。我更新了代码,使其包含配置类,并提供了指向 GITHUB 的链接,以便您知道在哪里查找它。
        猜你喜欢
        • 2015-08-09
        • 1970-01-01
        • 2018-07-15
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 2016-07-12
        • 1970-01-01
        相关资源
        最近更新 更多