IntelliJ IDEA 是很好的 IDE, 但是免费的社区版不直接支持开发 Web 项目。所以需要自己配置。 网上大多是教程都是用 Maven 和 jetty 来配置。我刚学 JAVA。因此直接从 Gradle 上手配置了。

这里直接用写一个 Servlet 的 Helloworld,记录下踩过的坑,让后来的人少走弯路。 在这之前请自行安装 Gradle 并使其能全局运行。了解基本的 Gradle 的概念。本文默认读者会使用 Gradle。

下载安装 IntelliJ IDEA Community

https://www.jetbrains.com/idea/

新建项目

  1. 用 Gradle 新建项目 项目目录下,命令行运行
gradle init --type java-library
  1. 编辑 build.gradle 这是我的配置
/** This build file was auto generated by running the Gradle 'init' task* by 'Yang' at '16-9-18 下午5:48' with Gradle 3.0** This generated file contains a sample Java project to get you started.* For more details take a look at the Java Quickstart chapter in the Gradle* user guide available at https://docs.gradle.org/3.0/userguide/tutorial_java_projects.html*/// Tomcat 需要
buildscript {repositories {mavenCentral()}dependencies {classpath "com.bmuschko:gradle-tomcat-plugin:2.2.3"}
}// 要使用的插件,看名字应该知道什么作用
// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'war'
apply plugin: "com.bmuschko.tomcat"// In this section you declare where to find the dependencies of your project
repositories {// Use 'jcenter' for resolving your dependencies.// You can declare any Maven/Ivy/file repository here.// jcenter()// maven 的仓库mavenCentral()
}// In this section you declare the dependencies for your production and test code
dependencies {// The production code uses the SLF4J logging API at compile timecompile 'org.slf4j:slf4j-api:1.7.21'// 依赖 servletcompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'// Declare the dependency for your favourite test framework you want to use in your tests.// TestNG is also supported by the Gradle Test task. Just change the// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add// 'test.useTestNG()' to your build script.testCompile 'junit:junit:4.12'// 配置 Tomcat 插件def tomcatVersion = '8.0.27'tomcat   "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}","org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}","org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
}// Tomcat 配置
// Tomcat 配置tomcatRun.contextPath = 'testa/'
tomcatRunWar.contextPath = 'teats/'
  1. 运行命令 注意第一次使用 gradlew 命令,可能会很慢,但是配置 gradlew 仍然有必要。因为后面 idea 运行 Tomcat 会使用这个命令。 你可以在 gradle/wrapper/gradle-wrapper.properties 中配置一下使用本地文件加快速度。

配置离线工作也可加快速度

File > Settings > Build, Execution & Deployment > Compiler > Compiler and de-select Configure on demand The above still uses data but is faster, I was able to load images and maps. However, in addition, if >you want to be completely offline, you need to do the following: File -> Settings ->Build, Execution,Deployment -> Build Tools -> Gradle -> check Offline work

试试有没有配置成功

gradlew build # 编译项目
gradlew tasks # 查看任务
  1. 创建 idea 项目文件
gradlew idea

如果之后项目更新了依赖,可以运行下面的命令同步到 idea

gradlew cleanIdea
  1. 用 Idea 打开项目 直接用 idea 打开 ipr 文件 按 alt + 1 可以显示文件导航侧栏

配置调试环境

  1. 配置远程调试 进入运行配置界面

点击 + remote

写一个名字,其他直接默认就好。可以把第一行参数复制出来下一步使用,即:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
  1. 配置 Tomcat 运行 同上步骤,进入运行配置界面 点击 + Gradle

第一行根据自己项目填写 gradle build 的位置 第二行输入 tomcatRun 第三行填上一个步骤复制下来的参数。

编写 Helloworld

package main.java;/** This Java source file was auto generated by running 'gradle buildInit --type java-library'* by 'Yang' at '16-9-18 下午5:48' with Gradle 3.0** @author Yang, @date 16-9-18 下午5:48*/import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;@WebServlet("/")
public class Library  extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Writer writer = resp.getWriter();writer.write("HelloWorld!");writer.flush();writer.close();super.doGet(req, resp);}
}

运行调试

  1. 运行

这里注意 src/main 下面必须有 webapp 目录,空目录都要。不然访问会出现 404 错误

  1. 调试 选择之前配置的远程 debug 配置

打断点 重新访问查看调试结果

参考

  1. http://yukinami.github.io/2015/07/29/gradle-tomcat-plugin-404/
  2. http://www.cnblogs.com/woshimrf/p/5779811.html
  3. http://stackoverflow.com/questions/18158034/how-to-setup-android-studio-to-work-completely-offline
  4. http://www.cnblogs.com/huang0925/p/3458507.html

IntelliJ IDEA Community 社区版配置 Web 开发环境(Gradle + Tomcat)相关推荐

  1. Python办公自动化 2.1开发环境搭建:PyCharm社区版配置Anaconda开发环境

    课程大纲 第二章 Python10分钟入门 [2.1]:PyCharm社区版配置Anaconda开发环境 [2.2]:Python基础知识及正则表达式入门 第三章 Python操作Excel [3.1 ...

  2. idea社区版配置web项目

    由于社区版直接阉割掉了web功能,所以只能自己配置了,欸,真是百因必有果,这是白嫖党最难过的一天,但是白嫖党总能想办法... 话不多说,直接开始 具体流程为: 下载tomcat,解压 创建项目并完善 ...

  3. 在IntelliJ IDEA里配置Go开发环境

    文章目录 一.Go语言概述 1.Go 语言特色 2.Go 语言用途 二.下载Go安装包 三.安装Go开发工具 四.第一个Go程序 1.编写源程序HelloWorld.go 2.直接运行程序HelloW ...

  4. Linux桌面版安装及开发环境配置

    Linux桌面版安装及开发环境配置 1.安装Linux For Desktop 本文所面向对象无非也是和本人一样的技术小白,和windows系统以及mac不同,对于linux系统的选择却也是众说纷纭, ...

  5. IntelliJ IDEA Community社区版集成Tomcat or Jetty教程

    IntelliJ IDEA Community社区版集成Tomcat教程 第一步: 打开Intellij IDEA -> Preference -> Plugins 第二步: 在Plugi ...

  6. IntelliJ IDEA 2019 配置 PHP 开发环境及创建 PHP 项目

    目录 安装 appserv IntelliJ IDEA 2019 配置 PHP 开发环境 IntelliJ IDEA 2019 创建 PHP 项目 安装 appserv appserv-win32-2 ...

  7. 如何使用 IntelliJ IDEA 2017 配置PHP开发环境 及项目搭建

    IntelliJ IDEA 2017 配置PHP开发环境 及项目搭建 本文适用于初学者利用 IntelliJ IDEA搭建 PHP 工程及 PHP开发环境,本人也是初学者,在学习中有所心得,特此罗列如 ...

  8. 使用IntelliJ IDEA配置Erlang开发环境

    使用IntelliJ IDEA配置Erlang开发环境 https://www.cnblogs.com/jasonduan/p/5175660.html 转自:http://blog.csdn.net ...

  9. IntelliJ IDEA(社区版) 背景图片、颜色、字体等设置

    IntelliJ IDEA(社区版) 背景图片.颜色.字体等设置 1.背景图片设置 2.背景颜色设置 3.字体设置 1.背景图片设置 共有两种方式: ①打开IntelliJ IDEA(社区版),然后依 ...

最新文章

  1. 用C#操作word替换字符,用spire
  2. 数据离散化 - 等宽等频聚类离散 - Python代码
  3. 将S所指的字符串中下标为偶数同时ASCⅡ值为奇数的字符删除,所指串中剩余的字符形成新的串放在t所指的数组中
  4. 实录:有钱男性的真实私生活
  5. mysql 锁设置密码_[转载]mysql锁小结
  6. 【编译制导指令】#pragma pack - 字节数基准对齐
  7. 移远ec20驱动_移远通信Cat 1产品天团迎来新成员,引爆中速连接市场
  8. matlab 高维矩阵转置,matlab中关于矩阵的转置
  9. Windows下XMake编译imgui成功之DX11
  10. Linux切换jdk版本
  11. 二分图/二部图(bipartite graph)
  12. 如何评估ASO优化方案案例推广效果,优化aso方案
  13. spring 注解@primary解析
  14. c#—MemoryStream读图片存入ImageList
  15. 【CodeForces】CF13C Sequence(配数学证明)
  16. 网络打印两个基本端口(LPD和9100)的相关知识
  17. 智慧社区的介绍,为什么要做智慧社区
  18. 高大上网站布局的三个技巧
  19. 蒙氏数学素材---乘法板及作业纸
  20. Guava 本地缓存使用教程

热门文章

  1. 用Debug函数实现API函数的跟踪
  2. 苹果笔记本中为虚拟机扩充硬盘的方法
  3. Vue常用指令v-show
  4. Spring的一些资源
  5. centos 7 部署 open-falcon 0.2.0
  6. 关于接口测试,我们要测什么,怎么上手
  7. js小效果-简易计算器
  8. gradle第二天(Building a RESTful Web Service)
  9. XE3随笔6:SuperObject 的 JSON 对象中还可以包含 方法
  10. HTML4.0 / XHTML 1.0 Reference Manual