Preferred File Location for Static Files in Maven WAR Project - java

I am trying to access some WSDLs and XMLs files from WebApp which i built in Maven as a WAR. Now I knew that the resources folder was the default location for these types of files, but when I checked the war package, I found that the files ended up inside the /WEB-INF folder, which is of not much use. Just wanted to know where do I put these files so that I can access them via URL:
https://server:port/Context/File_Path
I tried to make a folder inside webapp but outside WEB-INF, it did seem to do the trick. But just wanted to know if that the right approach.

Maven has a concept of standard directory layout
So, yes, in order to work with something that can be packaged into WAR with maven-war-plugin you should follow the convention and place the files into the relevant folders (of course you can change these defaults if you wish).
When it comes to the static resources, indeed there is a special src/main/webapp folder. So you're right

Related

Where is the right way that should you put your resources (i.e Images and Sound files) when trying to create a JAR file?

I am using Intellij IDEA and whenever I build a JAR artifact it places itself on the productions folder. When I try to run the JAR file some custom defined functions (i.e. setting Icons on Components and adding sounds) are missing, while running it on the Intellij's IDEA is functioning correctly. I know that it involves using getResourceAsStream method but is there some sort of right way to place external resources?
Additional questions I may add, where is the correct destination for placing the META-INF folder? Should I make a resource folder for all my external files?
This is for Eclipse users, I believe the same thing can be accomplished in other IDE's relatively easily.
Create a resources folder
Put your non-code files in it
Use as "Source Folder"
After creating the JAR, the contents of "resources" will be packed in the JAR.

Properly deploying a small Java project

I'm currently working on a small Java Project (~30 Classes, 5 external libs).
The code accesses resources in the folders src/resources and src/test_resources using getClass.getResouce("/resources/any.file").
Most of these resource files will probably never be touched by a user, but there are also some regular configuration files which are intended to be edited by the end users.
My question now is: How should I be deploying such an application?
Exporting everything into a runnable jar doesn't seem to be a good way, as I don't wanna torture my users and let them unzip the jar for editing the configuration files.
Should I export all of the internal stuff into the jar, and copy the resources directory into a Folder side by side with it? How can I access the resources then?
Thank you guys!
You could copy the resources folder. It doesn't necessarily need to be side-by-side with the jar file. The key is that you need to put the parent folder of the resources folder on the classpath.
For example, you could copy it someplace like:
c:\some\directory\resources
Then, when you execute, do something like:
java -cp c:\some\directory;c:\some\path\to\your.jar;... your.main.ClassName

Overriding WAR classes directory

If you use the Ant to create your WAR via the war task, then (as explained here) it will automatically create a WEB_INF/classes/ directory in the WAR. I would like to change the name of this generated classes/ directory to, say, classpath/ and can't figure out how.
If this is possible, would someone provide a code example as well as a reference to the documentation that shows how to do this (I'm curious!). Thanks!
You shouldn't change the name of the classes to something else.
The directory structure of a WAR file is standard and defines a WEB-INF\classes for all the .class files of your application

Web application build process: Keep config files in WEB-INF/classes?

Some projects, as part of their build process, clean up the classes folder in WEB-INF, while others keep some of their config files in the classes folder. The latter group might use a temporary folder somewhere else to build their project and then copy the generated class files from this temporary folder to the WEB-INF/classes, thus keeping their config files safe.
Is there any best practice regarding web application builds about this? I use ant.
User-accessible config files are better stored outside of /classes, e.g. in /WEB-INF/config. I differentiate config files that site admins can touch (they are placed under WEB-INF) and those, kind of 'static' ones, that are meant for developers/deployment configurators only (stuff like sql scripts, XML/XSLT templates, i18n etc).
It is preferred practice to clean up WEB-INF/classes during builds - some classes get deleted/renamed, so are resource files.
Other config files, under WEB-INF, but not in /classes or /lib, have to be treated as upgradable resources: either replace old ones only when there is a new one, or use specifically designed upgrade classes to add missing config tags or lines.
There are tricky situations, like log4j.properties is sometimes stored into /classes root. It is a bit of a different story how to properly handle it, but in most instances it falls under "delete all classes and copy everything anew".

Accessing XMLBeans generated schemas.jar in Maven project

I have a schemas.jar supplied to me and am trying to access it from within a simple Maven project. I have put the schemas.jar file in my src/main/resources directory, which is in my classpath.
When I am trying to access the created documents with something like:
GetOrdersRequestDocument getOrdersRequestDocument = GetOrdersRequestDocument.Factory.newInstance();
It complains about the GetOrdersRequestDocument (can't find it).
How do I get the project to pick up these classes? Do I need to import anything specific?
I have put the schemas.jar file in my src/main/resources directory, which is in my classpath.
Yes, the files in src/main/resources directory are on your classpath. But this doesn't mean that the content of the jar itself is directly available. You could use a URLClassLoader to load the JAR though.
But... this is not how I would do things. If this is an option, I would just install the JAR in your corporate or local repository (using install:install-file) and declare it as a dependency. This would make the content of your JAR available to your code, like any other dependency.

Categories