maven實例

  • 創建簡單的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>








發佈了55 篇原創文章 · 獲贊 17 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章