maven实例
2012-09-22 22:09 is_wolf 阅读(169) 评论(0) 编辑 收藏 举报-
创建简单的Web应用
mvn archetype:create -DgroupId=me.andy.practice -DartifactId=practice -DpackageName=me.andy.practice -DarchetypeArtifactId=maven-archetype-webapp
生成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</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>packaging元素的值为war,这个项目会打包成war文件,运行mvn package
-
集成jetty服务器插件
<build> <finalName>practice</finalName> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> </plugin> </plugins> </build>运行mvn jetty:run成功后,访问http://localhost:8080/practice/可以看src/main/webapp/index.jsp内容<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>
-
构建一个父模块
<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>pom.xml文件
<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>