I'm developing a spring project and I've just encountered serious problems with loading XML config files within junit (but they are accessible from the web controllers) - can't load XML files.
I listed my classpath (in junit tests) and found that, among the rest, there are 2 directories included:
/var/www/Java/lyricsBase/build/web/WEB-INF/classes/
/var/www/Java/lyricsBase/build/test/classes/
There is just one file I want to include in my test:
/WEB-INF/lyricsBaseApp-servlet.xml
and it imports 3 files below:
/WEB-INF/hibernate.xml
/WEB-INF/dataSource.xml
/WEB-INF/beans.xml
I can clearly see that deploying my project on tomcat copies confg files to /var/www/Java/lyricsBase/build/web/WEB-INF/ABC.xml but this directory is not in the classpath, /var/www/Java/lyricsBase/build/web/WEB-INF/classes is included instead. I read that WEB-INF should not be in the classpath (it's just a Java EE principle). OK - so where should I put my config files to access them easily both from web controllers and junit tests? Any good practices?
Place the config files under WEB-INF/classes on the war package, and they will be loaded into the classpath. Consider deploying your code as an exploded war, so the config files can be changed easily.
Also consider using maven war project for your packaging. If you do, you can just place your config files under src/main/resources under the source code, and they will be packaged into WEB-INF/classes for you.
I had the same problem and from then on started putting them in META-INF under resources. This is where the spring template projects which don't run in Tomcat put them and seemed to solve my issues.
Related
I have a Spring Boot application using Google Pub Sub API. I need to inject Google credentials and other properties using file credentials.json. I put the file in my src/main/resources (otherwise, it will not put the file in the built jar) like this:
spring.cloud.gcp.credentials.location=file:src/main/resources/credentials.json
However, when I build the jar, this file is placed in the root directory and this path is no longer valid. So I am able to run my application from Eclipse, since by the that time, the file is still in my resources directory but I can't run it as a standalone jar after built because the path is suddently just file:credentials.json.
Is there some easy way how to specify the path as relative so it works both in IDE and when running my jar? I can inject the path through env. variables but I would do so only if absolutely necessary.
If you use the classpath prefix then Spring will look for the file on your classapth.
If you put the file in src/main/resources then Maven will, by default, copy it to the root of your classpath and it will then be addressable as follows:
spring.cloud.gcp.credentials.location=classpath:credentials.json
This should hold true whether ...
You are running in your IDE; your IDE's Maven integration will copy the file from src/main/resources to the root of your classpath - typically target/classes
You are running a built JAR; Maven will copy the file from src/main/resources to the root of your JAR
I have a maven based project and my war file would be bigger than 100M. because of firewalls and some network policies in our company, it takes hours to transfer my new war file to our server.
I want to exclude all jar dependencies and locate them into Tomcat lib. How can I handle it in maven? and how can I add my dependencies to tomcat classpath? thank you
You can utilize the maven dependency plugin in order to collect the dependencies in a folder: https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-project-dependencies.html
If you place the libraries in the lib folder of the tomcats they are available for the tomcat common/shared classloader. If you want to put them in a subfolder to avoid mixing up with already present jars used by tomcat you can add that in catalina.properties.
If you have to deplyoy several webapps on the tomcat with possibly different versions of the same dependency you could run in very ugly problems because the loading of the libs by the common classloader is more or less "random".
I am building a webapp through Gradle's war plugin. In order to disable Tomcat's session persistence, I need to place the file context.xml in the META-INF directory of the war's root.
I attempted the following:
Create the file src/main/webapp/META-INF/context.xml of the main project
Create the file src/main/resources/META-INF/context.xml of the main project
However, when I build the project using gradle clean war, the produced war file contains a META-INF with only a MANIFEST.MF in it. It is as if my directory gets overridden.
How do I place context.xml in the war?
src/main/webapp/META-INF/context.xml is correct and works fine for me. Chances are that you went wrong somewhere, or that there is a problem with your build script.
I have a Maven project. It is successfully deploying the jar file. I also want it to deploy the contents of src/main/resources.
mvn deploy does not deploy the resources.
How can I make it do that?
I read about using the copy file task and other workaround methods, but I want to use Maven's default behavior for deploying, which I thought would include the resources.
The folder src/main/resources contains resources which will be packaged into the jar file which means in other words it is already deployed within the created jar file.
I am using Maven for creating my project structure. The following is the way I am doing
Generate Archetype
Have the following modules - Ear, War and WarSource (I am deleting the src, ejb and jar folders)
So my EAR will have 2 modules - War and WarSource which inturn have src/main/java and src/main/resource folders
Question is - where should my application context reside so that I avoid the File Not Found error during runtime.
Thanks
If you're using spring mvc it will be in the war src/main/webapp/WEB-INF/ directory. Also if you're using spring you may not even need an ear file, you should read up on that to be sure you're not adding unnecessary complexity.