我的 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/