maven多模塊工程打包部署


一般maven多模塊工程結構如下圖,圖中分為dao數據層和上層web層(當然還可以有service層),在進行多模塊划分的時候,一般將dao層采用jar進行打包,web層進行war打包。

在進行war包部署時,發現dao是以jar包形式存在於lib包目錄下,dao里引用的配置文件也都在自己的jar文件里,

如果部署服務器時,web層會引用不到dao里的配置文件。因此研究了下用maven進行合並打包的方法:

project
    |--business  (核心業務)
        |--business-api
        |--business-service
        |--business-message
        |--business-dao
        |--business-web
    |--common  (公共組件、服務、常量)
        |--common-component
            |--common-component-...
        |--common-service
        |--common-constants
        |--common-...
    |--management  (管理台)
        |--management-...
    |--taskserver (定時任務、批處理)
    |--msgserver  (消息隊列)

 

1.確保dao pom.xml中有以下配置

<!--配置打包時不過濾非java文件開始  -->
  <!--說明,在進行模塊化開發打jar包時,maven會將非java文件過濾掉,
  xml,properties配置文件等,但是這些文件又是必需的,
  使用此配置可以在打包時將不會過濾這些必需的配置文件。
  -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.js</include>
                    <include>**/*.json</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <!-- resource的filtering屬性用來表示資源文件中的占位符是否需要被替換,true為需要替換。 -->
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

2.在web工程pom.xml添加以下插件配置

<build>
      <!-- war的項目配置需加上如下內容   start -->
      <plugins>
          <!-- 下述插件表示將business_service.0.0.1-SNAPSHOT.jar解壓至web工程的target/classes目錄中(即工程編譯源碼目錄)
          子模塊jar文件里的配置文件也會相應解壓到target/classes目錄中
           -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.ovit</groupId>
                                <artifactId>business_service</artifactId>
                                <version>0.0.1-SNAPSHOT</version>
                                <type>jar</type>
                                <overWrite>true</overWrite>
                                <outputDirectory>${project.build.directory}/classes</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
      <pluginManagement>
          <plugins>
              <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
              <plugin>
                  <groupId>org.eclipse.m2e</groupId>
                  <artifactId>lifecycle-mapping</artifactId>
                  <version>1.0.0</version>
                  <configuration>
                      <lifecycleMappingMetadata>
                          <pluginExecutions>
                              <pluginExecution>
                                  <pluginExecutionFilter>
                                      <groupId>
                                          org.apache.maven.plugins
                                      </groupId>
                                      <artifactId>
                                          maven-dependency-plugin
                                      </artifactId>
                                      <versionRange>
                                          [2.1,)
                                      </versionRange>
                                      <goals>
                                          <goal>unpack</goal>
                                      </goals>
                                  </pluginExecutionFilter>
                                  <action>
                                      <ignore></ignore>
                                  </action>
                              </pluginExecution>
                          </pluginExecutions>
                      </lifecycleMappingMetadata>
                  </configuration>
              </plugin>
          </plugins>
      </pluginManagement>
      <!-- war的項目配置需加上如下內容   end -->
  </build>

3.運行web工程,即可發現dao相關配置文件及源碼已合並過來。(注意,此時在默認的war包的lib中還會包含dao的jar)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM