服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - maven多模块工程打包部署的方法步骤

maven多模块工程打包部署的方法步骤

2021-01-19 11:02xb_smart Java教程

本篇文章主要介绍了maven多模块工程打包部署的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一般maven多模块工程结构如下图,图中分为dao数据层和上层web层(当然还可以有service层),在进行多模块划分的时候,一般将dao层采用jar进行打包,web层进行war打包。在进行war包部署时,发现dao是以jar包形式存在于lib包目录下,如果在部署服务器上需要进行相关配置修改会比较麻烦。因此研究了下用maven进行合并打包的方法:

maven多模块工程打包部署的方法步骤

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

?
1
2
3
4
5
6
7
8
9
10
11
12
<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.在dao目录下执行:mvn clean install -dmaven.test.skip=true,以生成jar包

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<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.test</groupid>
            <artifactid>dao</artifactid>
            <version>1.0</version>
            <type>jar</type>
            <overwrite>true</overwrite>
            <outputdirectory>./target/classes</outputdirectory>
          </artifactitem>
        </artifactitems>
      </configuration>
    </execution>
  </executions>
</plugin>

上述插件表示将dao-1.0.jar解压至web工程的target/classes目录中(即工程编译源码目录)

4.运行web工程,即可发现dao相关配置文件及源码已合并过来。(注意,此时在默认的war包的lib中还会包含dao的jar)

若还需要对war工程进行jar打包部署,则在web工程pom.xml中添加jar打包插件即可

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<plugin>
  <groupid>org.apache.maven.plugins</groupid>
  <artifactid>maven-jar-plugin</artifactid>
  <executions>
    <execution>
      <id>1</id>
      <phase>package</phase>
      <goals>
        <goal>jar</goal>
      </goals>
      <configuration>
        <classesdirectory>./target/classes</classesdirectory>
        <excludes>
          <exclude>*.properties</exclude>
          <exclude>*.xml</exclude>
        </excludes>
        <finalname>web</finalname>
        <outputdirectory>./target/web-inf/lib</outputdirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

加入后,在web下重新执行mvn install命令,会在target/web-inf/lib目录下生成web.jar文件,jar中不包含properties和xml文件(去掉excludes,默认情况下会将所有文件进行打包)。

在上面情况下并没有将配置文件打包,因此需要将配置文件进行拷贝:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<plugin>
  <groupid>org.apache.maven.plugins</groupid>
  <artifactid>maven-resources-plugin</artifactid>
  <version>2.4.3</version>
  <executions>
    <execution>
      <id>copy-resources</id>
      <phase>package</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <encoding>utf-8</encoding>
        <outputdirectory>./target/web-inf/classes</outputdirectory>
        <resources>
          <resource>
            <directory>./target/classes</directory>
            <includes>
              <include>*.properties</include>
            </includes>
            <filtering>true</filtering>
          </resource>
        </resources>
      </configuration>
    </execution>
    <!-- 拷贝web.xml文件-->
    <execution>
      <id>copy-web-xml</id>
      <phase>package</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <encoding>utf-8</encoding>
        <outputdirectory>./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>

通过上面配置方式,则可将配置文件拷贝至target/web-inf/classes目录下

通过上述配置,对于web工程部署,还缺少所依赖的其他jar包,因此需要将jar包拷贝过来(同时去除依赖的dao):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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>

通过以上步骤可在target目录看到web常规工程部署结构:

maven多模块工程打包部署的方法步骤

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/hpb21/article/details/51284640

延伸 · 阅读

精彩推荐