MAVEN Multi-Module Engineering package Deployment

Source: Internet
Author: User
Tags unpack

General MAVEN multi-module engineering structure as shown below, the figure is divided into DAO data layer and the upper Web layer (of course, there can be service layer), in the multi-module division, the DAO layer is generally packaged with a jar, the Web layer for war packaging. When a war package is deployed, it is found that DAO is present in the Lib package directory as a jar package, which can be cumbersome if related configuration modifications are required on the deployment server. Therefore, the following methods for merging and packaging with Maven are studied:



1. Ensure that the following configuration is available in DAO Pom.xml

<resources>
    <resource>
        <directory>${basedir}/src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
            <include>**/*.properties</include >
        </includes>
    </resource>
    <resource>
        <directory>${basedir}/src/main /resources</directory>
    </resource>
</resources>


2. Execute in DAO Directory: mvn clean install-dmaven.test.skip=true to generate jar package


3. Add the following plug-in configuration to the Web engineering pom.xml

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>maven-dependency-plugin </artifactId> <executions> <execution> <id>unpack</id> &
            Lt;phase>generate-resources</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <ar Tifactitem> <groupId>com.test</groupId> <artifactid>d Ao</artifactid> <version>1.0</version> &LT;TYPE&GT;JAR&L T;/type> <overWrite>true</overWrite> <outputdirectory&gt ;.
            /target/classes</outputdirectory> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> 

The above plugin means to extract the Dao-1.0.jar into the target/classes directory of the Web project (i.e. the project compiled source directory)


4. Run the Web project, you can find DAO related configuration files and source code has been merged. (Note that DAO's jar will also be included in the default war package's Lib)



If you also need a jar-packaged deployment of the war project, add the jar packaging plug-in to the Web engineering pom.xml

<plugin> <groupId>org.apache.maven.plugins</groupId> < artifactid>maven-jar-plugin</artifactid> <executions> <execution> <id>1 </id> <phase>package</phase> <goals> <goal>jar</g oal> </goals> <configuration> <classesdirectory>./target/clas Ses</classesdirectory> <excludes> <exclude>*.properties</exclude&
                    Gt <exclude>*.xml</exclude> </excludes> <finalname>web</finalname&
                Gt <outputDirectory>./target/WEB-INF/lib</outputDirectory> </configuration> </execut Ion> </executions> </plugin> 
After joining, the MVN install command is executed under the WEB, and Web.jar files are generated in the Target/web-inf/lib directory, and the jar does not contain properties and XML files (remove excludes, All files are packaged by default.

The configuration file is not packaged in the above case, so the configuration file needs to be copied:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>maven-resources-plugin& lt;/artifactid> <version>2.4.3</version> <executions> <execution> &
                Lt;id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> ;encoding>utf-8</encoding> <outputdirectory>./target/web-inf/classes</outputdirectory&gt
                ; <resources> <resource> <directory>./target/classes</direc
                        Tory> <includes> <include>*.properties</include>
                    </includes> <filtering>true</filtering> </resource> </resources> </configuration> </execution> ;! --Copy web.xml file--> <execution> <id>copy-web-xml</id> <phase>pack age</phase> <goals> <goal>copy-resources</goal> </goals > <configuration> <encoding>UTF-8</encoding> <outputdi 
                        Rectory>./target/web-inf</outputdirectory> <resources> <resource> <directory>./src/main/webapp/WEB-INF</directory> <includes
                        > <include>*.xml</include> </includes> <filtering>true</filtering> </resource> </resources > </configuration> </execution> </executions> </plugin> 
The configuration file can be copied to the Target/web-inf/classes directory by the way it is configured


With this configuration, there is also a lack of other jar packages on the Web engineering deployment, so you need to copy the jar package (while removing the dependent DAO):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId> maven-dependency-plugin</artifactid>
    <executions>
        <execution>
            <id>copy< /id>
            <phase>install</phase>
            <goals>
                <goal>copy-dependencies</goal >
            </goals>
            <configuration>
                <outputdirectory>./target/web-inf/lib</ outputdirectory>
                <excludeGroupIds>com.test</excludeGroupIds>
            </configuration>
        </execution>
    </executions>
</plugin>


From the above steps, you can see the Web General engineering deployment structure in the target directory:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.