Access file present in /test/resources - classpath issue - java

I'm working on a Spring multi-module project. One of the child modules has some files under /test/resources/certs/ and a property file under /test/resources/test-ssl.properties.
───resources
│ test-ssl.properties
├───certs
│ test-keystore.p12
test-ssl.properties has a property that points to /certs/test-keystore.p12file.
server.ssl.trust-store=/certs/test-keystore.p12
In child modules pom.xml I'm using Maven plugin test-jar and in parent pom I've added this module as a dependency.
With this structure integration test present in parent module is able to successfully read classpath:test-ssl.properties but it fails to resolve its property value.
Spring throws FileNotFoundException: \certs\test-keystore.p12. What change we can do to make Spring read a file present in test jar?
Also tried the following patterns,
server.ssl.trust-store=classpath:/certs/test-keystore.p12
server.ssl.trust-store=classpath:certs/test-keystore.p12
server.ssl.trust-store=classpath*:/certs/test-keystore.p12
Please note that this test property doesn't try to load any certificate. It is there because property placeholder can find some value for the property during build.

Issue is resolved by changing integration-test phase to process-test-resources.
Credit goes to the following answer of Pascal Thivent:
The content of the test output directory (target/test-classes) is on the class path, not src/test/resources. But resources under src/test/resources are copied to the test output directory by the resources:testResources goal (which is bound by default to the process-test-resources phase).

Related

quarkus dev build failes with customized outputDirectory in pom.xml of dependend module

I started with a quarkus multi maven-module project and encountered the following problem when starting quarkus in the dev mode (mvn quarkus:dev) to support hot reload for depended modules' classes:
A module "my-app" configures its' build output path via the
<project><build><outputDirectory>build</outputDirectory> in the pom.xml. The module is build successfully, and the output exists in path my-app/build instead of the default target/classes.
Then the build of another module "momsServerQuarkus", which depends on the "my-app", and which uses quarkus-maven-plugin, fails with error:
[ERROR] Failed to execute goal io.quarkus:quarkus-maven-plugin:2.11.0.Final:dev (default-cli) on project momsServerQuarkus: Failed to run: Hot reloadable dependency de.kwsoft.moms:my-app:6.14.0-SNAPSHOT has not been compiled yet (the classes directory S:\trunk\moms\my-app\target\build does not exist)
As visible in the error message, the <outputDirectory>build</outputDirectory>is read correctly from the my-apps' pom.xml, but is appended to the default output path "target" instead of the projects root folder. I played also variables, e.g. <outputDirectory>${basedir}/build</outputDirectory>, but the variable is not resolved by the quarkus-maven-plugin.
Does anyone know how this could be solved, or whether this could be a bug?
Thank you in advance.

how to filter resources in dependency jar using maven?

I have a project named A, and there is a spring-config.xml and a config.properties in A.  Project A dependency project B, and project B also have a resource file spring-datasource.xml.I want to filter resource during package project A ,so I use maven-resource-filter.But it can only filter the resource file in Project A, doesn't work for project B. How can i filter project B's resource file during package project A?
You cannot (and should not) filter an already packaged dependency.
Filtering is enabled only during the build of the project.
More precisely, during the process-resources phase.
To achieve what you want, you have two ways :
moving spring-datasource.xml in the A project
OR
filtering spring-datasource.xml directly in the B project build.

Maven - access properties file from dependency

Expanding upon this question, if using a properties file in the dependency is the original question possible?
Usually you would do the following:
Create a pom-packaged project, using it as a parent pom
in this parent pom, read the property files you want to read using the Maven properties plugin by binding it's read-project-properties goal to the validate phase.
Refer to this parent pom in all the poms that shall share the properties read.

Maven modules and Spring test resources

I have a Maven project made up of several modules. Some of the modules depend on the other modules for example Module C <- Module B <- Module A. Module C depends on Module B which depends on Module A.
In each module, I have Spring config files in main/resources and test/resources, those under test are for unit testing, while the those under main are for release/production. Each config file is self contained - Module B contained only its Spring config (file names are like so foo-B.xml, foo-A.xml)
However, when I need to test Module C, I need to reference Module B's Spring config under test/resources, but what is included is Module B's main/resources config file. This presents a problem because the production file has references to JNDI datasources where test one does not.
How can I get Maven or Spring to reference the test configuration file from the module dependency?
Maven separates the source classes & resources from the test classes & resources. You may configure Module B to create a test jar using the maven-jar-plugin test-jar goal. Then, you may have Module C reference Module B's test code as a dependency.
<dependency>
<groupId>com.myCompany</groupId>
<artifactId>moduleB</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
Alternately, you can create a regular Maven project including only the test code you'd like to share, then include that as a test dependency where needed. This idea is described in the maven jar plugin's usage docs.
I'm not sure this can be done. Maven deliberately does not include test resources in artifacts. If I were in your place, I would duplicate the test resources in module C. Presumably, you're not testing the same things in both modules, so hopefully it won't cause a bad case of copy&paste/dual-maintenance.
As an aside, I try to avoid having "production" data sources and "test" data sources. Use the same JNDI name for both, but have the JNDI provider configured to point to test or production based on the circumstance. For example, all of our web servers have the same data sources defined, but the JDBC urls are different for dev/qa/prod. For your unit tests, use something like simple-jndi to simulate a JNDI environment.

Classloader.getResources() in maven project returns an empty Enumeration

I have some code which gets resources as follows:
public static final String CONVERTER_FILE = "META-INF/jumbo-converters";
static {
ClassLoader ldr = Thread.currentThread().getContextClassLoader();
Enumeration<URL> e = ldr.getResources(CONVERTER_FILE);
but when I run this in a JUnit test (Java 6) the enumeration is empty. There are a number of subprojects in the project which have the following file:
myProject/mySubProject/src/main/resources/META-INF/jumbo-converters
and I believe that it has worked in the past. What does getResources do? and how can I debug its current failure?
To load the file using ClassLoader.getResources() it has to be found on your classpath. The solution would be to make sure that the parent folder to META-INF is in the classpath.
The resources for a module (sub-project) will not be added to the classpath of the aggregating project unless the aggregating project has a dependency on the module. Being a module to a project does not establish a dependency relationship in either direction.
In your case above you should add a dependency on mySubProject to myProject. Thus making myProject depend on mySubProject.

Categories

Resources