Handling Classpath Resources

Another common use case, which requires no changes to the POM shown previously, is the packaging of resources into a JAR file. For this common task, Maven again uses the standard directory layout. This means that by adopting Maven’s standard conventions, you can package resources within JARs, simply by placing those resources in a standard directory structure.

In the following example, you need to add the directory src/main/resources. That is where you place any resources you wish to package in the JAR. The rule employed by Maven is that all directories or files placed within the src/main/resources directory are packaged in your JAR with the exact same structure, starting at the base of the JAR.

Figure 2-2: Directory structure after adding the resources directory

You can see in the preceding example that there is a META-INF directory with an application.properties file within that directory. If you unpacked the JAR that Maven created you would see the following:

Figure 2-3: Directory structure of the JAR file created by Maven

The original contents of src/main/resources can be found starting at the base of the JAR and the application.properties file is there in the META-INF directory. You will also notice some other files like META-INF/MANIFEST.MF, as well as a pom.xml and pom.properties file. These come standard with the creation of a JAR in Maven. You can create your own manifest if you choose, but Maven will generate one by default if you don’t. If you would like to try this example, simply create the resources and META-INF directories and create an empty file called application.properties inside. Then run mvn install and examine the jar file in the target directory.

The pom.xml and pom.properties files are packaged up in the JAR so that each artifact produced by Maven is self-describing and also allows you to utilize the metadata in your own application, should the need arise. One simple use might be to retrieve the version of your application. Operating on the POM file would require you to use Maven utilities, but the properties can be utilized using the standard Java APIs.

Comments

Eloborate

I am new to maven and as you have stated this book is good for beginners I don't see why it should be difficult to follow. Forgive me for saying so, but unless I have missed something obvious, I am unable to start the example suggested because you haven't made clear what needs to be done.. I have created the dir src/main/resources, so it now lives here: my-app\src\main\resources\META-INF

But where do I put the resources and where do I copy them from, I tried copying everything from C:\my-app\src\main\java\com\.. and copied all of it including the 'com' directory into 'my-app\src\main\resources\...' but nothing seems to happen and the 'application.properties' in META-INF doesn't get updated after running mvn install; I've tried running 'mvn install' in both my-app dir and in 'my-pp\src\main\resources'

What am I doing wrong?