Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 659
user157916 Avatar asked Aug 19 '09 02:08

user157916


People also ask

Where do I put Meta-INF?

It shouldn't be at the project root, but directly under the source folder. At runtime, the persistence. xml file is searched in the classpath, under META-INF. So if you want the META-INF folder to be put at the top of the compiled package tree, you need to put it at the top of the source tree.

What is meta-inf in maven?

The META-INF folder is the home for the MANIFEST. MF file. This file contains meta data about the contents of the JAR. For example, there is an entry called Main-Class that specifies the name of the Java class with the static main() for executable JAR files.

What is the purpose of Meta-INF folder?

The META-INF directory The manifest file that is used to define extension and package related data. This file is generated by the new "-i" option of the jar tool, which contains location information for packages defined in an application or extension.

Is Meta-INF in the classpath?

Re: Is META-INF in the classpath? No, not generally. If its needed you would have to use a META-INF/jboss-structure. xml to instruct the structure deployers to add it.


2 Answers

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.

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:

[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.

like image 129
Rich Seller Avatar answered Oct 15 '22 18:10

Rich Seller


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

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

For specifying additional resource directories, see here.

like image 41
jt. Avatar answered Oct 15 '22 18:10

jt.