private void onArtifact()

in src/main/java/org/apache/sling/feature/maven/mojos/ApisJarMojo.java [866:998]


    private void onArtifact(
            final ApiRegions apiRegions, final ApisJarContext ctx, final RegionSupport regionSupport, Artifact artifact)
            throws MojoExecutionException {
        File bundleFile = getArtifactFile(artifact.getId());

        Manifest manifest = regionSupport.getManifest(artifact.getId(), bundleFile);

        // check if the bundle is exporting packages?
        Clause[] exportedPackageClauses = regionSupport.getExportedPackages(manifest);
        if (exportedPackageClauses.length > 0) {

            // calculate the exported packages in the manifest file for all regions
            final Set<String> usedExportedPackages = regionSupport.computeAllUsedExportPackages(
                    apiRegions, ctx.getConfig().getEnabledToggles(), exportedPackageClauses, artifact);

            boolean isArtifactFullyExported = usedExportedPackages.size() == exportedPackageClauses.length;
            if (!usedExportedPackages.isEmpty()) {
                // check for previous version of artifact due to toggles
                ArtifactId previous = null;
                for (final String pckName : usedExportedPackages) {
                    for (final ApiRegion region : apiRegions.listRegions()) {
                        final ApiExport exp = region.getExportByName(pckName);
                        if (exp != null) {
                            if (exp.getToggle() != null
                                    && !ctx.getConfig().getEnabledToggles().contains(exp.getToggle())
                                    && exp.getPreviousArtifactId() != null) {
                                if (previous != null && previous.compareTo(exp.getPreviousArtifactId()) != 0) {
                                    throw new MojoExecutionException(
                                            "More than one previous version artifact configured for "
                                                    + artifact.getId().toMvnId() + " : " + previous.toMvnId() + ", "
                                                    + exp.getPreviousArtifactId()
                                                            .toMvnId());
                                }
                                previous = exp.getPreviousArtifactId();
                            }
                            break;
                        }
                    }
                }
                if (previous != null) {
                    final Artifact previousArtifact = new Artifact(previous);
                    previousArtifact.getMetadata().putAll(artifact.getMetadata());
                    getLog().debug("Using " + previous.toMvnId() + " instead of "
                            + artifact.getId().toMvnId() + " due to disabled toggle(s)");
                    artifact = previousArtifact;
                    bundleFile = getArtifactFile(artifact.getId());
                    manifest = regionSupport.getManifest(artifact.getId(), bundleFile);
                    exportedPackageClauses = regionSupport.getExportedPackages(manifest);
                }

                final ArtifactInfo info = ctx.addArtifactInfo(artifact);
                info.setUsedExportedPackages(usedExportedPackages);

                // calculate per region packages
                for (final ApiRegion region : apiRegions.listRegions()) {
                    final Set<Clause> usedExportedPackagesPerRegion = regionSupport.computeUsedExportPackagesPerRegion(
                            region, exportedPackageClauses, usedExportedPackages);

                    // check whether packages are included in api jars - or added as a dependency
                    String useAsDependency = "";
                    if (ctx.getConfig().isUseApiDependencies()) {
                        useAsDependency = regionSupport.calculateOmitDependenciesFlag(
                                region, exportedPackageClauses, usedExportedPackagesPerRegion);
                    }
                    if (useAsDependency == null) {
                        if (ctx.findDependencyArtifact(getLog(), info)) {
                            // check scm info
                            if (artifact.getMetadata().get(ApisUtil.SCM_LOCATION) != null) {
                                throw new MojoExecutionException(
                                        "Dependency artifact must not specify " + ApisUtil.SCM_LOCATION + " : "
                                                + artifact.getId().toMvnId());
                            }
                        } else {
                            useAsDependency = "Unable to find artifact in maven repository.";
                        }
                    }
                    info.setUsedExportedPackages(region.getName(), usedExportedPackagesPerRegion, useAsDependency);
                    if (!isArtifactFullyExported) {
                        final Set<Clause> providedCapabilitiesPerRegion =
                                computeProvidedCapabilities(region, getProvidedCapability(manifest), artifact);
                        info.setProvidedCapabilities(region.getName(), providedCapabilitiesPerRegion);
                    } else {
                        // in case there are no region restrictions just include all capabilities
                        info.setProvidedCapabilities(
                                region.getName(), new LinkedHashSet<>(Arrays.asList(getProvidedCapability(manifest))));
                    }
                }

                info.setBinDirectory(
                        new File(ctx.getDeflatedBinDir(), info.getId().toMvnName()));
                info.setSourceDirectory(
                        new File(ctx.getDeflatedSourcesDir(), info.getId().toMvnName()));

                final boolean skipBinDeflate = info.getBinDirectory().exists();
                if (skipBinDeflate) {
                    getLog().debug("Artifact " + info.getId().toMvnName() + " already deflated");
                }
                final boolean skipSourceDeflate = info.getSourceDirectory().exists();
                if (skipSourceDeflate) {
                    getLog().debug("Source for artifact " + info.getId().toMvnName() + " already deflated");
                }

                final String bundleClassPath = manifest.getMainAttributes().getValue(Constants.BUNDLE_CLASSPATH);
                final String[] embeddedBundles;
                if (bundleClassPath != null && !bundleClassPath.isEmpty()) {
                    embeddedBundles = bundleClassPath.split(",");
                } else {
                    embeddedBundles = null;
                }

                processBinary(ctx, info, bundleFile, artifact, embeddedBundles, skipBinDeflate, skipSourceDeflate);

                // check if the bundle wraps other bundles
                if (embeddedBundles != null) {
                    computeWrappedBundles(ctx, info, embeddedBundles, skipBinDeflate, skipSourceDeflate);
                }

                postProcessArtifact(ctx, info, artifact);

                if (!info.getSourceDirectory().exists()) {
                    info.setSourceDirectory(null);
                }

                if (generateJavadocJar) {
                    ApisUtil.buildJavadocClasspath(getLog(), repositorySystem, mavenSession, artifact.getId())
                            .forEach(ctx::addJavadocClasspath);
                }

            } else {
                // TODO: add relevant capabilities?
            }
        }
    }