項目自動化建構工具gradle 入門4——javaWeb在瀏覽器中顯示helloWorld


在java應用中,其實做的最多的還是java web應用。所以現在我們做的就是用gradle構建一個簡單的web項目,簡單點,直接上代碼吧。

1、進入目錄D:\work\gradle\web,新建文件build.gradle,鍵入內容:

 1 apply plugin: 'war' // 引入war插件,
 2 
 3 repositories {    // 從哪里找jar包
 4     flatDir {    
 5         // 先看下build.gradle文件所在目錄下的 libs目錄中有沒有
 6         dirs 'libs'
 7     }
 8     maven {        
 9         //如果目錄中木有,則找url對應的maven倉庫,下面是阿里的maven倉庫,速度杠桿的。
10         url "http://maven.oschina.net/content/groups/public/"
11     }
12     jcenter() // 官方默認倉庫,JCenter相比mavenCenter構件更多,性能也更好
13     mavenCentral()    //如果都木有,則取maven的官方倉庫吧,某些構件只有此處有
14 }
15 
16 dependencies {
17     providedCompile "javax.servlet:servlet-api:2.5"
18     testCompile "junit:junit:4.12"
19 }
20 
21 war {
22     //from 'src/rootContent' // adds a file-set to the root of the archive
23     //webInf { from 'src/additionalWebInf' } // adds a file-set to the WEB-INF dir.
24     //classpath fileTree('additionalLibs') // adds a file-set to the WEB-INF/lib dir.
25     //classpath configurations.moreLibs // adds a configuration to the WEB-INF/lib dir.
26     //webXml = file('src/someWeb.xml') // copies a file to WEB-INF/web.xml
27 }
build.gradle

2、進入目錄:D:\work\gradle\web\src\main\webapp\WEB-INF,新建文件web.xml,鍵入內容

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 4   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 6   version="4.0">
 7 
 8     <welcome-file-list>
 9         <welcome-file>index.html</welcome-file>
10         <welcome-file>index.htm</welcome-file>
11         <welcome-file>index.jsp</welcome-file>
12     </welcome-file-list>
13 
14 </web-app>

3、進入目錄D:\work\gradle\web\src\main\webapp,新建文件index.html,鍵入內容:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>hello,world</title>
 6 </head>
 7 <body>
 8     <p>hello,world! welcome to gradle!</p>
 9 </body>
10 </html>
index.html

4、進入目錄D:\work\gradle\web,cmd下鍵入命令:gradle assemble

5、進入目錄D:\work\gradle\web\build\libs,拷貝web.war 到tomcat服務器下的webapp目錄下,我電腦上是D:\portable\apache-tomcat-9.0.0.M10\webapps。

6、進入tomcat 的 bin目錄,雙擊startup.bat文件,啟動tomcat服務器。我的電腦上是D:\portable\apache-tomcat-9.0.0.M10\bin\startup.bat。

7、在瀏覽器中輸入地址:localhost:8080/web/index.html 能夠看到結果了。

ok,到此都做完了,很簡單的例子。


 

下面照例稍作解釋:

步驟1:第一行的插件改為war,指明讓gradle干生成war包的活。

repositories部分 和前幾篇一樣,不用改,這個是告訴gradle從哪里找代碼中用到的jar包的。

dependencies部分是告訴gradle,代碼依賴哪些jar包。

  providedCompile 是指tomcat之類的服務器會提供的包,讓gradle不要去下載和獲取。

  testCompile是指編譯測試代碼時需要的jar包

  "javax.servlet:servlet-api:2.5" ,,這個怎么寫,在<項目自動化建構工具gradle 入門2——log4j輸出helloWorld> 的解釋部分有一個暴力的做法。

war部分是告訴gradle生成war包有什么要注意的,一般都是拷貝某些文件到某個文件下之后再生成war包。該部分都注釋掉了。有需要的時候再放開注釋就好了。

 

步驟2是war包中必須的web.xml

步驟3是war包中提供的index.html,我們在瀏覽器中看到的就是這個頁面。

步驟4是執行gradle指令,讓gradle趕緊把war給生成了。

步驟5-7是把war包跑起來。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM