Spring Boot how to add another resources folder in test folder - spring-boot

Is it possible to add another resources folder in test folder, which will also be on the classpath?
I need it because I don't want to add application-test.properties file in default resources folder because it belongs in test folder.
I tried to add folder manually but it does not work.

I soloved this problem, in Intellij IDEA by:
Right clicking on the project -> Projectu structure,
and I marked newlycreated folder as Resources file.

It is gradle project or maven? If you have gradle just add the line below to the build.gradle file:
ext {
resourcesDir = projectDir.path + "/other/resources"
}
where the /other/resources is your dedicated resource folder

Related

Add a file to a specific folder with gradle build

I am currently working with a project (using gradle) that needs a properties file in the same package to the class that consumes it (I can't change this config). I have added the file in my package, but when I execute the gradle build command omits the file, and only adds the .class file. The app is deployed as a war file.
I have tried editing the war task, but I can't find the way to make the properties file to be added:
war {
from('src/main/com/foo/bar') {
include 'b.properties'
into 'WEB-INF/classes/com/foo/bar'
}
}
The war task does execute (I have added a prinln and it executes). Also, I have tried changing the paths, but no results. Also replacing from('path') to from (file('path')), but still doesn't seem to add the file anywhere.
How should I achieve this? Thanks!
Is there anything stopping you from following Gradle's convention for resources?
Eg put your property file at src/main/resources/com/foo/bar/b.properties
If you do this you won't need any task customisations

Gradle: Copy one specific file from main project directory into subproject/build/libs

Searching in vain for examples on this humble task, none worked, I need to learn how to use gradle to copy one specific file from the main directory of a gradle project (containing subprojects) and into the /build/libs folder of one of the subprojects. Structure is this:
mainProject folder
file.txt
Subproject folder
build folder
libs folder
(I want to copy file.txt here)
This copy process shall be in the build.gradle (for the Subproject) file. I am using Android Studio and the subproject is a pure java application.
Thanks for any help.
You need to implement a custom task of type Copy to copy the file to the target directory. To access the file in the root project, you may use the rootProject property of the subproject.
task copyFileFromRootProject(type: Copy) {
from "${rootProject.projectDir}/file.txt"
into 'build/libs'
}

Spring Boot version 2.3.x src/test/resources removed

Seems in the Springboot version 2.3.x , they have removed the src/test/resources folder ,
I downloaded a skeleton from start.spring.io and did not see it , the current project structure is as below , in such a case how do I use a test specific property file , problem is even if I create a src/test/resources folder and put a test.property file in it , its not being found while I am running the test , Any help is greatly appreciated.
Options I chose while generating the skeleton project in start.spring.io
After a fair amount of research I found my answer , so the "resources" folder under src/test has been removed as part of the skeleton project that we could download from start.spring.io , but now they have included a new build task , well my project is in gradle but I am sure such is the case with Maven too , the name of this task in gradle is "processTestResources" , it copies whatever you have inside your src/test/resources folder to the <Project_HOME>/build/resources/test , which is in the classpath and you can access your resources as you could earlier , the only things is now you have to manually create the "resources" folder , now issue resolved , Thanks everyone for your inputs.
What IDE are you using ? Does the project use Maven ? In Eclipse with Maven if you right click on src/test and choose New -> Source Folder, and call it resources it should be created and added to the test classpath by Maven.
After placing the appropriate files in src/main/resources, and src/test/resources and running
try(final Stream<String> fileStreamFromClasspath = Files.lines(Path.of(ClassLoader.getSystemResource("test.file").toURI())))
{
fileStreamFromClasspath.forEach(l -> System.out.println(l)); // NOSONAR
}
try(final Stream<String> fileStreamFromFilesystem = Files.lines(Path.of(("src/main/resources/test.file"))))
{
fileStreamFromFilesystem.forEach(l -> System.out.println(l)); // NOSONAR
}
try(final Stream<String> testFileStreamFromFilesystem = Files.lines(Path.of(("src/test/resources/test.file"))))
{
testFileStreamFromFilesystem.forEach(l -> System.out.println(l)); // NOSONAR
}
Produces output:
This is the test test file.
This is the main test file.
This is the test test file.

projects_default.yml file in thorntail

Thorntail generator does not generate any project_defaults.yml file in project structure. No resources folder also seen.Should we manually place this file to configure project properties?
Note : I am new to thorntail.
Yes, you need to create a file project-defaults.yml inside src/main/resources.

Gradle dependency destination on non-jar config file

I can create a dependency to something other than a jar file like this:
dependencies {
compile files("../other-project/config.txt")
}
The above works fine, except that config.txt ends up in the WEB-INF/lib folder of my war file. Instead I need it to be in WEB-INF/classes in the war file, and in src/main/resources for jettyRun.
How can I control where the dependency ends up? Or am I going about this the wrong way?
I can also solve this with a copy task, but this really is a dependency in that I don't need the file updated unless it changes. An unconditional copy would work, but I'd rather do this the right way.
The war task (as configured by the war plugin) puts dependencies into WEB-INF/lib, the web project's own code/resources into WEB-INF/classes, and web app content (which by default goes into src/main/webapp) into WEB-INF. Other content can be added by explicitly configuring the war task. For example:
war {
into("WEB-INF/classes") {
from "../other-project/config.txt"
}
}
One way to make this work with embedded Jetty (though maybe not the most convenient during development) is to use jettyRunWar instead of jettyRun. Another solution that comes to mind, particularly if the content to be added resides in its own directory, is to declare that directory as an additional resource directory of the web project (sourceSets.main.resources.srcDir "../other-project/someResourceDir"). This is in fact an alternative to configuring the war task. If the web project already has a dependency on the other project, you could instead configure an additional resource directory for that project.
Let's say you have configured a multi-project build with the following directory and file structure:
/combined-war
/main-project
/src
/webapp
/WEB-INF
web.xml
build.gradle
/other-project
/resources
/WEB-INF
/classes
config.txt
build.gradle
build.gradle
In order to allow jettyRun to combine the contents of the webapp directory from main-project with the contents of the resources directory in other-project you need to add a workaround to your build.gradle of main-project (I've adapted the one posted by the user siasia on gist).
Adding the same directory content to the war file is quite simple and is documented in the Gradle User Guide and and the DSL reference.
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'jetty'
import org.gradle.api.plugins.jetty.internal.JettyPluginWebAppContext
def newResourceCollection(File... resources) {
def script = '''
import org.mortbay.resource.ResourceCollection
new ResourceCollection(resources)
'''
def shell = new GroovyShell(JettyPluginWebAppContext.class.classLoader)
shell.setProperty("resources", resources as String[])
return shell.evaluate(script)
}
jettyRun.doFirst {
jettyRun.webAppConfig = new JettyPluginWebAppContext()
jettyRun.webAppConfig.baseResource = newResourceCollection(
// list the folders that should be combined
file(webAppDirName),
file("${project(':other-project').projectDir}/resources")
)
}
war {
from("${project(':other-project').projectDir}/resources")
}
Whenever you execute gradle jettyRun a new ResourceCollection is created that combines the given directories. Per default Jetty locks (at least on Windows) all the files it's serving. So, in case you want to edit those files while Jetty is running take a look at the following solutions.
Update
Since other-project in this case is not another Gradle project the two tasks in build.gradle should look like that:
jettyRun.doFirst {
jettyRun.webAppConfig = new JettyPluginWebAppContext()
jettyRun.webAppConfig.baseResource = newResourceCollection(
file(webAppDirName),
file("$projectDir/../other-project/resources")
)
}
war {
from("$projectDir/../other-project/resources")
}
I'm not aware of any solution that adds only one file (e.g. config.txt). You'll always have to add a complete directory.
As I mentioned above, it's simple enough to do an unconditional copy that solves the problem. Again, not the question I originally asked. But here's my solution that works for both war and jettyRun tasks:
processResources.doFirst {
copy {
from '../other-project/config.txt'
into 'src/main/resources'
}
}

Resources