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

本篇文章主要介绍了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多模块工程打包部署的方法步骤

以上就是本文的全部内容,希望对大家的学习有所帮助。

 

原文地址:https://www.jb51.net/article/125909.htm