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

org.lint.azzert.LintAssertMojo Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
package org.lint.azzert;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.lint.azzert.context.MethodMetadata;
import org.lint.azzert.processor.LintAssertBuildParameters;
import org.lint.azzert.processor.LintAssertProcessor;
import org.lint.azzert.strategy.output.ToStringStrategy;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import java.util.Set;

/*
Test stand-alone:
mvn install lint-assert:lint-assert
 */
@Mojo(name = "lint-assert")
public class LintAssertMojo extends AbstractMojo {

    @Parameter( property = "packageName", required = false)
    private String packageName;

    @Parameter( property = "includeClasspathJars", defaultValue = "false", required = false)
    private boolean includeClasspathJars;

    @Parameter( property = "verbose", defaultValue = "false", required = false)
    private boolean verbose;

    @Parameter(defaultValue = "${project}", readonly = true, required = true)
    protected MavenProject project;

    public void execute() {
        getLog().info("Hello from LintAssertMojo!;;" + this.toString());
        getLog().info(this.toString());
        try {
            Set methodMetadata = new LintAssertProcessor(getUrlClassLoader(),
                    new LintAssertBuildParameters(packageName, verbose, includeClasspathJars)).process();
            String result = new ToStringStrategy(methodMetadata).render();
            getLog().info(result);
        } catch (Exception e) {
            getLog().error("Failed to lint", e);
        }
    }

    URLClassLoader getUrlClassLoader() throws DependencyResolutionRequiredException {
        final List testDir = this.project.getCompileClasspathElements();
        testDir.addAll(this.project.getTestClasspathElements());

        URL[] compiledTestsDirs = testDir.stream()
                .map(File::new)
                .map(File::toURI)
                .map(u -> {
                            try {
                                return u.toURL();
                            } catch (MalformedURLException ignore) {
                                return "";
                            }
                        }
                )
                .toArray(URL[]::new);

        if (includeClasspathJars) {
            final PluginDescriptor pluginDescriptor = (PluginDescriptor) getPluginContext().get("pluginDescriptor");
            final ClassRealm classRealm = pluginDescriptor.getClassRealm();
            compiledTestsDirs = ArrayUtils.addAll(compiledTestsDirs,classRealm.getURLs() );
        }

        for (URL compiledTestsDir : compiledTestsDirs) {
            getLog().info("compiledTestsDir::" + compiledTestsDir);
        }

        return new URLClassLoader(compiledTestsDirs, null);
    }

    @Override
    public String toString() {
        return "LintAssertMojo{" +
                "packageName='" + packageName + '\'' +
                ", includeClasspathJars=" + includeClasspathJars +
                ", verbose=" + verbose +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy