【问题标题】:Java EE / Maven - package WEB-INF folder into multiple warsJava EE / Maven - 将 WEB-INF 文件夹打包成多个战争
【发布时间】:2016-11-11 00:44:02
【问题描述】:

我有一个使用 Maven 打包为 EAR 的 Java EE 项目,它包含两个 WAR 模块(一个 web 一个移动),结构如下:

EAR
    -Entities.jar
    -EJB.jar
    -Test.jar
    -Web.war
    -Mobile.war

我在 Web.war/WEB-INF 中有一个目录,其中包含两个 .wars 共有的一些 .xhtml 文件

有什么方法可以指示 Maven 在构建过程中自动将此目录复制到另一个 .war 中?

例如。我愿意。。

WebApp-Web/src/main/webapp/WEB-INF/emails/*

融入

WebApp/WebApp-web/WEB-INF/emails

WebApp/WebApp-mobile/WEB-INF/emails

(我使用的是maven-war-plugin btw)

【问题讨论】:

标签: maven jakarta-ee maven-3


【解决方案1】:

如果您将共享资源添加到“父项目”(或其他地方),您可以在 maven-war-plugin 的 webResources 标签中定义相对路径(对于两个 web 项目):

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <webResources>
            <resource>
                <directory>../src/main/resources</directory>
                <targetPath>WEB-INF</targetPath>
            </resource>
        </webResources>
    </configuration>
</plugin>

另一种方法是在构建时使用 maven-resources-plugin 从相对路径复制文件,但我相信 maven-war-plugin 方法更好。

使用 maven-resources-plugin: (source how-to-get-a-war-package-with-resources-copied-in-web-inf) 我只更改了目录,使其相对于其父级(即:../src/main/resource

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.4.2</version>
    <executions>
      <execution>
        <id>default-copy-resources</id>
        <phase>process-resources</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <overwrite>true</overwrite>
          <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/</outputDirectory>
          <resources>
            <resource>
              <directory>../src/main/resources</directory>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>

【讨论】:

    【解决方案2】:

    如果您使用的是实现 Servlet 3.0 或更新版本的 Web 容器,那么您可以利用 web-fragment 功能将这些公共文件构建到一个 jar 文件中,该 jar 文件添加到每个文件的 WEB-INF/lib 目录中Web应用程序。

    通过将 web 资源(例如 xhtml 文件)放置在 jar 文件的 META-INF/resources 目录中来使它们可用。任何可能与这些 xhtml 文件关联的 web.xml 配置都可以放在 META-INF/web-fragment.xml 文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 2019-01-27
      • 2011-05-19
      相关资源
      最近更新 更多