-
創建簡單的Web應用
mvn archetype:create -DgroupId=me.andy.practice -DartifactId=practice -DpackageName=me.andy.practice -DarchetypeArtifactId=maven-archetype-webapp
生成pom.xml文件
packaging元素的值爲war,這個項目會打包成war文件,運行mvn package<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>me.andy.practice</groupId> <artifactId>practice</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>practice Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>practice</finalName> </build> </project>
-
集成jetty服務器插件
運行mvn jetty:run成功後,訪問http://localhost:8080/practice/可以看src/main/webapp/index.jsp內容<build> <finalName>practice</finalName> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> </plugin> </plugins> </build>
<html> <body> <h2>Hello World!</h2> </body> </html>
- 添加J2EE依賴
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> </dependency>
-
構建一個父模塊
pom.xml文件<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>me.andy.practice</groupId> <artifactId>practice-parent</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <name>practice Parent Project</name> <url>http://maven.apache.org</url> <modules> <module>practice</module> </modules> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </pluginManagement> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
表示這個父模塊下面的子模塊<modules> <module>practice</module> </modules>
父模塊的配置所有子模塊都會繼承
pluginManagement定義子模塊重用的插件
-
子模塊指向父模塊
<parent> <artifactId>practice-parent</artifactId> <groupId>me.andy.practice</groupId> <relativePath>../pom.xml</relativePath> <version>1.0-SNAPSHOT</version> </parent>