private void rewriteDependencyReducedPomIfWeHaveReduction()

in src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java [1123:1202]


    private void rewriteDependencyReducedPomIfWeHaveReduction(
            List<Dependency> dependencies, boolean modified, List<Dependency> transitiveDeps, Model model)
            throws IOException, ProjectBuildingException, DependencyCollectionException {
        if (modified) {
            for (int loopCounter = 0; modified; loopCounter++) {

                model.setDependencies(dependencies);

                if (generateUniqueDependencyReducedPom) {
                    dependencyReducedPomLocation = Files.createTempFile(
                                    project.getBasedir().toPath(), "dependency-reduced-pom-", ".xml")
                            .toFile();
                    project.getProperties()
                            .setProperty(
                                    "maven.shade.dependency-reduced-pom",
                                    dependencyReducedPomLocation.getAbsolutePath());
                } else {
                    if (dependencyReducedPomLocation == null) {
                        // MSHADE-123: We can't default to 'target' because it messes up uses of ${project.basedir}
                        dependencyReducedPomLocation = new File(project.getBasedir(), "dependency-reduced-pom.xml");
                    }
                }

                File f = dependencyReducedPomLocation;
                // MSHADE-225
                // Works for now, maybe there's a better algorithm where no for-loop is required
                if (loopCounter == 0) {
                    getLog().info("Dependency-reduced POM written at: " + f.getAbsolutePath());
                }

                if (f.exists()) {
                    // noinspection ResultOfMethodCallIgnored
                    f.delete();
                }

                Writer w = WriterFactory.newXmlWriter(f);

                String replaceRelativePath = null;
                if (model.getParent() != null) {
                    replaceRelativePath = model.getParent().getRelativePath();
                }

                if (model.getParent() != null) {
                    File parentFile =
                            new File(project.getBasedir(), model.getParent().getRelativePath()).getCanonicalFile();
                    if (!parentFile.isFile()) {
                        parentFile = new File(parentFile, "pom.xml");
                    }

                    parentFile = parentFile.getCanonicalFile();

                    String relPath = RelativizePath.convertToRelativePath(parentFile, f);
                    model.getParent().setRelativePath(relPath);
                }

                try {
                    PomWriter.write(w, model, true);
                } finally {
                    if (model.getParent() != null) {
                        model.getParent().setRelativePath(replaceRelativePath);
                    }
                    w.close();
                }

                synchronized (session.getProjectBuildingRequest()) { // Lock critical section to fix MSHADE-467
                    ProjectBuildingRequest projectBuildingRequest =
                            new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
                    projectBuildingRequest.setLocalRepository(session.getLocalRepository());
                    projectBuildingRequest.setRemoteRepositories(project.getRemoteArtifactRepositories());

                    ProjectBuildingResult result = projectBuilder.build(f, projectBuildingRequest);

                    getLog().debug("updateExcludesInDeps()");
                    modified = updateExcludesInDeps(result.getProject(), dependencies, transitiveDeps);
                }
            }

            project.setFile(dependencyReducedPomLocation);
        }
    }