maven - 如何使用 Maven 将生成的文件包含到 war 中,但不将其添加到源中

标签 maven war

我的 Maven 项目中需要其他项目(.jsp、.tag 文件)的依赖项。为了解决这个问题,我从这些其他项目创建了一个“资源”jar,并将它们指定为 Maven 中的依赖项。然后,我解压该 jar(在 ant 中,在生成源生命周期阶段)并将文件复制到需要的位置。这就是问题所在(也欢迎提出更好的整体方法的建议)。我想我可以将这些文件移动到我想要的目标目录中,然后将它们与 war 一起打包,但这不起作用。常见的方法似乎是向 war 插件添加一个额外的资源目录:

How create file and put it in a war with Maven?

Rename a generated file in Maven before building WAR

但是,我真的不想将这些文件保留在我的源代码中,因为它们属于另一个项目。有没有办法可以将临时资源包含到 jar 中?

最佳答案

我想说,这是一个典型案例,您应该查看 Maven War Overlay .

Overlays are used to share common resources across multiple web applications. The dependencies of a WAR project are collected in WEB-INF/lib, except for WAR artifacts which are overlayed on the WAR project itself.

这意味着你可以将另一场 war 的片段应用到你自己的 war 中。最好的事情是,如果所有公共(public)资源都在一个父 war 中,并由其他使用覆盖技术的 war 使用。

您向要用作叠加层的 war 添加依赖项:

<dependency>
    <groupId>com.acne</groupId>
    <artifactId>acne-web-style</artifactId>
    <type>war</type>
    <scope>compile</scope>
</dependency>

然后将其应用到 maven-war-plugin 中:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <overlays>
            <overlay>
                <groupId>com.acne</groupId>
                <artifactId>acne-web-style</artifactId>
                <includes>
                    <include>**/resources/*.jpg</include>
                </includes>
            </overlay>
            <overlay>
                <!-- empty groupId/artifactId represents the current build -->
            </overlay>
        </overlays>
    </configuration>
</plugin>

关于maven - 如何使用 Maven 将生成的文件包含到 war 中,但不将其添加到源中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13383639/

相关文章:

java - Spring启动: How to exclude static files from building . war文件?

java - 如何解包和重新打包 WAR 文件

java - 我们可以更新已部署服务器中的 jar/war 文件,然后重新加载新的 jar/war 文件吗?

java - 在非 Maven-Non-Spring 项目中包含 Maven-Spring-Roo-Hibernate jar 的正确步骤是什么?

maven - 如何在多模块项目中列出Maven中所有激活的配置文件

java - 如何将 Google App Engine 与 Webjars 一起使用?

java - 在 Tomcat 中部署的问题

Maven:从github下载依赖源的插件?

java - 有没有办法使用 Apache poi 从给定的 PowerPoint 文件中准确获取演讲者笔记?

java - 自动更新tomcat应用程序