3

I am trying to get the absolute path of a resource generate by an archetype.

https://github.com/DigitalPebble/storm-crawler/blob/master/external/elasticsearch/archetype/src/main/resources/archetype-resources/es-crawler.flux#L25

I tried to replace the line above with

  • "${project.basedir}"
  • "${project.build.directory}"
  • "${basedir}"
  • "${outputDirectoryFile}"
  • "${outputDirectoryFile}/${artifactId}"
  • "${basedir}/${artifactId}"

but none of these worked. The furthest I got was to get the /${artifactId} part right.

The file is listed as filtered in archetype-metadata.xml

Is there a variable I can use to get the absolute path of the directory generated?

Julien Nioche
  • 4,772
  • 1
  • 22
  • 28
  • I don't know enough about flux, but if the `es-crawler.flux` is used at runtime, you should not try to use Maven placeholders, because Maven is only about buildtime. The `.` looks like a good approach, or OS specific, e.g. `echo $(pwd)` (*nix) / `echo %cd%` (Windows) – Robert Scholte Sep 03 '20 at 15:05
  • thanks @RobertScholte. we need an absolute path not a relative one and we can't run a script within Flux. I don't want to use the placeholders at runtime but at build time so that the default value generated points to the absolute path. The archetype generates many other resources at buildtime, can't see why we shouldn't do the same with this one. – Julien Nioche Sep 04 '20 at 07:00
  • I still think the approach is problematic: once an absolute path ends up in your deliverable, you're doomed. – Robert Scholte Sep 04 '20 at 15:54
  • I couldn't fully understand the question. Where do you try to put that variable? To the line 25 of es-crawler.flux? I think checking out maven resources plugin may help https://maven.apache.org/plugins/maven-resources-plugin/index.html Try ${project.build.resources} first. And set the correct resources directory using the plugin if there's any custom directory configuration for your resources – Onur Baştürk Sep 05 '20 at 09:44
  • @RobertScholte 'build time' is probably not the right term. What I meant was generation time, i.e. when a user calls 'mvn archetype:generate' – Julien Nioche Sep 06 '20 at 07:30
  • @OnurBaştürk ${project.build.resources} did not work and remained as is after 'mvn archetype:generate'. – Julien Nioche Sep 07 '20 at 08:30

1 Answers1

-1

Try This:

URL res = getClass().getClassLoader().getResource("abc.txt");
File file = Paths.get(res.toURI()).toFile();
String absolutePath = file.getAbsolutePath();

Credit: https://stackoverflow.com/a/17351116/14212394

Seth
  • 2,214
  • 1
  • 7
  • 21