Java Maven:在类文件夹中包含一个 META-INF 文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1297473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven: Including a META-INF folder in the classes folder
提问by user157916
I have a very simple WAR project and I want to include a directory named META-INF
at the top of the classes output folder where all the compiled Java classes are.
I'm using Maven, but it seems that by default Maven won't include anything that is not a Java class. So it ignores my META-INF
directory that is sitting at the top of the src
directory.
The META-INF
directory contains a file named persistence.xml
. Any quick pointers on how to instruct Maven to put this directory and file into the output folder?
我有一个非常简单的 WAR 项目,我想META-INF
在所有编译的 Java 类所在的类输出文件夹的顶部包含一个名为的目录。我正在使用 Maven,但似乎默认情况下 Maven 不会包含任何不是 Java 类的内容。所以它忽略了我META-INF
位于src
目录顶部的目录。该META-INF
目录包含一个名为persistence.xml
. 关于如何指示 Maven 将此目录和文件放入输出文件夹的任何快速指示?
回答by jt.
回答by Zac
I'll indirectly answer your question by saying that if you've already made the jump to Maven2 I'd definitely recommend using the Archetype Plugin. Using the webapp archetype will ensure you end up with a canonical directory structure. Anyone else looking at your source will immediiately know where everything is and there won't be any question as to where your files go.
我会间接回答你的问题,如果你已经跳转到 Maven2,我肯定会推荐使用 Archetype 插件。使用 webapp 原型将确保您最终获得规范的目录结构。任何其他查看您的来源的人都会立即知道所有内容在哪里,并且不会对您的文件去向有任何疑问。
回答by Rich Seller
In general, for a Java-based Maven project, non-source files should go in the src/main/resources
sub-directory of the project. The contents of that resources
directory are copied to the output directory (by default, target/classes
) during the process-resourcesphase of the build.
一般来说,对于基于 Java 的 Maven 项目,非源文件应该放在src/main/resources
项目的子目录中。在构建的进程资源阶段,该resources
目录的内容被复制到输出目录(默认为target/classes
)。
For Maven WAR projects, it is slightly more complicated: there is also the src/main/webapp
directory, wherein Maven expects to find WEB-INF/web.xml
. To build your WAR file, that file must exist; otherwise, you'll see an error message like this:
对于 Maven WAR 项目,它稍微复杂一些:还有一个src/main/webapp
目录,Maven 希望在其中找到WEB-INF/web.xml
. 要构建您的 WAR 文件,该文件必须存在;否则,您将看到如下错误消息:
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
As the WEB-INF
directory must exist under src/main/webapp
, I'd recommend avoiding defining it again in src/main/resources
. Although this is perfectly valid and the contents of the two directories will be merged, it can get confusing if a file is defined in both. The contents of src/main/resources
will take precedence as they are copied over the top of the contents from src/main/webapp
.
由于WEB-INF
目录必须存在于 下src/main/webapp
,我建议避免在src/main/resources
. 尽管这是完全有效的并且两个目录的内容将被合并,但如果在两个目录中都定义了文件,则可能会造成混淆。的内容src/main/resources
将优先,因为它们被复制到src/main/webapp
.
回答by Java Guy
Give the below entry in POM.xml
在 POM.xml 中给出以下条目
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<!--webappDirectory>/sample/servlet/container/deploy/directory</webappDirectory-->
<packagingExcludes>**/web.xml</packagingExcludes>
</configuration>
</plugin>
回答by Makub
Put this into pom.xml:
把它放到 pom.xml 中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>