0

I'm creating Spring MVC application with tomcat and gradle where user can upload some file from frontend and then can make changes with that. I want to upload that file to the project directory (scr/tmp/env). It works if I create directory manually, but i can't push empty packages into git. It means that when someone downloads my project to another computer, he won't have these directories inside the project. For example i can create some class with public static void method and this code:

File file = new File("src/tmp/dbEnv/");
if (!file.exists()) {
  Files.createDirectories(Paths.get("src/tmp/dbEnv/"));
}

It will create directory. But if i paste this code into spring methods (in controller or dao classes for example), directories aren't created.

Could you give me advice, how to create directories in project before starting and clear those before finishing application? May be Spring or Gradle can do it? Thank you.

Alexey
  • 362
  • 1
  • 5
  • 17

4 Answers4

1

You can use application listeners to tap into the application start up and tear down events, like so:

@Configuration
public class TempDirConfig {

  private static final Path TEMP_PATH = Paths.get( "/tmp/dbEnv" );

  @Bean( "createTempDir" )
  public ApplicationListener<ContextStartedEvent> createTempDir() {
    return event -> {
      try {
        if ( !Files.exists( TEMP_PATH ) ) {
          Files.createDirectories( TEMP_PATH );
        }
      }
      catch ( IOException aE ) {
        // oops
      }
    };
  }

  @Bean( "removeTempDir" )
  public ApplicationListener<ContextClosedEvent> removeTempDir() {
    return event -> {
      try {
        if ( Files.exists( TEMP_PATH ) ) {
          FileSystemUtils.deleteRecursively( TEMP_PATH );
        }
      }
      catch ( IOException aE ) {
        // oops
      }
    };
  }
}
0

i can't push empty packages into git

In this case we add a .gitkeep (similar to .gitignore) to that directory. The dir is no longer empty, yet the . makes the file hidden.

File file = new File("src/tmp/dbEnv/");

I guess this creates the file "somewhere" else, try one of these getResource()-methods.

May be Spring or Gradle can do it?

Gradle was my first thought, too; creating the dir while starting up. But neither Spring nor Gradle will (reliable) detect the application's shutdown... You could add anything (but the metioned .gitkeep, in case you use it) to .gitignore in case you just want to prevent to commit it. And/or you can clear the directory during startup uf the application (e.g. in the main-method you mentioned).

crusy
  • 1,424
  • 2
  • 25
  • 54
0

URL path = ClassName.class.getClass().getProtectionDomain().getCodeSource().getLocation();

//URL path = this.getClass().getProtectionDomain().getCodeSource().getLocation();

String libpath = path.toString().substring(0, path.toString().indexOf("env/"));

//Libpath will return absolute path with scr/tmp/

0

My solution:

public class StartAppFilesProcessorListener implements ApplicationListener<ContextRefreshedEvent> {

  private static final String PATH = System.getProperty("user.dir") + "/src/tmp/dbEnv";

  @Override
  public void onApplicationEvent(ContextRefreshedEvent event) {
    File file = new File(PATH);
    if (!file.exists()) {
      try {
        Files.createDirectories(Paths.get(PATH));
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}
Alexey
  • 362
  • 1
  • 5
  • 17