【Maven】構建多模塊maven開發項目

更多請參考我的博客:https://hywelzhang.github.io/2017/03/27/Maven-Multi-modules.html

爲什麼需要構建一個多模塊開發框架?

項目爲什麼需要劃分成模塊:
1. 當項目越來越大,每個模塊越來越可能會引用一些相同的jar包,但是版本不一致,很容易造成項目的版本衝突
2. 項目模塊之間用到的一些util類,在其他項目也可能會用到。將util獨立成一個模塊,由專人維護,減少重複工作。還可以打成jar包共享給其他項目使用,提高代碼複用性。
3. 當項目越來越大,build時間越來越長。拆分成模塊後,可以各模塊自行build,打包。

多模塊開發,便於多人協作,每個模塊對應一個pom.xml,整體項目一個pom.xml。可以將共用的jar依賴抽象到項目pom.xml,統一版本管理。模塊單獨需要的jar依賴則放在自己模塊的pom.xml,不會影響到其他模塊,降低耦合。

一個簡單的Maven多模塊架構:

---- Spark-parent
|-- pom.xml (pom)
|
|-- Spark-util
| |-- pom.xml (jar)
|
|-- Spark-module1
| |-- pom.xml (jar)
|
|-- Spark-module2
| |-- pom.xml (jar)
|
|-- Spark-module3
|-- pom.xml (jar)

項目的pom.xml配置文件(Spark-parent下的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>com.test.hans</groupId>
    <artifactId>app-parent</artifactId>
    <!--打包方式,下屬模塊打包方式修改爲jar-->
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <!--包含模塊-->
    <modules>
        <module>app-util</module>
        <module>app-dao</module>
        <module>app-service</module>
        <module>app-web</module>
    </modules>
</project>

jar依賴繼承

如何將各模塊需要的jar依賴抽象到項目pom.xml中呢?
項目pom.xml

<!--dependencyManagement元素下的依賴不會自動引入子模塊,保證靈活性-->
<!--如果不加dependencyManagement元素,dependencies下依賴會自動繼承到所有子模塊-->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.test.hans</groupId>
            <artifactId>org-remote-service</artifactId>
            <version>1.0.26</version>
        </dependency>
    </dependencies>
</dependencyManagement>

子模塊pom.xml
這樣繼承項目pom.xml中的依賴,版本由項目pom.xml控制

<dependencies>
    <!--繼承項目pom.xml的jar依賴,版本由項目pom.xml中指定-->
    <dependency>
        <groupId>com.test.hans</groupId>
        <artifactId>org-remote-service</artifactId>
    </dependency>
    <!--子模塊需要單獨引用的jar依賴-->
    <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>
</dependencies>

plugins繼承

當然build中也可以這麼做

項目pom.xml

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>${encoding}</encoding>
                 </configuration>
            </plugin>         
        </plugins>
     </pluginManagement>
</build>

子模塊pom.xml

<build>
     <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
            </plugin>         
        </plugins>
</build>

這樣就實現了共用依賴的抽取,解決版本衝突問題,同時也給各子模塊重複的靈活性。

完整的pomx.xml實例

部分依賴刪減,不過能理解整個意思就行了。

項目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>com.test</groupId>
  <artifactId>test-project</artifactId>
  <version>1.0-SNAPSHOT</version>
  <modules>
    <module>test-module1</module>
    <module>test-module2</module>
  </modules>
  <packaging>pom</packaging>
  <inceptionYear>2017</inceptionYear>
  <properties>
    <maven.compiler.source>1.5</maven.compiler.source>
    <maven.compiler.target>1.5</maven.compiler.target>
    <encoding>UTF-8</encoding>
    <scala.version>2.10.5</scala.version>
    <spark.version>1.6.1</spark.version>
    <alluxio.version>1.3.0</alluxio.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <repositories>
    <repository>
      <id>scala-tools.org</id>
      <name>Scala-Tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>scala-tools.org</id>
      <name>Scala-Tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </pluginRepository>
  </pluginRepositories>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-streaming_2.10</artifactId>
        <version>${spark.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-sql_2.10</artifactId>
        <version>${spark.version}</version>
      </dependency>
      <dependency>
        <groupId>org.alluxio</groupId>
        <artifactId>alluxio-core-client</artifactId>
        <version>${alluxio.version}</version>
      </dependency>

    </dependencies>
  </dependencyManagement>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.scala-tools</groupId>
          <artifactId>maven-scala-plugin</artifactId>
          <!--<version>${scala.maven.version}</version>-->
          <executions>
            <execution>
              <id>compile</id>
              <goals>
                <goal>compile</goal>
              </goals>
              <phase>process-resources</phase>
            </execution>
          </executions>
          <configuration>
            <scalaVersion>${scala.version}</scalaVersion>
          </configuration>
        </plugin>

        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
          <configuration>
            <source>1.6</source>
            <target>1.6</target>
            <encoding>${project.build.sourceEncoding}</encoding>
          </configuration>
        </plugin>

        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.6</version>
          <configuration>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
          </configuration>
          <executions>
            <execution>
              <id>make-assembly</id>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <reporting>
    <plugins>
      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <configuration>
          <scalaVersion>${scala.version}</scalaVersion>
        </configuration>
      </plugin>
    </plugins>
  </reporting>
</project>

其中一個子模塊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>
    <parent>
          <groupId>com.test</groupId>
          <artifactId>test-project</artifactId>
          <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>test-module1</artifactId>
    <inceptionYear>2017</inceptionYear>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>
        <dependency>
            <groupId>io.spray</groupId>
            <artifactId>spray-json_2.10</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-streaming-kafka_2.10</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.alluxio</groupId>
            <artifactId>alluxio-core-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-streaming_2.10</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.10</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章