Freemarker Configuration in a spring boot app - spring

I am migrating a multi module maven project from SpringToolsSuite to IntelliJ Idea Ultimate Edition. I am using freemarker to generate xml files. When I launch the project from STS the template folder is well configured and the template is found and everything works.
But when I run the application from Intellij Idea the template folder is not found by the system.
Here is the error from the stacktrace:
freemarker.template.TemplateNotFoundException: Template not found for name "AnaCredit_Bbk_Riad.ftl".
The name was interpreted by this TemplateLoader: FileTemplateLoader(baseDir="C:\", canonicalBasePath="C:\").
My code to set the directory for template loading is this:
cfg.setDirectoryForTemplateLoading(new File("/"));
These properties exists in application.yml:
spring.freemarker.template-loader-path: /
spring.freemarker.suffix: .ftl
What must be configured to get the same results in both STS and Idea?
Why does it work in SpringToolsSuite but not in Idea?

Related

In an Ejb project java classes form src/main/java can't see classes from src/test/java

Am getting an error that says
Description Resource Path Location Type
The import com.esprit.puzzled.test cannot be resolved TutorialImpl.java /Puzzled/src/main/java/com/esprit/puzzled/services/tutorial line 14 Java Problem
Note that the error wasn't there before I had to format my laptop and completely set up my work environnement again.
I've tried clean project, Update maven project, refresh project, switch workspaces but no dice.
Also Jrebel was working fine before but activating it now gives me an error at jrebel.xml file too.
screen capture

Springboot Strange Resource Loading Behavior

I'm working on a springboot 1.5.1 application that I'm trying to load a WSDL included in my resources directory in the wsdl directory. Depending on where my application executes I'm getting different results (command line, intellij, cloud foundry) and I can't seem to get all three to work at the same time.
I've tried several ways of locating the resource:
From prior to the migration to springboot we had this (worked in IntelliJ but not java -jar myboot.jar):
this.getClass().getResource("/wsdl/my.wsdl");
I switched it to the typically more correct version and got it to work in IntelliJ and java -jar but not Cloud Foundry:
this.getClass().getClassLoader().getResource("/wsdl/my.wsdl");
I switched it to use the Spring Resource Loader version and it worked in IntelliJ and CloudFoundry but not java -jar:
#Value("classpath:/wsdl/my.wsdl")
private Resource wsdlResource;
wsdlResource.getURL();
On the command line what I've noticed is that it seems to be thinking that BOOT-INF/classes is a JAR file (Note the ! after classes):
Caused by: javax.xml.ws.WebServiceException: Failed to access the WSDL at: jar:file:/C:/dev/redacted/build/libs/redacted.jar!/BOOT-INF/classes!/wsdl/my.wsdl. It failed with:
JAR entry BOOT-INF/classes!/wsdl/my.wsdl not found in C:\dev\redacted\build\libs\redacted.jar.
From looking at IntelliJ's URL, it's referring to the actual source folder which explains why it seems to always work.
What is causing this and how might I universally load these class path resources successfully with springboot?

how to set java class loader PARENT_LAST

i have a spring mvc web application that I need to change the class loader on. I need to change the class loader to be equal to PARENT_LAST. I am using WAS 6.1 and already have a jacl script from a previous web application I can copy to do the job.
In the last application Apache ant was used and what they did was to make the deploy dependent on running the jacl script.
In my new web application I am using maven install to create a war file and am deploying that war file to my application server.
How can I set the class loader to be PARENT_LAST using maven? I know how to do it in the console but if there was a way to do it using scripting that would be nice.
Also will this setting be placed somewhere in the war file so that on deploy of the application the setting will be picked up. This question comes from my lack of understanding of how jacl scripts work?
thanks
If you are only deploying the WAR file itself you can't control this, but if you have your WAR file in an EAR file you can use the deployment.xml solution. The deployment.xml file would look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<appdeployment:Deployment xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:appdeployment="http://www.ibm.com/websphere/appserver/schemas/5.0/appdeployment.xmi" xmi:id="Deployment_1347529484613">
<deployedObject xmi:type="appdeployment:ApplicationDeployment" xmi:id="ApplicationDeployment_1347544766353" startingWeight="99" warClassLoaderPolicy="SINGLE">
<modules xmi:type="appdeployment:WebModuleDeployment" xmi:id="WebModuleDeployment_1347543866613" startingWeight="1" uri="YourWebApp.war" classloaderMode="PARENT_LAST"/>
<classloader xmi:id="Classloader_1347543866613" mode="PARENT_LAST"/>
</deployedObject>
</appdeployment:Deployment>
Once you are done all you need to do is to add the file in the correct location of your EAR project build assuming you are using src/main/application that would be src/main/application/META-INF/ibmconfig/cells/defaultCell/applications/defaultApp/deployments/defaultApp/deployment.xml and build the EAR using Maven as normal.
During server deployment this will be picked up by WAS.
AFAIK there is no way to preconfigure WAR for PARENT_LAST during assembly. Classloading policy is specified during deployment, thus the way of setting it depends on how application is deployed.
Changing the policy using the script is straightforward. Scripts are run using wsadmin tool. The Jython snippet below does the job. It can easily be converted to Jacl.
dep = AdminConfig.getid('/Deployment:app_name/')
depObject = AdminConfig.showAttribute(dep, 'deployedObject')
classldr = AdminConfig.showAttribute(depObject, 'classloader')
AdminConfig.modify(classldr, [['mode', 'PARENT_LAST']])
AdminConfig.save()
Websphere uses deployment.xml file to govern deployment setting of each module in an ear file. You can change the classloader setting in deployment.xml at the following path:
/MyTestEAR/META-INF/ibmconfig/cells/defaultCell/applications/defaultApp/deployments/defaultApp/deployment.xml
I do not know how you can configure that in Maven.

TOMCAT 7 not deploying war

I have developed a Dynamic Java Project with Eclipse which involves web services(RESTful). After completion I tried to run my project Tomcat 7 runtime of Eclipse IDE, it works fine. I am able to access all my web services.
But, when I created a WAR file out of my project by right-clicking on project and exporting it to WAR file and placed .war file in Tomcat 7's webapps folder. Started the tomcat by running startup.bat, it throws below exceptions and I am not able t access any web service:
I have placed all the required jar files in lib folder of Tomcat.
Exception fixing docBase for context [/Books] (Books is my war file name)
Failed to create work directory [{CATALINA_HOME}\work\Catalina\localhost\Books]
Failed to create destination directory to copy resources.
Could anyone please let me know what I am missing??
Thanks in advance.
Regards,
Piyush
The failed to create "work directory" error can occur if the work folder does not exist in the tomcat7 folder.
Also this error can be caused by tomcat not having sufficient permissions to read, write and execute in the tomcat7 directory.
The issue is because, tomcat is not finding your web.xml location in the classpath.
Do below steps:
Right click on web project-->Properties-->Deployment Assembly--> Add your classpath to web.xml(src/main/webapp) in my case.

Netbeans Including XML and Properties when building Spring projects with Maven

I am new to Maven and Spring. I'm using Netbeans 7 as my IDE, and setting up a Spring 3 project using Maven.
Everything seemed to set up smoothly, and I began running through the Spring User Guide. However, I'm getting a file not found exception when trying to load my context.xml file.
I have an App class located at com.myproject and the context.xml file is located at com.myproject.conf
I'm using the following line of code in App.java to try and load the context.xml file:
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
But when I run the application, it results in:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [context.xml]; nested exception is java.io.FileNotFoundException: class path resource [context.xml] cannot be opened because it does not exist
Looking at the NetBeans output, it also looks like it's not picking up the log4j.properties file which is also located in com.myproject.conf
I looked at the jar that the build process created, and the entire com.myproject.conf package is missing, meaning the .xml and .properties are missing as well. I've tried moving these config files to the com.myproject package as well as just putting them at the root of the project which don't yield any different results.
So I'm making the assumption that my maven project isn't set up entirely correctly, or maybe a setting isn't correct within NetBeans.
It seems like you need to learn about resources in Maven projects (such as XML, bitmaps, etc...). These are stored in separate directories. See here.
Put the package in "Other Sources"
I have the same problem with a .property file to manage the Internationalization.
I create it in com.company.app.view.resources at the Source Packages directory.
When I build the project and then look at my war file (target).. I don't found the .property file in /WEB-INF/classes/com/company/app/view/resources
Then I put the package in "Other Sources" directory and it works for me.

Resources