0

First: Sorry for the title but i was really not sure how to frase the question.

I have a maven project with several modules in it. All the modules are really simple as they provide some special logic classes to the main Web-App module. These logic classes are injected at runtime by spring.

Following is my Maven setup (abstracted for simplicity)

  • core-module (provides interfaces and basic classes for all modules)
  • algorithm-modules (modules which provide special algorithms, implement one of code-modules interfaces)
  • web-app-module (Spring Web App, which uses the algorithms that register themselves as @Component, is dependent on core and algorithm modules)

Now the Problem: The algorithm module has a resource - in this case a python script - which i want to execute on the systems Python interpreter. (resource/pythonscript.py)

All available Algorithm Classes (Implement Metric interface) are injected by spring in to the web-apps controller:

@Autowired
List<Metric> metrics;

Later the user chooses the algorithm he wants to execute:

metric.performCalculations(dataset);

This executes the logic that later tries to run the python script from the modules resource directory:

URL url = NXBetweennessCentrality.class.getResource("bc/betweenness_centrality.py");
logger.debug("URL: {}", url); //=> null

I tried sevaral combinations:

class.getResourceAsStream(resourcePath)
class.getClassLoader().getResources("")

basically everything from How do I load a file from resource folder? But i can't load this resource.

I heard of problems when trying to access resources of a maven dependencys project (Use a dependency's resources?), but in my case i execute the Class from the package which owns the resource - so there should be no problem, at least thats what i thought.

Can somebody point out my mistake? Is this not possible without some tricky workaround?

Sebastian
  • 786
  • 1
  • 8
  • 22

3 Answers3

0

If you run the class standalone then it should work. Else if you running as webapp using war then your resources should also get bundled inside your war. Since you have multiple modules, you might be packaging the modules which have your python resources as JAR and could have added that dependency to webapp module. At this point your resources which present the JAR module wont get bundled inside war.

  • So the only solution would be to repackege the resources from the dependency into the web-app module during the build? (Like in the thread i linked to?) – Sebastian Jun 27 '17 at 07:52
  • Yes. Either try to use some assembly plugin. Else Keep the python scripts in a common location in server where your webapp runs and load it runtime by passing the location of python scripts in VM arguments – codespawner Jun 27 '17 at 09:20
0

ex:

a.b.c.NXBetweennessCentrality.class.getResource("");

that's mean NXBetweennessCentrality packge
WEB-INF/classes/a/b/c

in your code url is null

URL url =a.b.c.NXBetweennessCentrality.class.getResource("bc/betweenness_centrality.py");

meaning
WEB-INF/classes/a/b/c/bc/betweenness_centrality.py

if your py file under the resources

you can try root "/" path in your web application

getResource("/bc/betweenness_centrality.py");

Correct me if I’m wrong ,thx.

ruc
  • 1
  • 4
  • the root path would not look inside the packege but rather in the root of your hdd (but i tested it anyway) The script is located like you mentioned in target\classes\bc (but not in the web-module, so not WEB-INF) – Sebastian Jun 27 '17 at 08:14
0

So i managed to make it work using the maven script from https://stackoverflow.com/a/6448530/760952

Now all resources from all sub-modules are copied to the web-app resources directory before packaging the war.

This is ok for me, because all the modules can keep their own resources and you don't need to configure anything when adding new resources or new modules.

Sebastian
  • 786
  • 1
  • 8
  • 22