About Maven resources - resources

From the stand point of a Maven project, should files stored there be accessed in any special way or be treated as any other file where "resources" is just another directory?
What makes this directory special? Is it fair to say that "resource" is anything that is not a source file and that is used by a program?
I am a bit confused here. Please clarity

All files in the resources directory get added to your jar (or war) without being compiled. Generally things like config files or other non-source resources are put in this directory, although as long as your files don't end in ".java" they could live in the sources directory and the resulting artifact would be the same.
To access a file in the resources directory you would use the ClassLoader.getResource or getResourceAsStream methods.
The other feature of resources when using Maven is that you can include property tokens that will be replaced by Maven as part of building your project. For instance:
This line in a resource file
artifactName=${project.build.finalName}
Would be replaced with something like:
artifactName=my-project-1.0.0
Any of the properties available within Maven can be replaced in your resources.

It's just a standard. By default, the contents of the resources directory are copied to the same target directory as the .class files and, if packaged as a jar, in the root of the jar.
You can also specify how resources are encoded. If you don't, you effectively preserve the encoding of the system on which you built, which is non-portable and is something Maven will warn you about.

Related

Spring boot alternate src/main/resource directory on classpath

This post is not about serving static resources in Spring Boot. There are other posts that cover that nicely. I think I have a grasp now of the different /public /static directory options, whether located underneath src/main/resources or not. What I want to know is whether I can specify an additional directory in my project whose contents will also be placed at the root of the classpath (as opposed to the web document root).
Specifically, I am referring to my translated resource bundles (e.g., index.properties, index_fr.properties, index_de.properties, etc.). In my pre-Spring Boot projects I have a "bundles" directory designated for all my translated property files. I would prefer to continue using a bundles directory for my translated strings and not have to place them in my src/main/resources directory with the main application property files.

How to load a resource from disk in OSGI environment

Part 1: I've been trying to load a (XML) file as a resource from disk using bundle class loader. I can't package the file in a bundle beforehand as it'll be generated at runtime. I tried searching too, and almost always, people talk about loading resources from within a bundle (either same or different). So is it even possible to load a resource from disk in an OSGi environment using bundle classloader? If yes, how can it be done?
Part 2: Now I need to add a constraint to the above. In my complete scenario, while I'd be generating the file, it would be loaded by a third-party bundle. In this case, what could be done (generate in a certain location, any changes to classpath etc.) so that the third-party bundle's class loader could find the generated file?
Using: apache karaf 3.0.2, ubuntu 12.
Part 1:
So is it even possible to load a resource from disk in an OSGi environment using bundle classloader?
Resources (read-only files on the classpath) can be loaded with classloaders, not ordinary files from any folder of the disk. When you want to process the content of files from the ClassPath, you should use the classloader.
You want to generate a temporary file (generated and processed at runtime) so you should use the standard Java API for that:
File myTmpFile = File.createTempFile(...);
For more info, see the javadoc of this function: https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String)
Part 2:
The third bundle should have an API that either accepts a File, URL, Path or other type instance that can point to a file in the file system.

creating a jar file - preserving file permissions

I would like to know how to create a jar file that preserves the file permissions of its content.
I'm packaging up source and executables in a jar file that will be extracted before use. People should be able to immediately run the examples and demonstrations by running batch / shell script files. Then they should be able to modify source and re-compile everything.
I'm trying to make life easy for people who use it and this includes people who are new to Java and programming who might be working on Linux / Unix.
I have access to machines running Windows and Linux. I don't use Linux a lot so maybe I've done something wrong.
I created a jar file on Linux using jar cf .... after setting permissions on the files so that everything worked as needed. Then I created a new test directory and extracted the jar file contents into it. The file permissions were not preserved ... i.e. -rw-rw-r--
The jar tool doesn't store file permissions, so you can't recover what has not been stored. jar format is not intended to archive files and metadata as zip or tar, it is a simple container to embed java files for an application.

Remove configurations from unrelated environments when building a Grails WAR

When building an environment-specific Grails WAR file configurations from other environments are included in the WAR file as well. For example, all the database connections properties from the production environment are also included in a WAR file built for test. Even though the configuration is compiled to class files it's quite simple to extract sensitive data from there.
In order to improve security, I want to exclude unrelated environment configurations from a WAR file. Is there a way to strip this configuration automatically during the WAR build or do I have to externalize these configurations and deploy them separately?
I suppose if you could somehow analyse the bytecode and work out precisely which Config$_closure3$_closure12 corresponds to the bit you don't need then you might be able to replace it in the war with an empty closure, but it's probably more hassle than it's worth. There's certainly no easy way I'm aware of.
I would put the config for each env in a separate file. If you put the prod config in web-app/WEB-INF/classes/myapp-prod.groovy (still in an environments block) and the dev config in a file in the top directory, and say
grails.config.locations = ["classpath:myapp-prod.groovy", "file:myapp-dev.groovy"]
then only the prod configuration will end up in the war. Though it would be completely human readable, so it may be more secure to simply leave the prod config directly in Config.groovy (so it gets compiled) and just use the external to override for dev mode.

How to get the jar filename of web-fragment

In servlet 3, web-fragments are jars deployed under WAR's WEB-INF/libs. The resources of web-fragments will be merged and be traded as they are resources of WAR. But that's not suitable for every situation.
For example when I want to get some resources under web-fragment's WEB-INF directory, i cannot read them directly, because no matter it's in a jar or a war, the resources under WEB-INF is simply not accessable by URL. Even worse, the typical way to read such resource, with the help of ServletContext doesn't work for web-fragments, because they are jars, not a real directory structure.
Finally I found a solution, to read them with ClassLoader, anyway I still need the exact resource name, I cannot do something like give me all xml files under the directory WEB-INF/myconfig/ The only way I think is to scan all web-fragement jars, build a directory structure in memory. However I still don't know how to get the jar names of all web-fragments. If I cannot get them, I must scan all jars under WEB-INF/libs. Is there any better solution?
Thanks

Resources