Classpath root pointing to grails-app/views - how to amend? - gradle

My goal is to add some Json files as project resources accessible from the code. The solution must work in IntelliJ and deployed in a Tomcat. I don't mind if the files are located under grails-app/conf or src.
Getting my classpath root using one of
grailsApplication.parentContext.getResource("classpath:") or
grailsApplication.parentContext.getResource("classpath:.")
resolves in $MY_APP/grails-app/views
However, trying to navigate from this root to grails-app/src using
grailsApplication.parentContext.getResource("classpath:..")
results in: "java.io.FileNotFoundException: class path resource [..] cannot be resolved to URL because it does not exist". This might be acceptable since a root is a root, I guess.
How can I extend my classpath to folders outside of grails-app/views?
How can I make sure that the solution works in both, IntelliJ and Tomcat deployment?
I'm using Gradle 3.5, Grails 3.3.11, Groovy 2.4.10. I'm working in IntelliJ. Rebuilding the project using the Gradle view has not helped so far. The project setup consists of
grails-app/conf/application.yml
grails-app/conf/application.groovy
grails-app/conf/ApplicationResources.groovy (I've tried to add a resource entry here and rebuild the project)
... (Please feel free to have a look in the project in case of further config detail questions.)
I've been exploring endless questions and answers of similar "how can I add my file to the classpath" questions, but nothing has helped so far.

As mentioned earlier in the comment, that you should put the JSON files under /src/main/resources folder. And, you should be able to access the files as:
getClass().getResource("/example.json")
Here is an example application

#PuneetBehl's answer helped me realizing that it isn't about the project code configuration. It is about the configuration of my IDE, which is IntelliJ. You can set Project Structure... --> Project Settings --> Modules. There, "Resource Folders" on the right hand side was listing as: "conf", "i18n" and "views". I dont know why IntelliJ chose "views" for the path "/.".
Anyway, it was enough to:
use Puneet's proposed syntax: getClass().getResource("/myfolder/example.json")
after I had created grails-app/conf/myfolder/example.json

Related

Setup SpringBoot in IDEA from Initializr

I don't understand what happens (before everything worked, feels like I've lost some settings somehow). I download archive from initializer (spring.io; doesn't matter which dependency include, let's say only Spring Web), extract from archive and open project from IDEA. First of all, I'm receiving file ***.iml outside of the container (before it is automatically was created inside)
Also now I have to Assign to "java" folder MarkDirectoryAs - SourceRoot by hand (before this staff applied authomatcally).
And finally looks like IDEA does not see the itself downloaded dependencies. When the project just opened I receive this:
How can I fix it?
UPD:
Errors after reload
And Libraries do not have Maven...
Also what I've found out, I have an error with link to local repository and do not have link to external (like in previous projects)
don't know how to fix it.
Also in my Maven only Lifecycle folder presented now.
Maven Home Path choose as shown on the pic. (Settings->Build,Execution,Deployment->Build Tools->Maven)

How to get IntelliJ IDEA run configuration to make use of Maven filtered resources?

I'm having some trouble working out how to run a project in IntelliJ IDEA that makes use of Maven resource filtering.
In my src/main/resources directory I have
spring.xml file that is used to start the application
service.env.properties
I also have the following in src/main/filters
env-uat.properties
env-prd.properties
env-dev.properties
In my pom.xml file I use maven-resources-plugin to create three configuration directories that get included into the final package. Each one is based on one of the filers. This package can then be deployed on any of the three environments without having to be rebuilt.
The issue that I'm having is when I try to run this using IntelliJ IDEA run configs I have to use
-Xbootclasspath/a:target/config/dev;
to force IntelliJ IDEA to pick up the .properties file with the correct filter.
While this works it doesn't seem like it is the right way to go here. Does anyone have any advice on how to handle configuration for multiple environments and how to get IntelliJ IDEA to pick this up through run configurations.

Spring STS not recognizing project structure with Gradle

I'm trying to do this getting started guide from Spring. So I cloned the project using git from the command line. As I'm working with Gradle, I removed the Maven files from the repository to clean the project.
After that I open my STS and go to File -> Open Projects from File System and navigate to the /initial folder of the repository. The project is imported correctly but when I try to add some "java stuff" (packages, classes, etc.) I get several errors and I can only add folders and files, which is quite annoying for working.
To be more descriptive, I'll add some images so you can see what's happening. This is the project initially imported into STS as I described above:
Then I try to add some java stuff, let's say for example a source folder. So Right Click on library -> New -> Source folder and this is the result:
And in the properties of library I have the following:
So the question is: how can I tell to STS to recognize the library folder as a Java Project (or Gradle module, I don't know how to call it) instead of a plain folder so I can add my packages and classes?
Thanks in advance for your answers.
In case you would like to work with Gradle, you should use the Gradle integration for Eclipse (project Buildship) and install that into your STS environment. After that, you should be able to import the projects either from disc as existing Gradle project (not as file system) or via the wizard that integrates the spring guides into STS (Import Getting Started Content). The wizard for the guides also let you choose between the Maven and the Gradle variant, so you don't need to close and delete stuff yourself.
Hope this helps!!!

Maven war plugin copy arbitrary files

I apologize that this is surely basic maven/war plugin stuff, but I'm totally not a maven user. I just have to hack this one thing into a project that is maven based.
What I need to do is to copy an (essentially arbitrary) directory and files into the root of my war. I need them to appear as resources available via HTTP when the war deploys.
I cannot simply put them in the "right place" in the source tree. This is because the files in question are actually the source files of my project. I realize this is a bit odd, but this is a documentation project, and I need to show the source and the effect all in the same war.
So, in the general case, how would I configure the maven war plugin to copy a given directory, along with it's contents, to the root of my war? (BTW, I have tried to make sense of the documentation of this tool, but it seems to be predicated on so much understanding of maven that it feels like I'll never understand it without learning maven first, and I'm a bit too pressed for time to do that just now!)
Many TIA
Toby.
You could try:
use the copy-resources plugin. Use the plugin to copy your source files under target prior to when the war is packaged.
or, configure the maven-war-plugin to include additional resources.
Thanks Drew, that got me where I needed to go. Bottom line, I added the sample pom fragment from the link you gave for copy-resources to my pom.xml with the following changes:
<outputDirectory>target/${project.name}-${project.version}/sources ...
<directory>src/main/java ...
I copied this from the link you gave, then edited the element to point at src/main/java, which picked up my files, and the outputDirectory to the aggregate target/${project.name}-${project.version}/sources . I found that ${project.name} mapped to the project, and the version came from ${project.version}, which seemed to resolve the last little bits of issue.
Thanks again.
Toby

Eclipse Juno m2ecliipse : directory structure explanation

I'm working under Eclipse Juno on a maven-archetype-webapp project done with m2eclipse.
I don't understand directory structure. Anyone could explain how it's working?
Thanks
What i src/main/ressources for ? When you create package, it's not showed as package but only directory.
I create a /src/main/java package and it's where i put my classes. The package structure hierarchy is ok.
What is librairies ressources? Is it where you put javascript jquery? but when i put jquery plugin it does a lot of effor in the project manager. And i want my javascript under /webapp/javascript... What is ECMAScript Library ? What to do with that, what is for?
Same question about Deployed Ressources and target and Jax-WS Web Services ?
Ok target must be the directory when you build the project maven, but i can't see the war file. When you run build how to configure it in eclipse to generate the build? What is the command ?
Thanks , i know it's a lot of questions but i'm beginner !
Have a great day :)
I just add the example when you put jquery in webapp
All of this is explained here.
src/main/java This is where your source code goes
src/main/resources This is where you put your resources like your xml files. Things that aren't java classes but need to be in your classpath. If you put them under /java, they won't be copied into your classpath so you have to put them here.
src/main/webapp This is for webapps. Everything you put in here will be copied over to your war file, directly.

Resources