新建一個Maven工程以後看起來是這樣的java
能夠看出我咱們須要的結構有很大差異,好比說JDK的版本不對,source folder也莫名其妙的只有一個,junit的依賴也太老了...git
對於我如今的程序來講,也許沒有hibernate和spring data jpa,沒有freemarker,也沒有其它什麼東西,可是以下幾個東西是必定會有的:github
springweb
apache commonsspring
jsonapache
logbackjson
junitapi
jstltomcat
因此個人初始化pom是這樣的:mvc
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.laolang.gongda</groupId> <artifactId>bookshoptest</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>bookshoptest Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <!-- 依賴版本信息 --> <spring.version>4.1.9.RELEASE</spring.version> <!-- 項目相關 --> <!-- 文件拷貝時的編碼 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- 編譯時的編碼 --> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> </properties> <dependencies> <!-- test --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-all</artifactId> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.dbunit</groupId> <artifactId>dbunit</artifactId> <version>2.5.0</version> <scope>test</scope> </dependency> <dependency> <groupId>com.github.springtestdbunit</groupId> <artifactId>spring-test-dbunit</artifactId> <version>1.2.1</version> <scope>test</scope> </dependency> <!-- java ee --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- commons --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <!-- json --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.2</version> </dependency> <!-- logback --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.13</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>org.logback-extensions</groupId> <artifactId>logback-ext-spring</artifactId> <version>0.1.2</version> </dependency> </dependencies> <build> <finalName>bookshoptest</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <!--這裏也可使用tomcat7-maven-plugin --> <version>2.2</version> <!--最新版本 --> <configuration> <url>http://localhost:8080/manager/text</url> <!--配置遠程tomcat的路勁 --> <username>admin</username> <password>admin</password> <uriEncoding>UTF-8</uriEncoding> <!--tomcat的url編碼 達到和修改server.xml文件同樣的功能 --> </configuration> </plugin> </plugins> </build> <reporting> <plugins> <!-- mvn cobertura:help 查看cobertura插件的幫助 --> <!-- mvn cobertura:clean 清空cobertura插件運行結果 --> <!-- mvn cobertura:check 運行cobertura的檢查任務 --> <!-- mvn cobertura:cobertura 運行cobertura的檢查任務並生成報表,報表生成在target/site/cobertura目錄下 --> <!-- cobertura:dump-datafile Cobertura Datafile Dump Mojo --> <!-- mvn cobertura:instrument Instrument the compiled classes --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>2.5.1</version> </plugin> </plugins> </reporting> </project>
這個pom除了添加上述的依賴以外,還添加了tomcat插件,cobertura插件,以及指定了項目依賴的JDK版本。
這個時候還不能開始寫代碼,由於在eclipse中建立maven項目是不支持servlet 3.0 的,因此咱們還須要修改項目的.setting中的文件,具體文件名是:
.settings/org.eclipse.wst.common.project.facet.core.xml
沒有修改的時候是這樣的:
<?xml version="1.0" encoding="UTF-8"?> <faceted-project> <fixed facet="wst.jsdt.web"/> <installed facet="jst.web" version="2.3"/> <installed facet="wst.jsdt.web" version="1.0"/> <installed facet="java" version="1.7"/> <installed facet="jst.jaxrs" version="2.0"/> </faceted-project>
咱們須要把 jstl.web那一行的version修改成3.0
然而這還不行,還須要把自動生成的web.xml修改成3.0的
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Archetype Created Web Application</display-name> </web-app>
而後更新項目便可
STS默認只顯示一個src/main/resources,若是你新建一個src/main/java的話,又會提示你已存在,這個時候須要修改項目設置 右鍵->Properties->java build path->Order And Export,把下面的複選框都選上就好了
在STS中,若是一個類由spring管理,或者是spring的配置文件,則會出現一個藍色的 s 標記,可是新建立的項目有時候有,但有時候又沒有這個標記,這就須要咱們打開相關設置 右鍵->Properties->spring,選中 Enable project specific settings,再把下面的都選中
而後選擇 Beans Support,再選擇右側的Config Detection,把下面的全部選項都選中
而後再把 Dependenty Management 與 Spring Properties Editor右側的選項選中,再更新項目,關閉再打開項目就能夠看到 s 標記了
最後看起來是這樣的;
這裏說的依賴不是pom文件中的,而是STS新建maven項目時,不會把maven依賴加入到項目配置中,具體操做方法: 右鍵->Properties->Deployment Assembly 若是沒有看到 Maven Dependencies,就要點擊 Add
最後應該是這樣的
在sts中運行maven構建的web項目有以下幾種方式:
第一種我歷來沒用過
第二種我只用過tomcat,也只用它的部署功能,偶爾會用tomcat插件來運行一次進行測試
第三種是我經常使用的,這種方式最大的好處就是能夠很方便的debug。須要注意的是,若是是spring web項目,則要注意Maven Dependencies有沒有加載到項目中,正常來講應該是這樣的:
若是沒有的話,那就按照上述添加 Maven依賴的方式添加。
代碼和運行效果我就不貼了。