我试图控制哪些文件进入由 mvn package
目标创建的 WAR 包。具体来说,我想从每个包的默认 src/main/resources 文件夹中排除一些文件(我正在尝试针对不同的环境进行构建/包)。
我尝试使用 maven-war-plugin 但失败了。如果我添加此配置(用于测试):
<webResources>
<resource>
<directory>src/main/resources</directory>
<targetPath>WEB-INF/classes</targetPath>
<excludes>
<exclude>*.xml</exclude>
</excludes>
</resource>
</webResources>
...我的 WEB-INF/classes 仍将包含 XML 文件。这是因为 webResources
参数似乎重复了复制过程(上述配置实际上有效,文件没有被复制......但它们在某些其他进程中被复制) 。
Maven-war-plugin 文档 状态:
所有 Maven 2 项目的默认资源目录是 src/main/resources ,它将最终位于 WAR 中的 target/classes 和 WEB-INF/classes 中。在此过程中将保留目录结构。
WAR 插件还能够通过 webResources 参数包含在默认资源目录中找不到的资源。
这有点令人困惑。这是否意味着:
-
webResources
参数是 maven-war-plugin 中的一项功能,允许仅从 src/main/resources 外部包含文件代码> 文件夹?如果是这样,我们如何从 src/main/resources 内部更改复制的文件?
- webResources 参数是 maven-war-plugin 中的一项功能,允许包含来自
src/main/resources
文件夹之外的文件。如果是这样,如何配置才能做到这一点?
- 还有其他选择吗?
I am trying to control which files make into the WAR package that is created by mvn package
goal. Specifically, I want to exclude some files from the default src/main/resources
folder for each package (I am trying to do builds/package for different environments).
I tried using maven-war-plugin but failed. If I add this configuration (for testing):
<webResources>
<resource>
<directory>src/main/resources</directory>
<targetPath>WEB-INF/classes</targetPath>
<excludes>
<exclude>*.xml</exclude>
</excludes>
</resource>
</webResources>
...my WEB-INF/classes
will still contain the XML files. This is because webResources
parameter seems to duplicate the copying process (the above configuration actually works, an files are not copied... but they get copied in some other process instead).
Maven-war-plugin documentation states:
The default resource directory for all Maven 2 projects is src/main/resources which will end up in target/classes and in WEB-INF/classes in the WAR. The directory structure will be preserved in the process.
The WAR Plugin is also capable of including resources not found in the default resource directory through the webResources parameter.
This is a bit confusing. Does it mean that:
- The
webResources
parameter is a feature in maven-war-plugin that allows files to be included only from outside src/main/resources
folder? If so, how can we alter the copied files from inside src/main/resources
?
- The
webResources
parameter is a feature in maven-war-plugin that allows files to be included also from outside src/main/resources
folder? If so, how can it be configured to do this?
- Some other option?
发布评论
评论(2)
资源目录 (
src/main/resources
) 中的资源被复制到构建输出目录 (target/classes
) 在流程资源
阶段。然后,在package
阶段,将target/classes
的内容打包到分布式存档中,如 WAR,并最终位于WEB-INF 中在这种情况下,/classes
。因此,如果您想控制哪些资源最终出现在
WEB-INF/classes
中,您需要控制哪些资源最终出现在target/classes
中,即以某种方式改变绑定到进程的目标的行为-resources
阶段,特别是 Maven 资源插件。为此,(这可能不直观),您可以在 pom 的
resources
元素内声明exclude
或include
元素,如图所示在包含和排除文件和目录中。应用于默认资源目录:如果您想针对不同环境使用自定义排除规则,请将其与 个人资料。例如:
使用此配置文件时,xml 文件不会出现在
target/classes
中,因此它们不会出现在WEB-INF/classes
中最后的战争。您在这里所做的是添加一个附加资源目录,该目录似乎是已包含的默认资源目录。因此,无论您要排除什么,这都不会产生任何影响,因为文件无论如何都会在
process-resources
期间复制到target/classes
,因此最终仍然在中WEB-INF/类
。换句话说,当您想要添加不属于默认资源目录的额外资源时,请使用
webResources
:但我不认为这就是您在这里寻找的内容,建议使用上面建议的方法。
Resources from the default resource directory (
src/main/resources
) are copied into the build output directory (target/classes
) during theprocess-resources
phase. Then, during thepackage
phase, the content oftarget/classes
is taken and packaged into a distributed archive, like a WAR, and end up inWEB-INF/classes
in that case.So, if you want to control what resources end up in
WEB-INF/classes
, you need to control what resources will end up intarget/classes
i.e to somehow alter the behavior of the goal bound to theprocess-resources
phase, specifically theresources:resources
goal of the Maven Resources Plugin.And to do so, (this is probably not intuitive), you can declare
exclude
orinclude
elements insideresources
element of the pom, as shown in Including and excluding files and directories. Applied to the default resource directory:And if you want to do use custom exclusion rules for different environments, combine this with profiles. For example:
And when using this profile, the xml files won't end up in
target/classes
and consequently they won't end up inWEB-INF/classes
in the final war.What you're doing here is adding an additional resources directory, which appears to be the already included default resource directory. So, whatever you'll exclude, this has no effect since the files get copied to
target/classes
duringprocess-resources
anyway and thus still end-up inWEB-INF/classes
.In other words, use
webResources
when you want to add extra resources that are not part of the default resource directory:But I don't think that this is what you're looking for here and suggest using the approach suggested above.
请参阅 帕斯卡的回答正确的配置和对过程的良好解释。我在这里直接回答这个问题:
是前者,
maven-war-plugin
中的webResources
参数只允许包含来自src/main/ 外部的额外资源资源。它不允许允许为
src/main/resources
本身配置包含/排除。See Pascal's answer for right configuration and good explanation of the process. I just give a direct answer to this question here:
It's the former, the
webResources
parameter inmaven-war-plugin
only allows one to include extra resources from outsidesrc/main/resources
. It does not allow one to configure includes/excludes forsrc/main/resources
itself.