How to load another module's resource?

Hi guys!

I am trying to load a resource file available in the Reporting module so I can use it in my own module. More precisely I am trying to load a XML test dataset (ReportTestDataset-openmrs-2.0.xml).

My api/pom.xml does depend on the Reporting module (Reporting Compatibility dependency was required too):

<dependency>
  <groupId>org.openmrs.module</groupId>
  <artifactId>reporting-api-tests</artifactId>
  <version>1.14.0-SNAPSHOT</version>
  <scope>test</scope>
</dependency> 

<dependency>
  <groupId>org.openmrs.module</groupId>
  <artifactId>reportingcompatibility-api</artifactId>
  <version>2.0.4</version>
  <scope>test</scope>
</dependency> 

I have tried:

public static final String XML_REPORT_DATASET = "ReportTestDataset-openmrs-2.0.xml";
public static final String XML_REPORT_DATASET_PATH = "org/openmrs/module/reporting/include/";
executeDataSet(XML_REPORT_DATASET_PATH + XML_REPORT_DATASET);

with no luck =>

Unable to find ‘org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.0.xml’ in the classpath

as well as something like:

InputStream is = OpenmrsClassLoader.getInstance().getResourceAsStream(XML_REPORT_DATASET_PATH + XML_REPORT_DATASET);

or

URL url = OpenmrsClassLoader.getInstance().getResource(XML_REPORT_DATASET_PATH + XML_REPORT_DATASET);

But both cases return null.

Any example someone could point me to?

Thank you

Romain

1 Like

Try using

<classifier>tests</classifier>

The dependency you have specified does not have the xml you need. See JFrog

<dependency>
    <groupId>org.openmrs.module</groupId>
    <artifactId>reporting-api-tests</artifactId>
    <version>0.10.1</version>
    <classifier>tests</classifier>
</dependency>

I think the more correct solution is to instead specify the type as test-jar to rule out any other related obscure issues that could arise. The reason behind is, what you want to be included on the classpath is the test jar file rather than the regular jar file for the reporting-api-tests sub module that is getting pulled in based on your configuration, as @vinay pointed out it, it turns out to actually be an empty archive since there is either no or nothing in the src/main/java source folder of that module’s as seen in it’s project structure.

Thanks. Loading the dataset works just fine now.

So here is the pom dependency:

...
<dependency>
  <groupId>org.openmrs.module</groupId>
  <artifactId>reporting-api-tests</artifactId>
  <version>${reportingVersion}</version>
  <scope>test</scope>
  <type>test-jar</type>
</dependency> 
...
1 Like