Java 如何在intellij IDEA中创建spring maven项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20229334/
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
How to create spring maven project in intellij IDEA
提问by user3038475
I'm trying to create spring + maven project in IntelliJ IDEA. I've read these official wiki, but when I finish steps, there is no maven in my project. I've tried to add it with my hands, but it seems, that I have hands not so good enough :(, because maven dependencies, lib folder and classes, that I want to use live their own lives (dependencies aren't in lib folder and I try to type classes from dependencies, there is no auto-complete). Does anyone has step-by-step guide or link?
我正在尝试在 IntelliJ IDEA 中创建 spring + maven 项目。我已经阅读了这些官方 wiki,但是当我完成步骤时,我的项目中没有 maven。我试图用我的手添加它,但似乎我的手不够好:(,因为 maven 依赖项、lib 文件夹和类,我想使用它们自己的生活(依赖项不在lib 文件夹,我尝试从依赖项键入类,没有自动完成。有人有分步指南或链接吗?
回答by Menuka Ishan
I think this is what you looking for?
我想这就是你要找的?
Create new project File > New > Project
- Click Maven on the left hand side of the new project dialog
- Check Create from archetype
- Click the Add Archetype button
- Set Group Id to pl.codeleak
- Set Artifact Id to spring-mvc-quickstart
- Set Version to 1.0.0
- Set Repository to http://kolorobot.github.io/spring-mvc-quickstart-archetype
- Click next and create the project
创建新项目文件 > 新建 > 项目
- 单击新建项目对话框左侧的 Maven
- 检查从原型创建
- 单击添加原型按钮
- 将组 ID 设置为 pl.codeleak
- 将工件 ID 设置为 spring-mvc-quickstart
- 将版本设置为 1.0.0
- 将 Repository 设置为http://kolorobot.github.io/spring-mvc-quickstart-archetype
- 单击下一步并创建项目
Refer below link for more.
有关更多信息,请参阅以下链接。
https://github.com/kolorobot/spring-mvc-quickstart-archetype
https://github.com/kolorobot/spring-mvc-quickstart-archetype
ButI would refer the classic way. Generate the maven project using arch-type and import the maven project to the Intellij Then add the spring maven dependencies to the pom.xml. It's clear
但我会参考经典的方式。使用arch-type生成maven项目,将maven项目导入Intellij,然后在pom.xml中添加spring maven依赖。很明显
回答by Dil
First create a spring project by going to File->New->Project and select spring. It will create a spring project. Then to add maven support, you can right click on the project and select "Add Framework Support". It will give you a pop up window, and from that select "maven".
首先通过转到 File->New->Project 并选择 spring 来创建一个 spring 项目。它将创建一个 spring 项目。然后要添加 maven 支持,您可以右键单击该项目并选择“添加框架支持”。它会给你一个弹出窗口,然后从中选择“maven”。
回答by jhyot
Go to https://start.spring.ioand let the Initializr generate a Maven project for you with the desired dependencies.
转到https://start.spring.io并让 Initializr 为您生成具有所需依赖项的 Maven 项目。
You will get a zip file which you can then unpack in your dev folder.
您将获得一个 zip 文件,然后您可以将其解压到您的 dev 文件夹中。
Then open Intellij and select File | New | Project from Existing Sources. (Or Import Project from the welcome screen).
然后打开 Intellij 并选择 File | 新 | 来自现有来源的项目。(或从欢迎屏幕导入项目)。
Select your unzipped folder, and then follow the wizard, selecting Maven when you are prompted to.
选择解压后的文件夹,然后按照向导操作,在出现提示时选择 Maven。
See here: https://www.jetbrains.com/help/idea/2016.2/importing-project-from-maven-model.html
请参阅此处:https: //www.jetbrains.com/help/idea/2016.2/importing-project-from-maven-model.html
回答by Witold Kaczurba
If you want to create Spring-boot with Maven: Most use Spring Initilzr.
- Use either website: https://start.spring.io/- you will get packaged stuff - best way
- Alternatively In IntelliJ Ultimate Edition (personally best tool): File > New > Project... > Spring Initilzr
- Finally you can create Maven on your own + add POM.xml with spring-boot dependency
If you want to create Spring non-boot with Mavenfrom scratch:
- Just simply File > New > Project... > Maven ... [any archetype]
- Edit pom.xml so it has SPring dependencies in it:
如果你想用 Maven创建Spring-boot:大多数使用 Spring Initilzr。
- 使用任一网站:https: //start.spring.io/- 你会得到打包好的东西 - 最好的方法
- 或者在 IntelliJ Ultimate Edition(个人最佳工具)中:文件 > 新建 > 项目... > Spring Initilzr
- 最后,您可以自己创建 Maven + 添加具有 spring-boot 依赖项的 POM.xml
如果要从头开始使用 Maven创建Spring 非引导:
- 只需简单的 File > New > Project... > Maven ... [任何原型]
- 编辑 pom.xml 使其具有 SPring 依赖项:
In this case (the harder way)- for Spring Testing start from POM.xml:
[ entire code + whole intelliJ dump here: https://github.com/wkaczurba/stackoverflow/tree/springmaveninintellij]
在这种情况下(更难的方法)- Spring 测试从 POM.xml 开始:
[整个代码 + 整个 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>
` Then you can create your first test, e.g.:
` 然后您可以创建您的第一个测试,例如:
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());
}
}
And your config + interface + bean:
还有你的配置 + 接口 + 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.
}
Interface:
界面:
package com.stackoverflow;
import org.springframework.stereotype.Component;
@Component
public interface YourInterface {
public boolean func();
}
And Implementation:
和实施:
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;
}
}
To run:
跑步:
- Run:
mvn clean test
- Or create in IntelliJ configuration for running the JUnit test and run.
- 跑:
mvn clean test
- 或者在 IntelliJ 配置中创建用于运行 JUnit 测试和运行。