1

I have two projects: A and B. Project B has a maven plugin, and when this is executed it should take some templates, which are resources of project A.

The templates are located in C:/path/to/project/A/module1/src/main/resources/templates.

The method that retrieves these templates uses velocity engine and the method is in a class located in C:/path/to/project/A/module1/src/main/java. I use the following path for the templates:

String path = new File( "" ).getAbsolutePath() + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + "templates" + File.separator;

In my unit tests this path is equal to C:/path/to/project/A/module1/src/main/resources/templates, but when I run the maven plugin from Project B, this path is equal to C:/path/to/project/B/module1/src/main/resources/templates. I realize this is because I launch from project B. I know if I put my templates in project B that it works but that's not what I want.

What I would like to achieve is to let the path always be equal to C:/path/to/project/A/module1/src/main/resources/templates. However, since other people are also using this project and their path to project A is most likely different from mine I cannot just do

String path = "C:/path/to/project/A/module1/src/main/resources/templates".

So I would like to keep the first part in such a way that the path to project A is found, followed by "src/main/resources/templates". Is this possible and if yes, how?

EDIT To clarify: what I want is to get the path to project A from a method A1 that is inside project A. And this path to project A should also be found when I run method A1 from project B. The problem that I have with how I am retrieving the path right now is that when I run method A1 from project B then I get the path to project B.

SOLUTION It is working when I use the ClasspathResourceLoader (instead of the FileResourceLoader).

6
  • 1
    Unrelated, but: the new File(""). + .. + .. + thing can be simplified to new File("src/main/resources/templates") - Java can deal with a / separator even if running on Windows Commented Nov 12, 2019 at 13:38
  • Does this need to actually be a filesystem file? In general best practice for java is to reference things via classpath resources. Answers to this question are relevant/useful: stackoverflow.com/questions/9063296/… Commented Nov 12, 2019 at 13:40
  • Are you asking how to find the path to project A, or how to add "src/main/resources/templates" to a path? Commented Nov 12, 2019 at 13:46
  • When you run methodA1 from project B, you are probably running a .class file stored in a jar file. It won't be the source directory. Commented Nov 12, 2019 at 14:28
  • @DodgyCodeException yes I think that is indeed the case. When I try PauMAVA's suggestion below, the path that I'm getting is the path to the jar file. Moreover, I see that my templates are inside this jar file as well. So I was thinking I might be able to access my templates in there if there is no easier way. Commented Nov 12, 2019 at 14:36

2 Answers 2

1

I did not completely unterstand your questions but getting dir-parts of a Path is possible when using class Path.

Have a look at

Path java.nio.file.Path.getName(int index) int java.nio.file.Path.getNameCount() 
Sign up to request clarification or add additional context in comments.

Comments

1

I think that putting this into your project A class should do the trick:

String projectApath = MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String path = projectApath + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + "templates" + File.separator; 

MyClass must be replaced by the class name where the method is in.

This will point to your Maven Local repository so you will have to export Project A with the templates, so that when you load it on Project B the templates will be extracted to your Local Maven Repo. This can be accomplished via ClasspathResourceLoader.

4 Comments

Thanks for the suggestion. When I use this it is working for the unit tests but when I run the plugin from Project B, then I see the path is not my path to Project A anymore but a path in my maven repositories, so then it fails.
If I am not misunderstanding, the Project A path points to your local Maven repo as you have imported it on Project B via Maven Dependency tools right? If that's the case, the only idea I have is that you pack the templates with the code so that when you import via Maven the templates are put into you Maven Local repo.
Yes that's correct. I packed the templates with the code and used the ClasspathResourceLoader to make it work.
Let me edit my answer, so that other users know how to approach this issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.