package io.freefair.gradle.plugins.maven.central;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.publish.maven.tasks.GenerateMavenPom;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.language.base.plugins.LifecycleBasePlugin;

/**
 * @author Lars Grefer
 */
public class ValidatePomsPlugin implements Plugin<Project> {

    @Override
    public void apply(Project project) {
        project.getTasks().withType(GenerateMavenPom.class, generateMavenPom -> {

            String checkTaskName;

            if (generateMavenPom.getName().startsWith("generate")) {
                checkTaskName = "validate" + generateMavenPom.getName().substring(8);
            }
            else {
                checkTaskName = "validate" + generateMavenPom.getName();
            }

            TaskProvider<ValidateMavenPom> validateMavenPom = project.getTasks().register(checkTaskName, ValidateMavenPom.class);

            project.getPlugins().withType(LifecycleBasePlugin.class, lbp -> {
                project.getTasks().named(LifecycleBasePlugin.CHECK_TASK_NAME).configure(check -> {
                    check.dependsOn(validateMavenPom);
                });
            });

            project.afterEvaluate(p -> {
                validateMavenPom.configure(v -> {
                    v.dependsOn(generateMavenPom);
                    v.getPomFile().set(generateMavenPom.getDestination());
                });
            });
        });
    }
}

@@ -105,16 +105,44 @@
 roject;%0A
+import org.gradle.api.Task;%0A
 import o
@@ -193,16 +193,16 @@
 venPom;%0A
-
 import o
@@ -441,24 +441,197 @@
  project) %7B%0A
+        TaskProvider%3CTask%3E validateAll = project.getTasks().register(%22validatePomFiles%22, t -%3E %7B%0A            t.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);%0A        %7D);%0A%0A
         proj
@@ -1012,16 +1012,16 @@
      %7D%0A%0A
-
         
@@ -1139,16 +1139,400 @@
 om.class
+, t -%3E %7B%0A                t.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);%0A                String description = generateMavenPom.getDescription();%0A                if (description != null) %7B%0A                    t.setDescription(description.replace(%22Generates%22, %22Validates%22));%0A                %7D%0A            %7D);%0A%0A            validateAll.configure(all -%3E all.dependsOn(validateMavenPom)
 );%0A%0A    

Add validatePomFiles Task