protected void packageSources()

in src/main/java/org/apache/maven/plugins/source/AbstractSourceJarMojo.java [281:359]


    protected void packageSources(List<Project> theProjects) throws MojoException {
        Artifact currentProjectArtifact = project.getMainArtifact().get();
        if (!currentProjectArtifact.getClassifier().isEmpty()) {
            getLog().warn("NOT adding sources to artifacts with classifier as Maven only supports one classifier "
                    + "per artifact. Current artifact [" + currentProjectArtifact.key() + "] has a ["
                    + currentProjectArtifact.getClassifier() + "] classifier.");

            return;
        }

        MavenArchiver archiver = createArchiver();

        for (Project pItem : theProjects) {
            Project subProject = getProject(pItem);

            String type = subProject.getPackaging().type().id();
            if (Type.POM.equals(type) || Type.BOM.equals(type)) {
                continue;
            }

            archiveProjectContent(subProject, archiver.getArchiver());
        }

        if (archiver.getArchiver().getResources().hasNext() || forceCreation) {

            if (useDefaultManifestFile && Files.exists(defaultManifestFile) && archive.getManifestFile() == null) {
                getLog().info("Adding existing MANIFEST to archive. Found under: " + defaultManifestFile);
                archive.setManifestFile(defaultManifestFile);
            }

            Path outputFile = outputDirectory.resolve(finalName + "-" + getClassifier() + getExtension());

            try {
                archiver.setOutputFile(outputFile.toFile());
                archive.setForced(forceCreation);

                getLog().debug("create archive " + outputFile);
                archiver.createArchive(session, project, archive);
            } catch (ArchiverException e) {
                throw new MojoException("Error creating source archive: " + e.getMessage(), e);
            }

            if (attach) {
                Artifact artifact = session.createArtifact(
                        project.getGroupId(),
                        project.getArtifactId(),
                        project.getVersion(),
                        getClassifier(),
                        null,
                        getType());
                boolean requiresAttach = true;
                for (Artifact attachedArtifact : projectManager.getAttachedArtifacts(project)) {
                    if (Objects.equals(artifact.key(), attachedArtifact.key())) {
                        Path attachedFile = session.getService(ArtifactManager.class)
                                .getPath(attachedArtifact)
                                .orElse(null);
                        if (attachedFile != null && !outputFile.equals(attachedFile)) {
                            getLog().error("Artifact " + attachedArtifact.key()
                                    + " already attached to a file " + relative(attachedFile) + ": attach to "
                                    + relative(outputFile) + " should be done with another classifier");
                            throw new MojoException("Presumably you have configured maven-source-plugin "
                                    + "to execute twice in your build to different output files. "
                                    + "You have to configure a classifier for at least one of them.");
                        }
                        requiresAttach = false;
                        getLog().info("Artifact " + attachedArtifact.key() + " already attached to "
                                + relative(outputFile) + ": ignoring same re-attach (same artifact, same file)");
                    }
                }
                if (requiresAttach) {
                    projectManager.attachArtifact(project, artifact, outputFile);
                }
            } else {
                getLog().info("NOT adding java-sources to attached artifacts list.");
            }
        } else {
            getLog().info("No sources in project. Archive not created.");
        }
    }