1717import java .net .URL ;
1818import java .nio .charset .StandardCharsets ;
1919import java .nio .file .Files ;
20+ import java .nio .file .Path ;
2021import java .nio .file .StandardCopyOption ;
2122import java .util .Arrays ;
2223import java .util .List ;
2324import java .util .function .Function ;
2425import java .util .regex .Pattern ;
2526import java .util .stream .Collectors ;
2627
28+ import io .github .fvarrui .javapackager .packagers .Packager ;
2729import org .apache .commons .io .IOUtils ;
2830import org .apache .commons .lang3 .StringUtils ;
2931
@@ -191,9 +193,13 @@ private static void copyStreamToFile(InputStream is, File dest) throws Exception
191193 throw new Exception ("Could not copy input stream to " + dest , ex );
192194 }
193195 }
194-
195- public static void copyResourceToFile (String resource , File dest , boolean unixStyleNewLines ) throws Exception {
196- copyResourceToFile (resource , dest );
196+
197+ public static void copyResourceToFile (String resource , File dest , boolean unixStyleNewLines ) throws Exception {
198+ copyResourceToFile (resource , dest , unixStyleNewLines , null );
199+ }
200+
201+ public static void copyResourceToFile (String resource , File dest , boolean unixStyleNewLines , Packager packager ) throws Exception {
202+ copyResourceToFile (resource , dest , packager );
197203 if (unixStyleNewLines ) {
198204 try {
199205 processFileContent (dest , c -> c .replaceAll ("\\ r\\ n" , "\n " ).replaceAll ("\\ r" , "\n " ));
@@ -208,8 +214,22 @@ public static void processFileContent(File dest, Function<String, String> functi
208214 content = function .apply (content );
209215 writeStringToFile (dest , content , StandardCharsets .UTF_8 );
210216 }
211-
212- public static void copyResourceToFile (String resource , File dest ) throws Exception {
217+
218+ public static void copyResourceToFile (String resource , File dest ) throws Exception {
219+ copyResourceToFile (resource , dest , null );
220+ }
221+
222+ public static void copyResourceToFile (String resource , File dest , Packager packager ) throws Exception {
223+ if (packager != null ) {
224+ String rsc = resource .startsWith ("/" ) ? resource .substring (1 ) : resource ;
225+ Path asset = packager .getAssetsDir ().toPath ().resolve (rsc );
226+ if (Files .exists (asset )) {
227+ Logger .info ("Copying resource [" + asset + "] to file [" + dest + "]" );
228+ copyFileToFile (asset .toFile (), dest );
229+ return ;
230+ }
231+ }
232+
213233 Logger .info ("Copying resource [" + resource + "] to file [" + dest + "]" );
214234 copyStreamToFile (FileUtils .class .getResourceAsStream (resource ), dest );
215235 }
0 commit comments