/*
 * Copyright 2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.api.publish.internal.validation;

import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import org.gradle.api.Project;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.publish.internal.PublicationInternal;

import javax.annotation.Nullable;
import java.net.URI;

public class DuplicatePublicationTracker {
    private final static Logger LOG = Logging.getLogger(DuplicatePublicationTracker.class);
    private final Multimap<String, PublicationWithProject> published = LinkedHashMultimap.create();

    public synchronized void checkCanPublish(Project project, PublicationInternal<?> publication, @Nullable URI repositoryLocation, String repositoryName) {
        // Don't track publications to repositories configured without a base URL
        if (repositoryLocation == null) {
            return;
        }

        String repositoryKey = normalizeLocation(repositoryLocation);

        PublicationWithProject publicationWithProject = new PublicationWithProject(project.getDisplayName(), publication);
        if (published.get(repositoryKey).contains(publicationWithProject)) {
            LOG.warn("Publication '" + publication.getCoordinates() + "' is published multiple times to the same location. It is likely that repository '" + repositoryName + "' is duplicated.");
            return;
        }

        ModuleVersionIdentifier projectIdentity = publication.getCoordinates();
        for (PublicationWithProject previousPublicationWithProject : published.get(repositoryKey)) {
            if (previousPublicationWithProject.getPublication().getCoordinates().equals(projectIdentity)) {
                LOG.warn("Multiple publications with coordinates '" + publication.getCoordinates() + "' are published to repository '" + repositoryName + "'. The publications " + previousPublicationWithProject + " and " + publicationWithProject + " will overwrite each other!");
            }
        }

        published.put(repositoryKey, publicationWithProject);
    }

    private String normalizeLocation(URI location) {
        String repoUrl = location.toString();
        if (!repoUrl.endsWith("/")) {
            return repoUrl + "/";
        }
        return repoUrl;
    }
}

@@ -2371,24 +2371,434 @@
 dentity)) %7B%0A
+                String firstPublication = previousPublicationWithProject.toString();%0A                String secondPublication = publicationWithProject.toString();%0A                if (secondPublication.compareTo(firstPublication) %3C 0) %7B%0A                    String temp = firstPublication;%0A                    firstPublication = secondPublication;%0A                    secondPublication = temp;%0A                %7D%0A
             
@@ -2964,24 +2964,21 @@
 ons %22 + 
-previous
+first
 Publicat
@@ -2976,35 +2976,24 @@
 tPublication
-WithProject
  + %22 and %22 +
@@ -2989,25 +2989,31 @@
 + %22 and %22 + 
-p
+secondP
 ublicationWi
@@ -3010,27 +3010,16 @@
 lication
-WithProject
  + %22 wil

Enforce some ordering in warning message