All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.iodesystems.whatjdk.maven.Plugin Maven / Gradle / Ivy

package com.iodesystems.whatjdk.maven;

import com.iodesystems.whatjdk.WhatJDK;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Mojo(
    name = "verify",
    defaultPhase = LifecyclePhase.PACKAGE,
    requiresProject = true,
    requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
    threadSafe = true)
public class Plugin extends AbstractMojo {

    @Component
    protected MavenProject project;

    @Parameter(required = true)
    private String allowedJdkVersions;

    public void execute() throws MojoExecutionException, MojoFailureException {
        WhatJDK whatJDK = new WhatJDK();

        final Set allowedVersionsSet = new HashSet();
        for (String allowedVersion : allowedJdkVersions.split(",")) {
            allowedVersionsSet.add(allowedVersion.trim());
        }


        final File file = project.getArtifact().getFile();
        String fileName = file.getAbsolutePath();
        whatJDK.checkFileExtensionCompatibility(fileName);
        try {
            whatJDK.extractClassVersionInfoForJar(fileName, new WhatJDK.Handler() {
                Map> violations = new HashMap>();

                public void handle(String fileName, Set versions) {
                    for (String version : versions) {
                        if (!allowedVersionsSet.contains(version)) {
                            violations.put(fileName, versions);
                            getLog().error(fileName + " contains classes compiled with the following JDK " + versions);
                            break;
                        }
                    }
                }

                @Override
                public void onFinish() throws Exception {
                    if (!violations.isEmpty()) {
                        throw new MojoFailureException("UallowedJdkVersions found.");
                    }
                }
            });
        } catch (MojoFailureException e) {
            throw e;
        } catch (Exception e) {
            throw new MojoExecutionException("Error running WhatJDK", e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy