普通web專案轉化為maven web專案
阿新 • • 發佈:2019-01-29
1..configure ->Convert to Maven Project
2..maven新增jetty支援 ,並且修改webAppSourceDirectory
<!-- 新增jetty支援,Jetty 8 必須 Jdk 1.6+,Servlet 3.0,類似於 Tomcat 7-->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<configuration>
<webAppSourceDirectory>${basedir}/WebContent</webAppSourceDirectory>
</configuration>
</plugin>
重要:maven專案webAppSourceDirectory預設為src/main/webapp對應於普通web專案的webcontent目錄
mvn jetty:run-war 先打包,然後再部署(只打成war包的話也可以用mvn package命令)
mvn jetty:run -Djetty.port=80 預設埠也為8080
3.轉換為maven專案jetty執行亂碼問題(maven打包時候系統預設編碼為 gbk)
pom.xml新增下面兩個plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
${basedir} represents the directory containing pom.xml
4.新增junit依賴
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
5.新增伺服器相關jar包
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
6.新增json所需依賴(Jackson三個主要的模組:缺少這些jar包無法將物件轉成json)
fasterxml為2.x(新版spring用的是這個) 1.x版本的包名是codehaus
<!--物件轉json所需jar包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.1.0</version>
</dependency>
7.依賴jar包放在WebContent/WEB-INF/lib等目錄下的情況
配置編譯引數<compilerArguments>,新增extdirs將目錄下的jar包相對路徑新增到配置中,如下:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>WebContent\WEB-INF\lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>
2..maven新增jetty支援 ,並且修改webAppSourceDirectory
<!-- 新增jetty支援,Jetty 8 必須 Jdk 1.6+,Servlet 3.0,類似於 Tomcat 7-->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<configuration>
<webAppSourceDirectory>${basedir}/WebContent</webAppSourceDirectory>
</configuration>
</plugin>
重要:maven專案webAppSourceDirectory預設為src/main/webapp對應於普通web專案的webcontent目錄
mvn jetty:run-war 先打包,然後再部署(只打成war包的話也可以用mvn package命令)
mvn jetty:run -Djetty.port=80 預設埠也為8080
3.轉換為maven專案jetty執行亂碼問題(maven打包時候系統預設編碼為 gbk)
pom.xml新增下面兩個plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
${basedir} represents the directory containing pom.xml
4.新增junit依賴
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
5.新增伺服器相關jar包
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
6.新增json所需依賴(Jackson三個主要的模組:缺少這些jar包無法將物件轉成json)
fasterxml為2.x(新版spring用的是這個) 1.x版本的包名是codehaus
<!--物件轉json所需jar包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.1.0</version>
</dependency>
7.依賴jar包放在WebContent/WEB-INF/lib等目錄下的情況
配置編譯引數<compilerArguments>,新增extdirs將目錄下的jar包相對路徑新增到配置中,如下:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>WebContent\WEB-INF\lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>