I have the following folder structure in my Spring application:
src
\__main
\ \__resources
\_test
How can make use of the files in resources folder while unit testing the application? In otherwords, how can I load the files in src/main/resources folder into test-classes which gets created while executing tests?
You can create another folder by the same name, i.e "resources" under the test package. This is a standard resource folder under Maven. All the files under this folder is accessible to the test classes under the test package during runtime.
Related
I have a legacy app that I'm porting from Ant to Maven. My Maven build works fine for the main project, which I've moved into the standard Maven directory layout (*.java files in /src/main/java/) and it outputs the compiled classes into /target/classes/ as neat as you could wish. These are packaged in a .war file.
However, the project also has a class outside of the folder hierarchy, indeed outside of the web application, that contains scripts that run via cron job. Let's say it's /cronjobs/MyClass.java. I need that class to be compiled and output to /target/cronjobs/MyClass.class and zipped up as part of the resulting .war file, in its /cronjobs/ folder.
Can Maven do this? I know it's possible to change the default "src" directory and "target" directory, but I don't know if (or how) it's possible to run a separate, parallel compile step for just one class.
I can move the source file, of course, if it's easier to compile it with the other classes and then move it later (maybe with the WAR plugin?) but I definitely need the compiled MyClass.class file in the /cronjobs/ directory of the .war.
I'd split the project in 2 parts, webapp as war and cronjobs as jar. Maven knows about multi-module format and it is somewhat the best way to go forward and decouple the webapp from non-webapp code.
I am trying to create a maven-plugin that generates new files based on a template file (basically using the FreeMarker language). I can successfully generate the files if I run the maven plugin from the directory of my maven-plugin project since my plugin accesses the template file based on a relative path.
However, if I try to run the plugin in some other java project directory, I cannot find the template file. I do not want to copy the templated file into the new java project.
I searched around to see if the maven plugin can access files within itself when another project is using the plugin but wasn't successful. Most of the documentation refers to accessing the java project files, and not the maven-plugin files.
Is this even possible or is there a better approach/workaround to tackle the problem?
Edit (Directory structure):
Maven-Plugin (FileGenerator):
src
--main
--java
--FileGenerator.java (references the TemplateFile based on the path to the root project)
TemplateFile
If I run the plugin as a standalone, I am able to generate new files based on the TemplateFile. I achieve the following structure
Maven-Plugin (FileGenerator):
src
--main
--java
--FileGenerator.java
TemplateFile
NewFile1
NewFile2
However, if I run the plugin in another directory (such as part of another java project) with the command mvn myplugin:mypluginplugin:1.0-SNAPSHOT:build
Another Java Project
src
--main
--java
--AnotherFile.java
pom.xml
I get an error mentioning that the TemplateFile cannot be found. Is there a way for the plugin to reference the TemplateFile regardless of where it is run at?
Ideal Output After running plugin
Another Java Project
src
--main
--java
--AnotherFile.java
pom.xml
NewFile1
NewFile2
Please guide since i wanted to understand on the build tool maven 'directory structure' intelli j as IDE where main/test divided into two separate folders but both have the same files.
Ps- I recently moved into QA automation so want to understand it better.
FYI per maven main website:
a. The main directory is the root directory for source code related to the application itself, not test code.
b. The test directory contains the test source code.
Maven search for the files in the below directory structure.
src/main/java - Source Java files
src/main/resources - Source resource files
src/test/java - Test cases
src/test/resources - Test resource files
Maybe this site would be helpful to you: https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
Maven uses a strict folderstructure. Are you sure, that there are the same files in the folders? As Bhargav Kumar R mentioned before, in the test-folder there will be your test-classes, in the resource-folder there will be the resources for your tests.
I've followed the guide here for turning a "hello, world" level Spring Boot app to a war file. I can run this war like a jar and it will return the simple template.
What I don't understand is why I can't access a main.css file I've created. I've placed it in the resources directory under "static/css/main.css" and according to the docs here Spring Boot will automatically server files under "resources", "static", "public", and "META-INF/resources". However, when I build my war file and run it I can't query those files in the browser (like http://localhost:8080/static/css/main.css). Have a missed a step? If I peek into the created war file I see the "static" directory in "WEBINF/classes" right beside the "templates" directory and the directory holding my application.
Files in src/main/resources/static are served from / so you don't need static in the path. You CSS file should be available from http://localhost:8080/css/main.css
I have a mavenized project with jar packaging, have standard layout:
/src/main/java
/src/main/resources
When I execute: mvn package
I am expecting the resulting jar (non-executing) file to include the resources folder and everything inside that folder from the project(i.e. some xml files), but when I open the
jar file I only see the com folder where and the contents of the resources folder in the top level...
I think the above is default for maven3, but I want to include the resources folder in the jar file...
You will not see src/main/resources in resulted jar file.
You will only see the contents of src/main/resources in the jar file.
So, if you did not create a package structure for your resource files and dumped everything into src/main/resources directory, you will see your resource files at the top level of your jar.
If you want your resources appear under certain directory in the resulting jar file, create such a directory under src/main/resources tree.