0

Hi this follows on from this question Java Maven reference files in WebApp

I'm attempting to copy a folder into the build directory (this is successful) I then attempt to run the application and copy that directory into a temp directory so I can make changes and zip it up into a Scorm package.

The folder is in the build as

C:\workspace\admin\target\admin-1.0-SNAPSHOT\resources\applications\scorm

I'm trying to reference this from the running application as:

    System.out.println(new File("applications/scorm").getAbsolutePath());

    File source = new File("applications/scorm");
    File dest = new File(Constants.TEMP_DIR_PATH + '/' + guideId);
    try {
        FileUtils.copyDirectory(source, dest);
        System.out.println("copy complete");
    } catch (IOException e) {
        e.printStackTrace();
    }

It never finds the file and my absolute path check gives me

C:\workspace\admin\applications\scorm\

Which would indicate it's looking in the root directory.

How do I reference a file from the built application that is in the build directory? enter image description here

i.e. the folder is in the root of the build directory under resources, what goes in here File source = new File("What is the file URL from the build directory?"); to reference that folder?

EDIT

This works

target/admin-1.0-SNAPSHOT/resources/applications/scorm

But I doubt will work in production when it's deployed as a WAR file

Community
  • 1
  • 1
beek
  • 3,522
  • 8
  • 33
  • 86
  • Maybe try a classpath resource? In that case you're resilient to running as a war or exploded war (i.e. unzipped war). – Taylor Oct 12 '16 at 20:27
  • How would I go about that and how do you reference the folder? – beek Oct 12 '16 at 20:30
  • Sorry, are you running the above as part of your build or as part of running your application? – Taylor Oct 12 '16 at 20:31
  • The application.. the copying into the build directory is already done - I want to copy the folder at run time to create a new folder with changes that I can zip and deploy as a Scorm package. – beek Oct 12 '16 at 20:36
  • This is really really unclear. But first, you cannot access a resource inside a JAR as a `File`: http://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar. What are you trying to do? *copy a folder into the build directory* Why? *run the application* Which app and where is it, where does it come from? *into a temp directory so I can make changes and zip it up into a Scorm package.* I don't follow. – Tunaki Oct 12 '16 at 20:45
  • It's not a Jar. It's a WAR. Does the same rules apply? A SCORM package is a zip file with a load of files in it, I have to make one change to the index.html file for each SCORM package. The only way I can see to do that is to copy a template folder, make the change, zip it up, deploy it and delete the temporary folder. – beek Oct 12 '16 at 20:54
  • I aggree with @Tunaki. Your description is unclear. How are you copying it ? It would be clear if you can provide your pom.xml. Also can you provide the screenshot of your entire folder structure(not just the target directory). I want to know where is your resources folder originally present. – RITZ XAVI Oct 12 '16 at 20:56
  • I'm not sure - I didn't set this project up - but I copied the original files into 'src\main\webapp\resources\applications' and boom they turned up in 'target\admin-1.0-SNAPSHOT\resources\applications' must be Maven magic – beek Oct 12 '16 at 20:59
  • 1
    copy them in `src\main\resources\applications` instead of `src\main\webapp\resources\applications`. – RITZ XAVI Oct 12 '16 at 21:00
  • Yeah I tried that and they didn't get deployed. I think the guy that build this must have configured it to use webapp/resources - all those JS and CSS files are found and used by the application - I don't really want to jeopardise that. Will have another go tho to see if it makes it easier to reference them from within the application. – beek Oct 12 '16 at 21:02
  • Yeah if I move the directory into src/main/resources it doesn't get copied in the build process. But this isn't really the problem - the files are there, it's just referencing them I'm having trouble with. – beek Oct 12 '16 at 21:07
  • 1
    Try this `File source = new File("../../../resources/applications/scorm");` and let us know if it worked. – RITZ XAVI Oct 12 '16 at 21:09
  • No still can't find it - thanks tho.. I've tried all combinations of ../../resources/applications/scorm etc also. – beek Oct 12 '16 at 21:18
  • My last guess would be to use `File source = new File(this.getClass().getResource( "/resources/applications/scorm").toURI());` – RITZ XAVI Oct 12 '16 at 21:29
  • Thanks - can't use 'this' as it's a static. I'll keep going and then look at it again when it comes to deploying on server. Thanks for your help. – beek Oct 12 '16 at 21:32
  • 1
    As I commented before, you _cannot_, and will never be able to, no matter how hard you try, access a resource inside of a JAR or a WAR as a `File`. It does not work. It is not a `File`. You can only have an `InputStream`. And whatever is inside `src/main/resources` is a resource that will end up in the JAR/WAR. And [I still don't understand the real question here](http://stackoverflow.com/questions/40007688/maven-reference-folder-in-build#comment67294070_40007688). – Tunaki Oct 12 '16 at 21:58
  • OK that's helpful - the resource files will probably have to go up separately from the WAR and be referenced from the root directory. Everything else is working I'll come back to this part when it's ready to go into server. All of our JS and CSS files are in this folder as webapp/resources.. they're all loaded as files in the browser right? It's probably deployed as an expanded WAR – beek Oct 12 '16 at 22:07

1 Answers1

1

You need to specify all the artifacts from your build directory (where your pom.xml) So the solution is to have the source location as resources/applications/scorm

System.out.println(new File("resources/applications/scorm").getAbsolutePath());

File source = new File("resources/applications/scorm");
File dest = new File(Constants.TEMP_DIR_PATH + '/' + guideId);
try {
    FileUtils.copyDirectory(source, dest);
    System.out.println("copy complete");
} catch (IOException e) {
    e.printStackTrace();
}
Eranda
  • 1,439
  • 1
  • 17
  • 30
  • Can you explain this a little better with code examples? – beek Oct 12 '16 at 20:35
  • Hi, I've been trying that - doesn't work - this works target/admin-1.0-SNAPSHOT/resources/applications/scorm but I doubt will work in production – beek Oct 12 '16 at 20:51
  • This is not the right way of doing it. Because your artifact name would have changed from `admin-1.0-SNAPSHOT` to something else by the time it would have gone to production. Hence hardcoding the path will not work. – RITZ XAVI Oct 12 '16 at 21:11
  • This won't work unless you have not package whatever resources in to your jar file. – Eranda Oct 12 '16 at 21:13
  • I think I'll host the resources elsewhere on S3 or something then load them in as a zip, do the changes and then deploy - this seems easier (but slower) – beek Oct 12 '16 at 22:18