package org.springframework.gradle.maven;

import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.publish.VariantVersionMappingStrategy;
import org.gradle.api.publish.VersionMappingStrategy;
import org.gradle.api.publish.maven.MavenPom;
import org.gradle.api.publish.maven.MavenPomDeveloperSpec;
import org.gradle.api.publish.maven.MavenPomIssueManagement;
import org.gradle.api.publish.maven.MavenPomLicenseSpec;
import org.gradle.api.publish.maven.MavenPomOrganization;
import org.gradle.api.publish.maven.MavenPomScm;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;

public class MavenPublishingConventionsPlugin implements Plugin<Project> {
	@Override
	public void apply(Project project) {
		project.getPlugins().withType(MavenPublishPlugin.class).all(new Action<MavenPublishPlugin>() {
			@Override
			public void execute(MavenPublishPlugin mavenPublish) {
				PublishingExtension publishing = project.getExtensions().getByType(PublishingExtension.class);
				publishing.getPublications().withType(MavenPublication.class)
						.all((mavenPublication) -> {
							mavenPublication.versionMapping(new Action<VersionMappingStrategy>() {
								@Override
								public void execute(VersionMappingStrategy versionStrategy) {
									versionStrategy.usage("java-runtime", new Action<VariantVersionMappingStrategy>() {
										@Override
										public void execute(VariantVersionMappingStrategy mappingStrategy) {
											mappingStrategy.fromResolutionResult();
										}
									});
								}
							});
							MavenPublishingConventionsPlugin.this.customizePom(mavenPublication.getPom(), project);
						});
				MavenPublishingConventionsPlugin.this.customizeJavaPlugin(project);
			}
		});
	}

	private void customizePom(MavenPom pom, Project project) {
		pom.getUrl().set("https://spring.io/projects/spring-security");
		pom.getName().set(project.provider(project::getName));
		pom.getDescription().set(project.provider(project::getDescription));
		pom.organization(this::customizeOrganization);
		pom.licenses(this::customizeLicences);
		pom.developers(this::customizeDevelopers);
		pom.scm(this::customizeScm);
		pom.issueManagement(this::customizeIssueManagement);
	}

	private void customizeOrganization(MavenPomOrganization organization) {
		organization.getName().set("Pivotal Software, Inc.");
		organization.getUrl().set("https://spring.io");
	}

	private void customizeLicences(MavenPomLicenseSpec licences) {
		licences.license((licence) -> {
			licence.getName().set("Apache License, Version 2.0");
			licence.getUrl().set("https://www.apache.org/licenses/LICENSE-2.0");
		});
	}

	private void customizeDevelopers(MavenPomDeveloperSpec developers) {
		developers.developer((developer) -> {
			developer.getName().set("Pivotal");
			developer.getEmail().set("info@pivotal.io");
			developer.getOrganization().set("Pivotal Software, Inc.");
			developer.getOrganizationUrl().set("https://www.spring.io");
		});
	}

	private void customizeScm(MavenPomScm scm) {
		scm.getConnection().set("scm:git:git://github.com/spring-projects/spring-security.git");
		scm.getDeveloperConnection().set("scm:git:ssh://git@github.com/spring-projects/spring-security.git");
		scm.getUrl().set("https://github.com/spring-projects/spring-security");
	}

	private void customizeIssueManagement(MavenPomIssueManagement issueManagement) {
		issueManagement.getSystem().set("GitHub");
		issueManagement.getUrl().set("https://github.com/spring-projects/spring-security/issues");
	}

	private void customizeJavaPlugin(Project project) {
		project.getPlugins().withType(JavaPlugin.class).all((javaPlugin) -> {
			JavaPluginExtension extension = project.getExtensions().getByType(JavaPluginExtension.class);
			extension.withJavadocJar();
			extension.withSourcesJar();
		});
	}
}

@@ -275,123 +275,8 @@
 on;%0A
-import org.gradle.api.publish.VariantVersionMappingStrategy;%0Aimport org.gradle.api.publish.VersionMappingStrategy;%0A
 impo
@@ -1214,472 +1214,8 @@
  -%3E 
-%7B%0A%09%09%09%09%09%09%09mavenPublication.versionMapping(new Action%3CVersionMappingStrategy%3E() %7B%0A%09%09%09%09%09%09%09%09@Override%0A%09%09%09%09%09%09%09%09public void execute(VersionMappingStrategy versionStrategy) %7B%0A%09%09%09%09%09%09%09%09%09versionStrategy.usage(%22java-runtime%22, new Action%3CVariantVersionMappingStrategy%3E() %7B%0A%09%09%09%09%09%09%09%09%09%09@Override%0A%09%09%09%09%09%09%09%09%09%09public void execute(VariantVersionMappingStrategy mappingStrategy) %7B%0A%09%09%09%09%09%09%09%09%09%09%09mappingStrategy.fromResolutionResult();%0A%09%09%09%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%09%09%09%7D);%0A%09%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%09%7D);%0A%09%09%09%09%09%09%09
 Mave
@@ -1300,17 +1300,8 @@
 ect)
-;%0A%09%09%09%09%09%09%7D
 );%0A%09

Revert "versionMapping"