[英]Maven: Including a META-INF folder in the classes folder

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類。所以它忽略了我位於src目錄頂部的META-INF目錄。 META-INF目錄包含名為persistence.xml的文件。有關如何指示Maven將此目錄和文件放入輸出文件夾的快速指示?

5 个解决方案



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-resources phase of the build.

通常,對於基於Java的Maven項目,非源文件應該放在項目的src / main / resources子目錄中。在構建的進程資源階段,該資源目錄的內容將復制到輸出目錄(默認情況下為目標/類)。

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] ------------------------------------------------------------------------
[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的內容頂部。



Maven wants this type of information in the resources folder. See here for more information.


|-- pom.xml
`-- src
    `-- main
        |-- java
        `-- resources

For specifying additional resource directories, see here.




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.




Put this into pom.xml:





Give the below entry in POM.xml





粤ICP备14056181号  © 2014-2020