使用 Intellij IDEA 与 Gradle 构建Spring项目
来源:互联网 发布:社会化网络的意义 编辑:程序博客网 时间:2024/06/06 03:01
http://blog.csdn.net/x_iya/article/details/60321572
如上,按照常规的方法,建立项目,导入依赖的jar包,编写代码。难免太麻烦了些。
有了Gradle,它可以帮助我们导入依赖的jar包,测试项目也方便了很多。
分享一个蛮不错的项目编写方案:
(1)根据gradle的项目结构约定(Maven也有自己的约定),在命令行中创建目录即gradle脚本文件。
(2)在gradle中引入idea插件,该插件用来生成IntelliJ IDEA的工程文件,我使用的IntelliJ IDEA而没有使用eclipse。
(3)运行一个命令"gradlew idea",生成一个*.ipr文件
(4)在IntelliJ IDEA中,直接打开该ipr文件。
(5)现在就可以编辑代码了。gradle也为eclipse提供相应的插件
(6)要编译项目,别再IDE里面进行,使用gradle或者Maven来构建
(7)这样一步一步向前迈,你会发现你越来越趋向于持续集成了。
build.gradle
group 'com.xiya'version '1.0-SNAPSHOT'apply plugin: 'java'apply plugin: 'idea'apply plugin: 'application'mainClassName = 'com.xiya.test.Test'//我们编写的是java应用程序,而非web程序sourceCompatibility = 1.8//使用阿里云镜像下载依赖jar包repositories { maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }}dependencies { testCompile group: 'junit', name: 'junit', version: '4.11'//用作测试 compile group: 'org.springframework', name: 'spring-context', version: '4.3.7.RELEASE'//Gradle帮助我们自动导入依赖jar包}//对应于:javac -encoding utf-8 ...tasks.withType(JavaCompile) { options.encoding = "UTF-8"}
几个简单的测试类:
package com.xiya.entity;/** * Created by N3verL4nd on 2017/4/9. */public interface People { void sayHello();}
package com.xiya.entity;import org.springframework.stereotype.Component;/** * Created by N3verL4nd on 2017/4/9. */@Componentpublic class American implements People { @Override public void sayHello() { System.out.println("Hello, I come form America!"); }}
package com.xiya.entity;import org.springframework.stereotype.Component;/** * Created by N3verL4nd on 2017/4/9. */@Componentpublic class Chinese implements People { @Override public void sayHello() { System.out.println("你好,我来自中国!"); }}
package com.xiya.service;import com.xiya.entity.People;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Service;/** * Created by N3verL4nd on 2017/5/17. */@Servicepublic class PeopleManager { @Autowired @Qualifier("chinese")// @Qualifier("american") private People people; public void sayHello() { people.sayHello(); }}Spring配置文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--配置自动扫描的包--> <context:component-scan base-package="com.xiya.entity, com.xiya.service"> <!--<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>--> <!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component"/>--> </context:component-scan></beans>测试:
package com.xiya.test;import com.xiya.service.PeopleManager;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by N3verL4nd on 2017/5/17. */public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans-helloworld.xml"); PeopleManager peopleManager = context.getBean(PeopleManager.class); peopleManager.sayHello(); }}项目结构:
编写完所有代码,其实我们就不依赖于IDE了。
在\Spring目录下执行 gradle build构建项目。
class:存放编译生成的*.class文件。
distributions:最终解压后可以运行的项目。
D:\N3verL4nd\Desktop\Study\Spring\build\distributions\Spring-1.0-SNAPSHOT>tree /f卷 本地磁盘 的文件夹 PATH 列表卷序列号为 00000200 0006:08B0D:.├─bin│ Spring│ Spring.bat│└─lib commons-logging-1.2.jar Spring-1.0-SNAPSHOT.jar spring-aop-4.3.7.RELEASE.jar spring-beans-4.3.7.RELEASE.jar spring-context-4.3.7.RELEASE.jar spring-core-4.3.7.RELEASE.jar spring-expression-4.3.7.RELEASE.jar运行脚本,输出结果。
当然也可以在
\spring目录,执行 gradle run 来运行项目。
Demo:https://github.com/N3verL4nd/Spring.git
阅读全文
0 0
- 使用 Intellij IDEA 与 Gradle 构建Spring项目
- 使用IntelliJ IDEA和gradle 构建spring 项目
- IntelliJ IDEA 与 Gradle + Spring项目的初次尝试
- IntelliJ IDEA 与 Gradle + Spring项目的初次尝试
- IntelliJ IDEA使用 - eclipse导入gradle项目
- intellij idea 使用maven构建SSM项目
- Idea上使用Gradle构建Spark项目
- 使用IDEA和Gradle构建Vertx项目
- intellij IDEA 搭建基于 Gradle 的 Spring 项目
- Spring Boot学习(1)—— mac上Intellij使用Gradle构建Spring Boot项目,并使用Groovy
- 使用gradle构建Spring boot项目
- Intellij Idea 使用Maven构建Java Web项目 + Tomcat集成
- SpringBoot(1) 使用Intellij IDEA社区版构建SpringBoot项目
- 使用IDEA创建基于Gradle构建的JavaWeb项目
- 使用IDEA创建基于Gradle构建的JavaWeb项目 -- 续
- 《intellij idea学习经历》-01- 使用IntelliJ IDEA 构建Maven的web项目
- idea 构建spring 项目
- Intellij IDEA + Gradle 搭建Spring源代码环境
- WPS国际版添加中文包
- static变量与实例变量 java
- C#中ArrayList.ToArray()用法
- 909422229_在windows上安装redis并设置密码,IP绑定【提供安装包】
- asp.net response.ContentType 下载文件的四种方法
- 使用 Intellij IDEA 与 Gradle 构建Spring项目
- xfire客户端调用服务端报错 XFireRuntimeException: Could not invoke service.. Server returned error code = 4ji
- Apache ActiveMQ实战(1)-基本安装配置与消息类型
- MySQL之删除慢查询日志
- eclipse安装sonarLint插件
- Python3.6 安装Flask 模块
- desktop位置不可用,桌面文件丢失,重启无效
- 查询外网IP地址的方法
- 微信小程序二维码如何生成?