Maven Failsafe Integration Test Classpath - spring

I have an spring application which reads properties file content from etcd server and then writes the content of that into applications properties file. After that I use that properties file to load my propertyManager.
Code implementation is done but when I try to create integration-tests, there is an small problem:
I use MyClass.class.getResource(configFilePath) to get URL of my properties file and since it points to a file inside of a JAR file while working integration tests. I can't write what I read from etcd server to my properties file since its in JAR file(under target) now.
I tried to add additionalClasspathElement ${project.build.directory}\conf to my classpath and use properties file inside of that folder but its not working.
Is there a way for me to change classpath of integration test via failsafe plugin?
Note: Springs etcd support is not want I want to use. I want to change my classpath when I run my integration tests.

Related

Spring Boot - how to include src/main/resources sub-directory to classpath

I have file src/main/resources/dev/important.properties where in fact 'dev' is one of Spring profile names I use.
Somewhere in application this file is referenced as:
#PropertySource(value = "classpath:important.properties")
When I run in Intellij:
mvn spring-boot:run -Dspring-boot.run.profiles=dev
I get FileNotFoundException upon application start. If I move file one level up (to src/main/resources) it works. I can say no surprise here. How to add src/main/resources/dev to classpath so this file would be visible in this directory? This stuff is needed only for local development (Maven+Intellij and dev profile) and it's excluded from runnable jar. I do not want to modify #PropertySource expression.

Could not open ServletContext resource in Spring boot application [duplicate]

I have a Spring Boot application using Google Pub Sub API. I need to inject Google credentials and other properties using file credentials.json. I put the file in my src/main/resources (otherwise, it will not put the file in the built jar) like this:
spring.cloud.gcp.credentials.location=file:src/main/resources/credentials.json
However, when I build the jar, this file is placed in the root directory and this path is no longer valid. So I am able to run my application from Eclipse, since by the that time, the file is still in my resources directory but I can't run it as a standalone jar after built because the path is suddently just file:credentials.json.
Is there some easy way how to specify the path as relative so it works both in IDE and when running my jar? I can inject the path through env. variables but I would do so only if absolutely necessary.
If you use the classpath prefix then Spring will look for the file on your classapth.
If you put the file in src/main/resources then Maven will, by default, copy it to the root of your classpath and it will then be addressable as follows:
spring.cloud.gcp.credentials.location=classpath:credentials.json
This should hold true whether ...
You are running in your IDE; your IDE's Maven integration will copy the file from src/main/resources to the root of your classpath - typically target/classes
You are running a built JAR; Maven will copy the file from src/main/resources to the root of your JAR

How do I get a gradle property into intellij while debugging?

I write test automation. I'm using Log4j2 in my gradle project, and I do development with Intellij. My automation runs at the end of a build. And everything that happens when the automation runs is logged using Log4j2. I pass in the properties that Log4j2 needs via the build.gradle file. The config properties that I pass to Log4j2 are the location of the config file, where to put the output log file, and what logging level I want. But when I'm writing tests and running them manually in intellij, it's like I'm short-circuiting the build.gradle file, and I get no properties and no logging when I do. How do I fix that?

Springboot externalizing log4j configuration

In a springboot application, I have a single jar, and then a subdirectory config with application.properties, applicationContext.xml, and log4j...properties file.
I am trying to externalize the log4j config. The application.properties is externalized this way.
But, when springboot runs it is using the log4j config file from within the jar file. By using the -Dlog4j.debug option I can see that log4j uses my external file first, but when spring starts it overrides the log4j setting with the one in the jar.
here is an example startup (with all options)
java -Dlog4j.debug
-Dlogging.config="file:/opt/config/log4j-qa.properties"
-Dlog4j.configuration="file:/opt/config/log4j-qa.properties"
-jar /opt/myjarName.jar
--spring.config.location=/opt/config/
on first startup log4j states
log4j: Reading configuration from URL file:/opt/config/log4j-qa.properties
then on springboot start
log4j: Reading configuration from URL jar:file:/opt/dms-events-api.jar!/log4j-qa.properties
but I want it to read only the external file file:/opt/config/log4j-qa.properties
resolution:
In our application we had line
#ImportResource("classpath:applicationContext.xml")
which then defined the log4j properties file from the classpath:
the simple solution
1. create a /config directory at the root of the api application and put the properties files there
2. remove the ImportResource line, it isn't needed now
3. add a line to the the application.properties file
logging.config=file:config/log4j-${our environment var}.properties
the explanation
By creating a /config directory at the root of the project then
we can work in eclipse as usual and find our properties files.
--and then to externalize configs
simply add a config directory off of where the application jar is and put properties files there.
Two problems are there:
Configuration for externalise: - Tried and works below one for me in spring boot jar
-Dlog4j.configuration=file:/Users/test/Any-Folder/log4j.properties
Spring logging takes over - for that you need to exclude the logging module. PFB the config for Gradle build.
configurations {
all*.exclude module : 'spring-boot-starter-logging'
}

In a Maven project, how can you configure the JUnit tests to use the project's own build artifact as a Java agent?

I've created a Maven project. When the project is built, it produces a JAR file that can be used as a Java agent. I have written tests and included them in the project. The tests make sure the Java agent works. In order for the tests to use the Java agent, the JAR file needs to have already been built and an argument needs to be passed to the VM that includes the location of the JAR file. Is this possible with Maven? If so, how?

Resources