当前位置: 首页 > 编程笔记 >

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

汝吕恭
2023-03-14
本文向大家介绍maven多模块工程打包部署的方法步骤,包括了maven多模块工程打包部署的方法步骤的使用技巧和注意事项,需要的朋友参考一下

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

1.确保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.在dao目录下执行:mvn clean install -Dmaven.test.skip=true,以生成jar包

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

<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打包插件即可

<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,默认情况下会将所有文件进行打包)。

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

<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):

<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常规工程部署结构:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 公用 模型 存储库 服务 网站 我们搜索了一下,没有找到一个解决方案,当项目具有这种结构时,如何制作一个可执行的jar。 下面是父pom.xml: 有人使用Spring Boot、Maven插件和Maven模块项目结构创建了可执行的fat jar吗?

  • 我有一个父pom和两个子pom,用于jar和war文件。war文件将jar文件作为pom中的依赖项。因此,构建顺序定义如下 家长 儿童罐 儿童战争 在父pom中,模块如下所示。 要部署到Jboss的最后一个工件位于父/工作区/子war/目标/项目文件夹下。战争 当我从父工作区文件夹调用maven目标时,部署失败。 mvn清理编译安装jboss为:deploy-Dmaven。测验跳过=真 我尝试在父

  • 本文向大家介绍spring boot测试打包部署的方法,包括了spring boot测试打包部署的方法的使用技巧和注意事项,需要的朋友参考一下 有很多网友会时不时的问我,spring boot项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下spring boot 如何开发、调试、打包到最后的投产上线。 开发阶段 单元测试 在开发阶段的时候最重要的是单元测试了,spring

  • 我有一个maven-module“web-app”,其打包类型为(JSF-application)。它依赖于同一个项目的另一个模块“核心”,打包类型为。这个jar包含Java类和资源(js或XHTML文件)。 现在,我在IntelliJ2016.3中使用Web-App的分解版本配置JBoss上的一个部署。但是模块“核心”也不会爆炸,所以我不能在核心的资源上执行热部署。但是:班级工作的热部署。 我已

  • 看完前面的文档,你会发现使用 Blade 开发Web应用是简单的、快速的,当然也功归于约定。 我们使用 Maven 构建项目,打包同时也可以用它,如果你对 Maven 插件熟悉的话可能也用过了, 当然没用过也没关系,你只需要按照下面的操作配置一下即可。 打包工程 添加插件 <build> <finalName>hello</finalName> <plugins>

  • 我有一个多模块maven项目。它的设置是正确的,因为当我运行maven clean install时,它成功地构建了所有模块。Webapp包含我需要在web服务中具备的通用功能,因此我将其作为maven依赖项包含在web服务中。 MavenReactor摘要: Eclipse甚至会将webapp项目到web服务的部署程序集。