- Saved searches
- Use saved searches to filter your results more quickly
- awwsmm/Maven-With-Resources
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- How to organise resource files for tests using Maven and Java
- What is a Resource?
- How to Create a Resource Folder Structure
- How to check if you have organised this properly?
- Summary
- Organizing Resource Files for Tests Using Maven and Java
- what is a resource?
- how to create a resource folder structure
- how to check whether you have organized this properly
- summary
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Easily read files from src/main/resources in your Maven project
awwsmm/Maven-With-Resources
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Using Resource Files in a Maven Project
Run the following command to generate a blank Maven project:
mvn -B archetype:generate \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DgroupId=com.companyname.packagename \ -DartifactId=my-new-package
It will make a new directory called » my-new-package » inside the current directory. Next, make a directory called src/main/resources inside the Maven project directory (inside my-new-package ) and add the following text to a file called example.txt in that directory:
this is an example resource here is a second line and a third one
Remove the files src/test/java/com/companyname/packagename/AppTest.java and src/main/java/com/companyname/packagename/App.java . Replace the text of your pom.xml file with the following text:
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> modelVersion>4.0.0modelVersion> groupId> com.companyname.packagename groupId> artifactId> my-new-package artifactId> packaging> jar packaging> version>1.0version> properties> project.build.sourceEncoding>UTF-8project.build.sourceEncoding> maven.compiler.source>1.8maven.compiler.source> maven.compiler.target>1.8maven.compiler.target> properties> build> plugins> plugin> groupId>org.apache.maven.pluginsgroupId> artifactId>maven-jar-pluginartifactId> version>3.1.0version> configuration> archive> manifest> addClasspath>trueaddClasspath> classpathPrefix>lib/classpathPrefix> mainClass>com.companyname.packagename.MainClassmainClass> manifest> archive> configuration> plugin> plugins> resources> resource> directory>src/main/resourcesdirectory> filtering>truefiltering> resource> resources> build> project>
Finally, create a file at src/main/java/com/companyname/packagename called MainClass.java and paste the following text into it:
package com.companyname.packagename; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.io.UnsupportedEncodingException; public class MainClass < public static void main (String[] args) throws UnsupportedEncodingException, IOException < readResource(); > public static void readResource() throws UnsupportedEncodingException, IOException < InputStream is = MainClass.class.getClassLoader().getResourceAsStream("example.txt"); InputStreamReader isr = new InputStreamReader(is, "UTF-8"); BufferedReader file = new BufferedReader(isr); String line = null; // line read from file while ((line = file.readLine()) != null) System.out.println(line); file.close(); isr.close(); is.close(); > >
Run mvn package in the Maven project directory and wait for the compilation and packaging to finish. Then, run java -jar target/my-new-package-1.0.jar . You should see something like:
$ java -jar target/my-new-package-1.0.jar this is an example resource here is a second line and a third one
Ta-da! Resource read within Maven.
How to organise resource files for tests using Maven and Java
But, they can be hard to wrap your head around. As evidenced by my own experiences trying to use them and the number of queries on stack overflow.
What is a Resource?
A resource is a file in the class path folder structure for your project.
This is important because your test resources will be put in your test-classes folder hierarchy and your main resources will be put in your classes folder hierarchy — both in your target folder.
How to Create a Resource Folder Structure
Maven has a standard directory layout.
We make things easy for ourselves by sticking to this layout.
I can have two obvious resource folder hierarchies:
- src\main\resources — for resources available to my main ’live’ code
- src\test\resources — for resources available to my test code
The folder hierarchy below resources is my package hierarchy.
i.e. if I have a test called LinkCheckerTest and it is in a package called
package com.javafortesters.course.exercises.casestudy.casestudy_002_buildAnHttpLinkChecker;
Appologies for the length, this is from an example in my Face to Face Java For Testers Training Course
The full folder structure for a resource file used by the LinkCheckerTest would be:
- src - test - resources - com - javafortesters - course - exercises - casestudy - casestudy_002_buildAnHttpLinkChecker
I could access a resource in the folder structure from within the LinkCheckerTest using
URL fileToRead = LinkCheckerTest.class.getResource("linksToCheck.txt");
Where linksToCheck.txt is a file in the casestudy_002_buildAnHttpLinkChecker folder listed above.
How to check if you have organised this properly?
Many of the queries on line are about .getResource not finding the file.
getResource looks for the file, relative to the Class in the class hierarchy. This means that if you look in the target folder to find your resource file then you will see if you have it in the correct place.
i.e. if I look in my target folder to find the linksToCheck.txt file and if I have organised everything correctly then the file should be in the same folder as the class.
In the above image I can see that it is, which means that the getResource method earlier will work.
I know this because the files are in the same folder:
- target - test-classes - com - javafortesters - course - exercises - casestudy - casestudy_002_buildAnHttpLinkChecker - LinkCheckerTest.class - linksToCheck.txt
If the linksToCheck.txt file was in a different place in the target folder then I would have to use relative path to access it e.g. I would use ../linksToCheck.txt if I had placed the file in the casestudy folder rather than the casestudy_002_buildAnHttpLinkChecker folder.
Summary
- resources are added to the class path hierarchy
- class path is relative to the package of the class
- resource paths are split between main and test
- use the target folder hierarchy to check if files are in the correct place
If you like this content then you might be interested in my Patreon Community. I create exclusive content multiple times a week. Gain access to Patreon only content and online training courses for as little as $1 per month. Learn more about the EvilTester Patreon Community.
Organizing Resource Files for Tests Using Maven and Java
Join the DZone community and get the full member experience.
resources are a very useful concept in java. they are essentially files in projects that are compiled into your jar. java also has commands for finding them ( .getresource ) and reading them ( .getresourceasstream ). very handy. but they can be hard to wrap your head around, as evidenced by my own experiences trying to use them and the number of queries on stackoverflow.
what is a resource?
a resource is a file in the class path folder structure for your project.
this is important because your test resources will be put in your test-classes folder hierarchy and your main resources will be put in your classes folder hierarchy — both in your target folder.
how to create a resource folder structure
maven has a standard directory layout . we make things easy for ourselves by sticking to this layout.
i can have two obvious resource folder hierarchies:
- src\main\resources for resources available to my main ‘live’ code.
- src\test\resources for resources available to my test code.
the folder hierarchy below the resources is my package hierarchy.
for example, i have a test called linkcheckertest and it is in a package called:
package com.javafortesters.course.exercises.casestudy.casestudy_002_buildanhttplinkchecker;
the full folder structure for a resource file used by the linkcheckertest would be:
- src - test - resources - com - javafortesters - course - exercises - casestudy - casestudy_002_buildanhttplinkchecker
i could access a resource in the folder structure from within the linkcheckertest using:
url filetoread = linkcheckertest.class.getresource("linkstocheck.txt");
here, linkstocheck.txt is a file in the casestudy_002_buildanhttplinkchecker folder listed above.
how to check whether you have organized this properly
many of the queries online are about .getresource not finding the file.
getresource looks for the file relative to the class in the class hierarchy. this means that if you look in the target folder to find your resource file, then you will see if you have it in the correct place.
for example, if i look in my target folder to find the linkstocheck.txt file, if i have organized everything correctly, then the file should be in the same folder as the class.
in the image above, i can see that it is, which means that the getresource method will work.
i know this because the files are in the same folder:
- target - test-classes - com - javafortesters - course - exercises - casestudy - casestudy_002_buildanhttplinkchecker - linkcheckertest.class - linkstocheck.txt
if the linkstocheck.txt file was in a different place in the target folder, then i would have to use the relative path to access it — for example, if i had placed the file in the casestudy folder, rather than the casestudy_002_buildanhttplinkchecker folder.
summary
- resources are added to the class path hierarchy
- class path is relative to the package of the class
- resource paths are split between main and test
- use the target folder hierarchy to check if files are in the correct place
Published at DZone with permission of Alan Richardson , DZone MVB . See the original article here.
Opinions expressed by DZone contributors are their own.