IntelliJ IDEA Community 社區(qū)版配置 Web 開發(fā)環(huán)境(Gradle + Tomcat)

IntelliJ IDEA 是很好的 IDE, 但是免費(fèi)的社區(qū)版不直接支持開發(fā) Web 項目。所以需要自己配置。
網(wǎng)上大多是教程都是用 Maven 和 jetty 來配置。我剛學(xué) JAVA。因此直接從 Gradle 上手配置了。

這里直接用寫一個 Servlet 的 Helloworld,記錄下踩過的坑,讓后來的人少走彎路。
在這之前請自行安裝 Gradle 并使其能全局運(yùn)行。了解基本的 Gradle 的概念。本文默認(rèn)讀者會使用 Gradle。

下載安裝 IntelliJ IDEA Community

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

新建項目

1.用 Gradle 新建項目
項目目錄下,命令行運(yùn)行

gradle init --type java-library

2.編輯 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"
    }
}

// 要使用的插件,看名字應(yīng)該知道什么作用
// 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 time
    compile 'org.slf4j:slf4j-api:1.7.21'

    // 依賴 servlet
    compile 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/'

3.運(yùn)行命令
注意第一次使用 gradlew 命令,可能會很慢,但是配置 gradlew 仍然有必要。因為后面 idea 運(yùn)行 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 # 查看任務(wù)

4.創(chuàng)建 idea 項目文件

gradlew idea

如果之后項目更新了依賴,可以運(yùn)行下面的命令同步到 idea

gradlew cleanIdea

5.用 Idea 打開項目
直接用 idea 打開 ipr 文件
按 alt + 1 可以顯示文件導(dǎo)航側(cè)欄

配置調(diào)試環(huán)境

1.配置遠(yuǎn)程調(diào)試
進(jìn)入運(yùn)行配置界面


點擊 + remote



寫一個名字,其他直接默認(rèn)就好??梢园训谝恍袇?shù)復(fù)制出來下一步使用,即:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

2.配置 Tomcat 運(yùn)行
同上步驟,進(jìn)入運(yùn)行配置界面
點擊 + Gradle


第一行根據(jù)自己項目填寫 gradle build 的位置
第二行輸入 tomcatRun
第三行填上一個步驟復(fù)制下來的參數(shù)。

編寫 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{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Writer writer = resp.getWriter();
        writer.write("HelloWorld!");
        writer.flush();
        writer.close();
        super.doGet(req, resp);
    }
}

運(yùn)行調(diào)試

1.運(yùn)行


P

這里注意 src/main 下面必須有 webapp 目錄,空目錄都要。不然訪問會出現(xiàn) 404 錯誤


2.調(diào)試
選擇之前配置的遠(yuǎn)程 debug 配置


打斷點
重新訪問查看調(diào)試結(jié)果


參考

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,933評論 18 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,958評論 6 342
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,316評論 25 708
  • 柳梢頭,黃昏后,孤魂影樹花滿地。草青黃,葉低揚(yáng),秋日已過,冬陽未來,落落落! 古道旁,河堤前,三杯白酒送別離。水波...
    菡萏花又開閱讀 284評論 0 0
  • 昨天中午把頭發(fā)剪了 這次剪頭發(fā)的行為非常的隨意 就是在前一天晚上 突然不想要長頭發(fā)了 然后找媽媽要了錢 找了一個有...
    張妙天閱讀 409評論 0 0