protected Manifest doGetManifest()

in src/main/java/org/apache/maven/shared/archiver/MavenArchiver.java [245:374]


    protected Manifest doGetManifest(
            Session session, Project project, ManifestConfiguration config, Map<String, String> entries)
            throws ManifestException {
        // TODO: Should we replace "map" with a copy? Note, that we modify it!
        Manifest m = new Manifest();

        if (config.isAddDefaultEntries()) {
            handleDefaultEntries(m, entries);
        }

        if (config.isAddBuildEnvironmentEntries()) {
            handleBuildEnvironmentEntries(session, m, entries);
        }

        DependencyResolverResult result;
        if (config.isAddClasspath() || config.isAddExtensions()) {
            result = session.getService(DependencyResolver.class).resolve(session, project, PathScope.MAIN_RUNTIME);
        } else {
            result = null;
        }

        if (config.isAddClasspath()) {
            StringBuilder classpath = new StringBuilder();

            String classpathPrefix = config.getClasspathPrefix();
            String layoutType = config.getClasspathLayoutType();
            String layout = config.getCustomClasspathLayout();

            Interpolator interpolator = new StringSearchInterpolator();

            for (Map.Entry<Dependency, Path> entry : result.getDependencies().entrySet()) {
                Path artifactFile = entry.getValue();
                Dependency dependency = entry.getKey();
                if (Files.isRegularFile(artifactFile.toAbsolutePath())) {
                    if (!classpath.isEmpty()) {
                        classpath.append(" ");
                    }
                    classpath.append(classpathPrefix);

                    // NOTE: If the artifact or layout type (from config) is null, give up and use the file name by
                    // itself.
                    if (dependency == null || layoutType == null) {
                        classpath.append(artifactFile.getFileName().toString());
                    } else {
                        List<ValueSource> valueSources = new ArrayList<>();

                        handleExtraExpression(dependency, valueSources);

                        for (ValueSource vs : valueSources) {
                            interpolator.addValueSource(vs);
                        }

                        RecursionInterceptor recursionInterceptor =
                                new PrefixAwareRecursionInterceptor(ARTIFACT_EXPRESSION_PREFIXES);

                        try {
                            switch (layoutType) {
                                case CLASSPATH_LAYOUT_TYPE_SIMPLE:
                                    if (config.isUseUniqueVersions()) {
                                        classpath.append(interpolator.interpolate(SIMPLE_LAYOUT, recursionInterceptor));
                                    } else {
                                        classpath.append(interpolator.interpolate(
                                                SIMPLE_LAYOUT_NONUNIQUE, recursionInterceptor));
                                    }
                                    break;
                                case CLASSPATH_LAYOUT_TYPE_REPOSITORY:
                                    // we use layout /$groupId[0]/../${groupId[n]/$artifactId/$version/{fileName}
                                    // here we must find the Artifact in the project Artifacts
                                    // to create the maven layout
                                    if (config.isUseUniqueVersions()) {
                                        classpath.append(
                                                interpolator.interpolate(REPOSITORY_LAYOUT, recursionInterceptor));
                                    } else {
                                        classpath.append(interpolator.interpolate(
                                                REPOSITORY_LAYOUT_NONUNIQUE, recursionInterceptor));
                                    }
                                    break;
                                case CLASSPATH_LAYOUT_TYPE_CUSTOM:
                                    if (layout == null) {
                                        throw new ManifestException(CLASSPATH_LAYOUT_TYPE_CUSTOM
                                                + " layout type was declared, but custom layout expression was not"
                                                + " specified. Check your <archive><manifest><customLayout/>"
                                                + " element.");
                                    }

                                    classpath.append(interpolator.interpolate(layout, recursionInterceptor));
                                    break;
                                default:
                                    throw new ManifestException("Unknown classpath layout type: '" + layoutType
                                            + "'. Check your <archive><manifest><layoutType/> element.");
                            }
                        } catch (InterpolationException e) {
                            ManifestException error = new ManifestException(
                                    "Error interpolating artifact path for classpath entry: " + e.getMessage());

                            error.initCause(e);
                            throw error;
                        } finally {
                            for (ValueSource vs : valueSources) {
                                interpolator.removeValuesSource(vs);
                            }
                        }
                    }
                }
            }

            if (!classpath.isEmpty()) {
                // Class-Path is special and should be added to manifest even if
                // it is specified in the manifestEntries section
                addManifestAttribute(m, "Class-Path", classpath.toString());
            }
        }

        if (config.isAddDefaultSpecificationEntries()) {
            handleSpecificationEntries(project, entries, m);
        }

        if (config.isAddDefaultImplementationEntries()) {
            handleImplementationEntries(project, entries, m);
        }

        String mainClass = config.getMainClass();
        if (mainClass != null && !mainClass.isEmpty()) {
            addManifestAttribute(m, entries, "Main-Class", mainClass);
        }

        addCustomEntries(m, entries, config);

        return m;
    }