protected void processResourceBundles()

in src/main/java/org/apache/maven/plugin/resources/remote/AbstractProcessRemoteResourcesMojo.java [889:1000]


    protected void processResourceBundles(ClassLoader classLoader, VelocityContext context)
            throws MojoExecutionException {
        List<Map.Entry<String, RemoteResourcesBundle>> remoteResources = new ArrayList<>();
        int bundleCount = 0;
        int resourceCount = 0;

        // list remote resources form bundles
        try {
            RemoteResourcesBundleXpp3Reader bundleReader = new RemoteResourcesBundleXpp3Reader();

            for (Enumeration<URL> e = classLoader.getResources(BundleRemoteResourcesMojo.RESOURCES_MANIFEST);
                    e.hasMoreElements(); ) {
                URL url = e.nextElement();
                bundleCount++;
                getLog().debug("processResourceBundle on bundle#" + bundleCount + " " + url);

                RemoteResourcesBundle bundle;

                try (InputStream in = url.openStream()) {
                    bundle = bundleReader.read(in);
                }

                int n = 0;
                for (String bundleResource : bundle.getRemoteResources()) {
                    n++;
                    resourceCount++;
                    getLog().debug("bundle#" + bundleCount + " resource#" + n + " " + bundleResource);
                    remoteResources.add(new AbstractMap.SimpleEntry<>(bundleResource, bundle));
                }
            }
        } catch (IOException ioe) {
            throw new MojoExecutionException("Error finding remote resources manifests", ioe);
        } catch (XmlPullParserException xppe) {
            throw new MojoExecutionException("Error parsing remote resource bundle descriptor.", xppe);
        }

        getLog().info("Copying " + resourceCount + " resource" + ((resourceCount > 1) ? "s" : "") + " from "
                + bundleCount + " bundle" + ((bundleCount > 1) ? "s" : "") + ".");

        String velocityResource = null;
        try {

            for (Map.Entry<String, RemoteResourcesBundle> entry : remoteResources) {
                String bundleResource = entry.getKey();
                RemoteResourcesBundle bundle = entry.getValue();

                String projectResource = bundleResource;

                boolean doVelocity = false;
                if (projectResource.endsWith(TEMPLATE_SUFFIX)) {
                    projectResource = projectResource.substring(0, projectResource.length() - 3);
                    velocityResource = bundleResource;
                    doVelocity = true;
                }

                // Don't overwrite resource that are already being provided.

                File f = new File(outputDirectory, projectResource);

                FileUtils.mkdir(f.getParentFile().getAbsolutePath());

                if (!copyResourceIfExists(f, projectResource, context)) {
                    if (doVelocity) {
                        try (DeferredFileOutputStream os =
                                new DeferredFileOutputStream(velocityFilterInMemoryThreshold, f)) {
                            try (Writer writer = bundle.getSourceEncoding() == null
                                    ? new OutputStreamWriter(os)
                                    : new OutputStreamWriter(os, bundle.getSourceEncoding())) {
                                if (bundle.getSourceEncoding() == null) {
                                    // TODO: Is this correct? Shouldn't we behave like the rest of maven and fail
                                    // down to JVM default instead ISO-8859-1 ?
                                    velocity.mergeTemplate(bundleResource, "ISO-8859-1", context, writer);
                                } else {
                                    velocity.mergeTemplate(bundleResource, bundle.getSourceEncoding(), context, writer);
                                }
                            }
                            fileWriteIfDiffers(os);
                        }
                    } else {
                        URL resUrl = classLoader.getResource(bundleResource);
                        if (resUrl != null) {
                            FileUtils.copyURLToFile(resUrl, f);
                        }
                    }

                    File appendedResourceFile = new File(appendedResourcesDirectory, projectResource);
                    File appendedVmResourceFile = new File(appendedResourcesDirectory, projectResource + ".vm");

                    if (appendedResourceFile.exists()) {
                        getLog().info("Copying appended resource: " + projectResource);
                        try (InputStream in = Files.newInputStream(appendedResourceFile.toPath());
                                OutputStream out = new FileOutputStream(f, true)) {
                            IOUtil.copy(in, out);
                        }

                    } else if (appendedVmResourceFile.exists()) {
                        getLog().info("Filtering appended resource: " + projectResource + ".vm");

                        try (Reader reader = new FileReader(appendedVmResourceFile);
                                Writer writer = getWriter(bundle, f)) {
                            Velocity.init();
                            Velocity.evaluate(context, writer, "remote-resources", reader);
                        }
                    }
                }
            }
        } catch (IOException ioe) {
            throw new MojoExecutionException("Error reading remote resource", ioe);
        } catch (VelocityException e) {
            throw new MojoExecutionException("Error rendering Velocity resource '" + velocityResource + "'", e);
        }
    }