public void execute()

in nb-repository-plugin/src/main/java/org/apache/netbeans/nbm/repository/PopulateRepositoryMojo.java [268:638]


    public void execute()
            throws MojoExecutionException {
        getLog().info("Populate repository with NetBeans modules");
        Project antProject = antProject();
        RemoteRepository deploymentRepository = null;

        if (parentGAV != null) {
            // populate artefactParent
            artefactParent = new Parent();
            String[] split = parentGAV.split(":");
            if (split.length != 3) {
                throw new MojoExecutionException(
                        "parentGAV should respect the following format groupId:artefactId:version");
            }
            artefactParent.setGroupId(split[0]);
            artefactParent.setArtifactId(split[1]);
            artefactParent.setVersion(split[2]);
        }

        if (deployUrl != null) {
            deploymentRepository = repositorySystem.newDeploymentRepository(session.getRepositorySession(), new RemoteRepository.Builder(deployId, "default", deployUrl).build());
        } else if (skipLocalInstall) {
            throw new MojoExecutionException(
                    "When skipping install to local repository, one shall define the deployUrl parameter");
        }

        if (netbeansInstallDirectory == null) {
            Input input = (Input) antProject.createTask("input");
            input.setMessage("Please enter NetBeans installation directory:");
            input.setAddproperty("installDir");
            try {
                input.execute();
            } catch (BuildException e) {
                getLog().error("Cannot run ant:input");
                throw new MojoExecutionException(e.getMessage(), e);
            }
            String prop = antProject.getProperty("installDir");
            netbeansInstallDirectory = new File(prop);
        }

        File rootDir = netbeansInstallDirectory;
        if (!rootDir.exists()) {
            getLog().error("NetBeans installation doesn't exist.");
            throw new MojoExecutionException("NetBeans installation doesn't exist.");
        }
        getLog().info("Copying NetBeans artifacts from " + netbeansInstallDirectory);

        PathConvert convert = (PathConvert) antProject.createTask("pathconvert");
        convert.setPathSep(",");
        convert.setProperty("netbeansincludes");
        FileSet set = new FileSet();
        set.setDir(rootDir);
        set.createInclude().setName("**/modules/*.jar");
        set.createInclude().setName("*/core/*.jar");
        set.createInclude().setName("platform*/lib/*.jar");

        convert.createPath().addFileset(set);
        try {
            convert.execute();
        } catch (BuildException e) {
            getLog().error("Cannot run ant:pathconvert");
            throw new MojoExecutionException(e.getMessage(), e);
        }

        String prop = antProject.getProperty("netbeansincludes");
        StringTokenizer tok = new StringTokenizer(prop, ",");
        Map<ModuleWrapper, Artifact> moduleDefinitions = new HashMap<>();
        Map<String, Collection<ModuleWrapper>> clusters = new HashMap<>();
        while (tok.hasMoreTokens()) {
            String token = tok.nextToken();
            File module = new File(token);
            String clust = module.getAbsolutePath().substring(rootDir.getAbsolutePath().length() + 1);
            clust = clust.substring(0, clust.indexOf(File.separator));
            ExamineManifest examinator = new ExamineManifest(getLog());
            examinator.setPopulateDependencies(true);
            examinator.setJarFile(module);
            examinator.checkFile();
            if (examinator.isNetBeansModule() || examinator.isOsgiBundle()) {
                //TODO get artifact id from the module's manifest?
                String artifact = module.getName().substring(0, module.getName().indexOf(".jar"));
                if ("boot".equals(artifact)) {
                    artifact = "org-netbeans-bootstrap";
                }
                if ("core".equals(artifact)) {
                    artifact = "org-netbeans-core-startup";
                }
                if ("core-base".equals(artifact)) {
                    artifact = "org-netbeans-core-startup-base";
                }
                String version = forcedVersion == null ? examinator.getSpecVersion() : forcedVersion;
                String group = groupIdPrefix + (examinator.isOsgiBundle() ? GROUP_EXTERNAL : examinator.hasPublicPackages() ? GROUP_API : GROUP_IMPL);
                Artifact art = createArtifact(artifact, version, group);
                ModuleWrapper wr = new ModuleWrapper(artifact, version, group, examinator, module);
                if (examinator.isOsgiBundle()) {
                    Dependency dep = findExternal(module);
                    if (dep != null) {

                        art = createArtifact(dep.getArtifactId(), dep.getVersion(), dep.getGroupId());
                        group = dep.getGroupId();
                        version = dep.getVersion();
                        wr = new ModuleWrapperMaven(artifact, version, group, examinator, module, dep);
                    }
                }
                wr.setCluster(clust);
                moduleDefinitions.put(wr, art);
                Collection<ModuleWrapper> col = clusters.get(clust);
                if (col == null) {
                    col = new ArrayList<>();
                    clusters.put(clust, col);
                }
                col.add(wr);
            }
        }

        File javadocRoot = null;
        if (netbeansJavadocDirectory != null) {
            javadocRoot = netbeansJavadocDirectory;
            if (!javadocRoot.exists()) {
                javadocRoot = null;
                throw new MojoExecutionException(
                        "The netbeansJavadocDirectory parameter doesn't point to an existing folder");
            }
        }
        File sourceRoot = null;
        if (netbeansSourcesDirectory != null) {
            sourceRoot = netbeansSourcesDirectory;
            if (!sourceRoot.exists()) {
                sourceRoot = null;
                throw new MojoExecutionException(
                        "The netbeansSourceDirectory parameter doesn't point to an existing folder");
            }
        }

        File nbmRoot = null;
        if (netbeansNbmDirectory != null) {
            nbmRoot = netbeansNbmDirectory;
            if (!nbmRoot.exists()) {
                nbmRoot = null;
                throw new MojoExecutionException(
                        "The nbmDirectory parameter doesn't point to an existing folder");
            }
        }
        List<ModuleWrapper> wrapperList = new ArrayList<>(moduleDefinitions.keySet());
        // artifact that we need to populate
        Map<ModuleWrapper, Artifact> tobePopulated = new HashMap<>();
        // external artefacts
        Map<ModuleWrapper, Artifact> oncentralWrapper = new HashMap<>();
        // triage
        for (Map.Entry<ModuleWrapper, Artifact> entry : moduleDefinitions.entrySet()) {
            if (entry.getKey() instanceof ModuleWrapperMaven) {
                oncentralWrapper.put(entry.getKey(), entry.getValue());
            } else {
                tobePopulated.put(entry.getKey(), entry.getValue());
            }
        }
        List<ExternalsWrapper> externals = new ArrayList<>();
        int count = tobePopulated.size() + 1;
        int index = 0;

        try {
            for (Map.Entry<ModuleWrapper, Artifact> elem : tobePopulated.entrySet()) {
                ModuleWrapper man = elem.getKey();
                Artifact art = elem.getValue();
                index = index + 1;
                getLog().info("Processing " + index + "/" + count);
                File pom = createMavenProject(man, wrapperList, externals);
                Artifact pomArt = new SubArtifact(art, "", "pom", pom);
                File javadoc = null;
                Artifact javadocArt = null;
                if (javadocRoot != null) {
                    File zip = new File(javadocRoot, art.getArtifactId() + ".zip");
                    if (zip.exists()) {
                        javadoc = zip;
                        javadocArt = createAttachedArtifact(art, javadoc, "jar", "javadoc");
                    }
                }
                File source = null;
                Artifact sourceArt = null;
                if (sourceRoot != null) {
                    File zip = new File(sourceRoot, art.getArtifactId() + ".zip");
                    if (zip.exists()) {
                        source = zip;
                        sourceArt = createAttachedArtifact(art, source, "jar", "sources");
                    }
                }
                File nbm = null;
                Artifact nbmArt = null;
                if (nbmRoot != null) {
                    File zip = new File(nbmRoot, art.getArtifactId() + ".nbm");

                    if (!zip.exists()) {
                        zip = new File(nbmRoot,
                                man.getCluster() + File.separator + art.getArtifactId() + ".nbm");
                    }
                    if (zip.exists()) {
                        nbm = zip;
                        nbmArt = createAttachedArtifact(art, nbm, "nbm-file", null);
                        if (nbmArt.getExtension().equals("nbm-file")) {
                            // Maven 2.x compatibility.
                            nbmArt = createAttachedArtifact(art, nbm, "nbm", null);
                        }
                        assert nbmArt.getExtension().equals("nbm");
                    }
                }
                File moduleJar = man.getFile();
                File moduleJarMinusCP = null;
                if (!man.getModuleManifest().getClasspath().isEmpty()) {
                    try {
                        moduleJarMinusCP = Files.createTempFile(man.getArtifact(), ".jar").toFile();
                        moduleJarMinusCP.deleteOnExit();
                        try (InputStream is = Files.newInputStream(moduleJar.toPath())) {
                            try (OutputStream os = Files.newOutputStream(moduleJarMinusCP.toPath())) {
                                JarInputStream jis = new JarInputStream(is);
                                Manifest mani = new Manifest(jis.getManifest());
                                mani.getMainAttributes().remove(Attributes.Name.CLASS_PATH);
                                if (!man.deps.isEmpty()) { // MNBMODULE-132
                                    StringBuilder b = new StringBuilder();
                                    for (Dependency dep : man.deps) {
                                        if (b.length() > 0) {
                                            b.append(' ');
                                        }
                                        b.append(dep.getGroupId()).append(':').append(dep.getArtifactId())
                                                .append(':').append(dep.getVersion());
                                        if (dep.getClassifier() != null) {
                                            b.append(":").append(dep.getClassifier());
                                        }
                                    }
                                    mani.getMainAttributes().putValue("Maven-Class-Path", b.toString());
                                } else {
                                    getLog().warn("did not find any external artifacts for " + man.getModule());
                                }
                                JarOutputStream jos = new JarOutputStream(os, mani);
                                JarEntry entry;
                                while ((entry = jis.getNextJarEntry()) != null) {
                                    if (entry.getName().matches("META-INF/.+[.]SF")) {
                                        throw new IOException("cannot handle signed JARs");
                                    }
                                    jos.putNextEntry(entry);
                                    byte[] buf = new byte[(int) entry.getSize()];
                                    int read = jis.read(buf, 0, buf.length);
                                    if (read != buf.length) {
                                        throw new IOException("read wrong amount");
                                    }
                                    jos.write(buf);
                                }
                                jos.close();
                            }
                        }
                    } catch (IOException x) {
                        getLog().warn("Could not process " + moduleJar + ": " + x, x);
                        moduleJarMinusCP.delete();
                        moduleJarMinusCP = null;
                    }
                }
                try {
                    if (!skipLocalInstall) {
                        install(pomArt.setFile(pom));
                        install(art.setFile(moduleJarMinusCP != null ? moduleJarMinusCP : moduleJar));
                        if (javadoc != null) {
                            install(javadocArt.setFile(javadoc));
                        }
                        if (source != null) {
                            install(sourceArt.setFile(source));
                        }
                        if (nbm != null) {
                            install(nbmArt.setFile(nbm));
                        }
                    }
                    try {
                        if (deploymentRepository != null) {
                            DeployRequest deployRequest = new DeployRequest();
                            deployRequest.setRepository(deploymentRepository);
                            deployRequest.setTrace(RequestTrace.newChild(null, "nb-repository-plugin"));
                            deployRequest.addArtifact(art.setFile(moduleJarMinusCP != null ? moduleJarMinusCP : moduleJar));
                            if (pom != null) {
                                deployRequest.addArtifact(pomArt.setFile(pom));
                            }
                            if (javadoc != null) {
                                deployRequest.addArtifact(javadocArt.setFile(javadoc));
                            }
                            if (source != null) {
                                deployRequest.addArtifact(sourceArt.setFile(source));
                            }
                            if (nbm != null) {
                                deployRequest.addArtifact(nbmArt.setFile(nbm));
                            }
                            repositorySystem.deploy(session.getRepositorySession(), deployRequest);
                        }
                    } catch (DeploymentException ex) {
                        throw new MojoExecutionException("Error Deploying artifact", ex);
                    }
                } finally {
                    if (moduleJarMinusCP != null) {
                        moduleJarMinusCP.delete();
                    }
                }
            }
        } finally {
            /*if ( searcher != null )
            {
                try
                {
                    searcher.close();
                }
                catch ( IOException ex )
                {
                    getLog().error( ex );
                }
            }*/
        }

        //process collected non-recognized external jars..
        if (!externals.isEmpty()) {
            index = 0;
            count = externals.size();
            for (ExternalsWrapper ex : externals) {
                Artifact art = createArtifact(ex.getArtifact(), ex.getVersion(), ex.getGroupid());
                index = index + 1;
                getLog().info("Processing external " + index + "/" + count);
                File pom = createExternalProject(ex);
                Artifact pomArt = new SubArtifact(art, "", "pom", pom);
                if (!skipLocalInstall) {
                    install(pomArt.setFile(pom));
                    install(art.setFile(ex.getFile()));
                }
                try {
                    if (deploymentRepository != null) {
                        DeployRequest deployRequest = new DeployRequest();
                        deployRequest.setRepository(deploymentRepository);
                        deployRequest.setTrace(RequestTrace.newChild(null, "nb-repository-plugin"));
                        deployRequest.addArtifact(pomArt.setFile(pom));
                        deployRequest.addArtifact(art.setFile(ex.getFile()));

                        repositorySystem.deploy(session.getRepositorySession(), deployRequest);
                    }
                } catch (DeploymentException exc) {
                    throw new MojoExecutionException("Error Deploying artifact", exc);
                }
            }
        }

        if (!defineCluster) {
            getLog().info("Not creating cluster POMs.");
        } else if (forcedVersion == null) {
            getLog().warn("Version not specified, cannot create cluster POMs.");
        } else {
            for (Map.Entry<String, Collection<ModuleWrapper>> elem : clusters.entrySet()) {
                String cluster = stripClusterName(elem.getKey());
                Collection<ModuleWrapper> modules = elem.getValue();
                getLog().info("Processing cluster " + cluster);
                Artifact art = createClusterArtifact(cluster, forcedVersion);
                File pom = createClusterProject(art, modules);
                if (!skipLocalInstall) {
                    install(art.setFile(pom));
                }
                try {
                    if (deploymentRepository != null) {
                        DeployRequest deployRequest = new DeployRequest();
                        deployRequest.setRepository(deploymentRepository);
                        deployRequest.setTrace(RequestTrace.newChild(null, "nb-repository-plugin"));
                        deployRequest.addArtifact(art.setFile(pom));

                        repositorySystem.deploy(session.getRepositorySession(), deployRequest);
                    }
                } catch (DeploymentException ex) {
                    throw new MojoExecutionException("Error Deploying artifact", ex);
                }
            }

        }
    }