0

I have two separate maven projects in which my first project is executing the code and generating some data that data I am writing into a file and this file should be created in Project2 under src/test/java/feature folder location.

**Project1**

public class Dependency {

public void execute() throws MojoExecutionException, MojoFailureException {
    
    System.out.println(".... execution begins...."); 
    GenerateData ob=new GenerateData();
    ob.getData();
    String fileLoc="./src/test/java/feature";(location of src/test/java/feature of Proj2 is          `given here)`
    File file=new File(fileLoc + ".feature");
    file.createNewFile();
}

**Project2**

src/test/java
 -feature

Is there a way that I can give the directory path of src/test/java/feature folder of Project2 in Project1 so that my file would get created in Project2.

  • You can either add the absolute path to Project 1 if you don't plan to share the program on to other systems nor change folder locations (brittle but quick and easy approach) or you can look into [file finding tutorials](https://docs.oracle.com/javase/tutorial/essential/io/find.html) if you do plan on having it on other systems. – Tim Hunter Aug 03 '20 at 16:23
  • I suppose another way would be to have Project 1 [use a File Browser](https://stackoverflow.com/questions/796743/how-do-i-add-a-file-browser-inside-my-java-application) so you can manually tell it where to put the file initially and then save that location into a local file inside Project 1 if you want it to just remember your original selection. Not sure how flexible of a approach you're looking for. – Tim Hunter Aug 03 '20 at 16:33
  • no folder location wont change ...it would be the same src/test/java/feature everytime and created file would go inside feature folder only. But how shall i give absolute path of a folder which is in another project like in my case src/test/java/feature – John Martin Aug 03 '20 at 16:34
  • Quick and dirty approach would be to open the folder in File Explorer, use the bar at the top to copy the absolute file path, and then paste it into a String in your program then just use that String as your file write location. – Tim Hunter Aug 03 '20 at 16:36
  • 1
    The idiomatic way would be to add "Project 1" as dependency with scope test to "Project 2" and pull your feature file from that dependency. Generating into some other projects src/ folder is IMHO "working against the tools" – Gyro Gearless Aug 03 '20 at 16:37
  • @TimHunter myprojectname along with src/test/java/feature would remain constant but drive can get changed like C:/ or H:/ .... so I need to put a path in such a way that drive can get changed but other parts would remain constant – John Martin Aug 03 '20 at 16:56
  • That means a folder change then from the absolute path's perspective (I may be using the wrong wording for the concept here). Not sure what infrastructure you're working with, so depending on which would be better for you I recommend looking both the way @GyroGearless mentioned and the file finding tutorial I linked above then decide which approach would work better for you. – Tim Hunter Aug 03 '20 at 17:02
  • will moving a file or copy a file wont work using Files class? So Project1 creates my file which I can store in a folder inside Project1 and then copy/move this file now to Project2 folder (src/test/java/features) – John Martin Aug 03 '20 at 20:24

0 Answers0