最近有個新項目是java web項目,記錄一下,可能比較亂。雖然沒接觸過Java,可是eclipse仍是用過的html
同事說,項目是maven打包的,能夠用maven開啓服務,我就研究了一下,因爲項目不能外露,我先建立一個demo的項目。java
打開eclipse File->New->Maven Project。若是默認列表中沒有Maven Project 選擇 Other...web
以下圖,什麼不用選擇,直接Nextspring
如圖一次找到 Server->Runtime Environments 點擊子面板上的 Add 按鈕
apache
選擇你本身的下載的tomcat 版本,我下載的是7,點擊Next
windows
找到剛纔解壓好的Tomcat路徑,點擊finish
api
項目上右鍵依次 Run As-> Run on Server,選擇剛纔添加的Tomcat,
瀏覽器
上圖,成功開啓WebApp
spring-mvc
下面介紹如何用CMD,本身用maven 命令行建立web項目tomcat
# 打出版本信息 Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T02:41:47+08:00) Maven home: C:\Workflow\apache-maven-3.6.0\bin\.. Java version: 1.8.0_172, vendor: Oracle Corporation, runtime: C:\Program Files (x86)\Java\jdk1.8.0_172\jre Default locale: en_US, platform encoding: GBK OS name: "windows 10", version: "10.0", arch: "x86", family: "windows"
mvn archetype:generate # 建立 -DgroupId=packageName # 至關於 eclipse裏面的groupid -DartifactId=webappName # 至關於 eclipse裏面的artifactid -DarchetypeArtifactId=maven-archetype-webapp #至關於在eclipse中選擇maven-archetype-webapp # 命令請弄成一行,執行 mvn archetype:generate -DgroupId=packageName -DartifactId=webappName -DarchetypeArtifactId=maven-archetype-webapp #這裏說明一下,在以前的maven版本使用create,在3.0.5版本以後使用generate替代create了
#貼一下成功的結果 [INFO] Scanning for projects... [INFO] [INFO] ------------------< org.apache.maven:standalone-pom >------------------- [INFO] Building Maven Stub Project (No POM) 1 [INFO] --------------------------------[ pom ]--------------------------------- [INFO] [INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<< [INFO] [INFO] [INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom --- [INFO] Generating project in Interactive mode Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-bundles/2/maven-archetype-bundles-2.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-bundles/2/maven-archetype-bundles-2.pom (1.5 kB at 2.2 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/maven-archetype-parent/1/maven-archetype-parent-1.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/maven-archetype-parent/1/maven-archetype-parent-1.pom (1.3 kB at 1.9 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/4/maven-parent-4.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/4/maven-parent-4.pom (10.0 kB at 15 kB/s) [INFO] Using property: groupId = packageName [INFO] Using property: artifactId = webappName Define value for property 'version' 1.0-SNAPSHOT: : test [INFO] Using property: package = packageName Confirm properties configuration: groupId: packageName artifactId: webappName version: test package: packageName Y: : [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: basedir, Value: C:\Workflow\Eclipse WS [INFO] Parameter: package, Value: packageName [INFO] Parameter: groupId, Value: packageName [INFO] Parameter: artifactId, Value: webappName [INFO] Parameter: packageName, Value: packageName [INFO] Parameter: version, Value: test [INFO] project created from Old (1.x) Archetype in dir: C:\Workflow\Eclipse WS\webappName [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 43.033 s [INFO] Finished at: 2018-12-18T14:48:42+08:00 [INFO] ------------------------------------------------------------------------
C:\Workflow\Eclipse WS\webappName>mvn tomcat:run [INFO] Scanning for projects... [INFO] [INFO] -----------------------< packageName:webappName >----------------------- [INFO] Building webappName Maven Webapp test [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] >>> tomcat-maven-plugin:1.1:run (default-cli) > compile @ webappName >>> [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ webappName --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ webappName --- [INFO] No sources to compile [INFO] [INFO] <<< tomcat-maven-plugin:1.1:run (default-cli) < compile @ webappName <<< [INFO] [INFO] [INFO] --- tomcat-maven-plugin:1.1:run (default-cli) @ webappName --- [INFO] Running war on http://localhost:8080/webappName [INFO] Using existing Tomcat server configuration at C:\Workflow\Eclipse WS\webappName\target\tomcat 十二月 18, 2018 3:07:41 下午 org.apache.catalina.startup.Embedded start INFO: Starting tomcat server 十二月 18, 2018 3:07:41 下午 org.apache.catalina.core.StandardEngine start INFO: Starting Servlet Engine: Apache Tomcat/6.0.29 十二月 18, 2018 3:07:41 下午 org.apache.coyote.http11.Http11Protocol init INFO: Initializing Coyote HTTP/1.1 on http-8080 十二月 18, 2018 3:07:41 下午 org.apache.coyote.http11.Http11Protocol start INFO: Starting Coyote HTTP/1.1 on http-8080
瀏覽器 訪問上面提供的網址,發現報錯了。以下圖
打開項目目錄下的pom.xml文件,在build標籤下增長以下內容
<plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8080</port> <uriEncoding>UTF-8</uriEncoding> <url>http://localhost:8080</url> <server>tomcat7</server> </configuration> </plugin> </plugins>
C:\Workflow\Eclipse WS\webappName>mvn tomcat7:run [INFO] Scanning for projects... [INFO] [INFO] -----------------------< packageName:webappName >----------------------- [INFO] Building webappName Maven Webapp test [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ webappName >>> [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ webappName --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ webappName --- [INFO] No sources to compile [INFO] [INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ webappName <<< [INFO] [INFO] [INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ webappName --- [INFO] Running war on http://localhost:8080/webappName [INFO] Using existing Tomcat server configuration at C:\Workflow\Eclipse WS\webappName\target\tomcat [INFO] create webapp with contextPath: /webappName 十二月 18, 2018 3:26:14 下午 org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["http-bio-8080"] 十二月 18, 2018 3:26:14 下午 org.apache.catalina.core.StandardService startInternal INFO: Starting service Tomcat 十二月 18, 2018 3:26:14 下午 org.apache.catalina.core.StandardEngine startInternal INFO: Starting Servlet Engine: Apache Tomcat/7.0.47 十二月 18, 2018 3:26:16 下午 org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["http-bio-8080"]
訪問瀏覽器,發現能夠正常顯示Hello World
至此,使用maven 建立了一個web項目並運行起來
給我項目中,我是用jetty起來的,沒有使用tomcat,介紹下jetty的使用方法
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.5</version> <configuration> <webAppSourceDirectory>src/main/webapp</webAppSourceDirectory> <scanIntervalSeconds>0</scanIntervalSeconds><!--這個是jetty支持的java熱部署,可是不建議使用--> <contextPath>/webappName</contextPath> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>8080</port> </connector> </connectors> </configuration> </plugin>
# maven 一頓安裝以後,輸出以下, [INFO] Configuring Jetty for project: webappName Maven Webapp [INFO] Webapp source directory = C:\Workflow\Eclipse WS\webappName\src\main\webapp [INFO] web.xml file = C:\Workflow\Eclipse WS\webappName\src\main\webapp\WEB-INF\web.xml [INFO] Classes = C:\Workflow\Eclipse WS\webappName\target\classes [INFO] Logging to org.slf4j.impl.MavenSimpleLogger(org.mortbay.log) via org.mortbay.log.Slf4jLog [INFO] Context path = /webappName [INFO] Tmp directory = C:\Workflow\Eclipse WS\webappName\target\work [INFO] Web defaults = jetty default [INFO] Web overrides = none [INFO] Webapp directory = C:\Workflow\Eclipse WS\webappName\src\main\webapp [INFO] Starting jetty 6.1.5 ... [INFO] jetty-6.1.5 [INFO] No Transaction manager found - if your webapp requires one, please configure one. [INFO] Started SelectChannelConnector@0.0.0.0:8080 [INFO] Started Jetty Server
Eclipse中固然也可使用上面的maven 命令進行項目build
作以下的配置,
Console 輸出
訪問瀏覽器會發現均可以訪問Hello World
改個java 代碼就須要從新開服務器感受太耽誤效率了。最後瞭解到有一個叫Jrebel的插件,下面介紹一下
依次點擊 Help -> Eclipse Marketplace搜索 Jrebel 插件,依次安裝上,重啓Eclipse。
依次打開 Help->JRebel->Configuration配置lincense,免費的話選擇第三個Activation Code,將剛纔註冊帳號時給的code複製進來就能夠。在這裏推薦一個永久免費的,是我在網上找到的一個server:http://139.199.89.239:1008/,郵箱隨便輸入就行了。
在你的Tomcat啓動以前在Console裏面看到以下輸出,證實你成功了:
2018-12-18 17:14:49 JRebel: Starting logging to file: C:\Users\kanewang\.jrebel\jrebel.log 2018-12-18 17:14:49 JRebel: 2018-12-18 17:14:49 JRebel: ############################################################# 2018-12-18 17:14:49 JRebel: 2018-12-18 17:14:49 JRebel: JRebel Agent 2018.2.3 (201811261141) 2018-12-18 17:14:49 JRebel: (c) Copyright ZeroTurnaround AS, Estonia, Tartu. 2018-12-18 17:14:49 JRebel: 2018-12-18 17:14:49 JRebel: Over the last 2 days JRebel prevented 2018-12-18 17:14:49 JRebel: at least 8 redeploys/restarts saving you about 0.3 hours. 2018-12-18 17:14:49 JRebel: 2018-12-18 17:14:49 JRebel: License acquired from License Server: http://139.199.89.239:1008 2018-12-18 17:14:49 JRebel: 2018-12-18 17:14:49 JRebel: Licensed to kanewang. 2018-12-18 17:14:49 JRebel: 2018-12-18 17:14:49 JRebel: 2018-12-18 17:14:49 JRebel: ############################################################# 2018-12-18 17:14:49 JRebel:
咱們改一個java代碼
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <mvc:default-servlet-handler/> <!-- Activates scanning of @Controller --> <!-- 這裏是你剛纔定義的包位置,我只有一層 這裏是告訴spring kane1包下帶@Controller annotation的方法都有映射到servlet裏面去 --> <context:component-scan base-package="kane1" /> <mvc:annotation-driven/> </beans>
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.3.RELEASE</version> </dependency>
package kane1; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class test1Controller { @RequestMapping(value = "test", method = RequestMethod.GET)//定義方法與url的映射關係 public @ResponseBody String test(HttpServletRequest request,HttpServletResponse response) { return "This is test1 controller's test function"; } }
從新啓動服務器,在url中輸入 http://localhost:8080/kane1/test,上圖:
咱們這個時候更改一下test1.Controller.java,將輸出更改成 "This is test1 controller's test function modify!",在console裏面看到以下提示證實JRebel成功在檢測java代碼的變化
2018-12-19 10:01:06 JRebel: Reloading class 'kane1.test1Controller'.
Jrebel 自己支持不少種使用方式,下面咱們再嘗試一下,本身在使用命令行,如何進行熱部署的配置。
│ 3rd-party-licenses-jrebel.txt │ Changelog.txt │ jrebel.ico │ jrebel.jar │ jrebel.png │ License.txt │ readme.txt │ ZeroTurnaround.url │ ├─bin │ activate-gui.cmd │ activate-gui.sh │ activate.cmd │ activate.sh │ go-offline.cmd │ go-offline.sh │ jrebel-activation.jar │ └─lib jrebel32.dll jrebel64.dll libjrebel32.dylib libjrebel32.so libjrebel64.dylib libjrebel64.so #咱們直接使用lib下的文件,bin下的文件是登陸激活使用的,由於在Eclipse裏面已經激活了,是不須要再次激活的
set MAVEN_OPTS=-agentpath:[your extract path]\lib\jrebel32.dll # 設置環境變量,後面是32仍是64看你的jdk位數,錯了會提醒你的
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <configuration> <scanIntervalSeconds>0</scanIntervalSeconds> </configuration> </plugin>
#看到以下輸出,就證實成功了 2018-12-19 10:20:49 JRebel: Starting logging to file: C:\Users\kanewang\.jrebel\jrebel.log 2018-12-19 10:20:49 JRebel: 2018-12-19 10:20:50 JRebel: ############################################################# 2018-12-19 10:20:50 JRebel: 2018-12-19 10:20:50 JRebel: JRebel Agent 2018.2.3 (201811261141) 2018-12-19 10:20:50 JRebel: (c) Copyright ZeroTurnaround AS, Estonia, Tartu. 2018-12-19 10:20:50 JRebel: 2018-12-19 10:20:50 JRebel: Over the last 2 days JRebel prevented 2018-12-19 10:20:50 JRebel: at least 12 redeploys/restarts saving you about 0.5 hours. 2018-12-19 10:20:50 JRebel: 2018-12-19 10:20:50 JRebel: License acquired from License Server: http://139.199.89.239:1008 2018-12-19 10:20:50 JRebel: 2018-12-19 10:20:50 JRebel: Licensed to kanewang. 2018-12-19 10:20:50 JRebel: 2018-12-19 10:20:50 JRebel: 2018-12-19 10:20:50 JRebel: ############################################################# 2018-12-19 10:20:50 JRebel:
#繼續看cmd輸出 [WARNING] !RequestLog [INFO] Started SelectChannelConnector@0.0.0.0:8080 [INFO] Started Jetty Server # 有下面這句 2018-12-19 10:22:49 JRebel: Reloading class 'kane1.test1Controller'.
上結果圖,輸出變化了。
給個官網的關於本身配置JRebel的連接 連接
咱們這個項目比較老,用的spring MVC框架,可是須要配置比較繁瑣。因此上面的東西可能都已經沒啥用了,本人也是初學者。目前應該都是使用Spring boot建立了,推薦一個在線建立項目的網址,很好用。Spring Boot 等再說了。本人真的是一點沒學過java,有什麼錯誤的地方望大神指出,好作學習。謝謝。