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

com.fluxtion.maven.FluxtionScanToGenMojo Maven / Gradle / Ivy

There is a newer version: 3.0.14
Show newest version
/*
 * Copyright (C) 2017-2020 V12 Technology Limited
 *
 * This file is part of Fluxtion.
 *
 * 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 com.fluxtion.maven;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;

/**
 *
 * @author V12 Technology Ltd.
 */
@Mojo(name = "scan",
    requiresProject = true,
    requiresDependencyResolution = ResolutionScope.COMPILE,
    defaultPhase = LifecyclePhase.COMPILE
)
public class FluxtionScanToGenMojo extends AbstractMojo {

    private static final String JAVA_CLASS_PATH = "java.class.path";
    /**
     * The output directory for build artifacts generated by fluxtion. Absolute paths are preceded with "/" otherwise
     * the path relative to the project root directory
     */
    @Parameter(property = "buildDirectory", defaultValue = "target/classes")
    private String buildDirectory;
    private URLClassLoader classLoader;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        if (System.getProperty("skipFluxtion") != null) {
            getLog().info("Fluxtion generation skipped.");
        } else {
            try {
                if (buildDirectory == null) {
                    buildDirectory = project.getBasedir().getCanonicalPath() + "/target/classes";
                } else if (!buildDirectory.startsWith("/")) {
                    buildDirectory = project.getBasedir().getCanonicalPath() + "/" + buildDirectory;
                }
                buildFluxtionClassLoader();
                //generate static context
                Class> apClazz = (Class>) classLoader.loadClass("com.fluxtion.generator.compiler.ClassProcessorDispatcher");
                apClazz.newInstance().accept(new File(buildDirectory).toURI().toURL(), project.getBasedir());
            } catch (Exception exception) {
                getLog().error(exception);
                throw new MojoExecutionException("problem setting building fluxtion class loader", exception);
            }
        }
    }

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

    private URLClassLoader buildFluxtionClassLoader() throws MojoExecutionException, MalformedURLException, DependencyResolutionRequiredException {
        List elements = project.getCompileClasspathElements();
        URL[] urls = new URL[elements.size()];
        for (int i = 0; i < elements.size(); i++) {
            File cpFile = new File(elements.get(i));
            getLog().debug("Adding element from runtime to classpath:" + cpFile.getPath());
            if (cpFile.isDirectory()) {
                urls[i] = cpFile.toURI().toURL();
            } else {
                urls[i] = new URL("jar:" + cpFile.toURI().toURL() + "!/");
            }
            addClassPath(cpFile.getPath());
        }
        getLog().debug("user classpath URL list:" + Arrays.toString(urls));
        classLoader = URLClassLoader.newInstance(urls);
        return classLoader;
    }


    public static boolean addClassPath(String dir) {
        File file = new File(dir);
        if (file.exists()) {
            String path;
            try {
                path = file.getCanonicalPath();
            } catch (IOException ignored) {
                path = file.getAbsolutePath();
            }
            if (!Arrays.asList(System.getProperty(JAVA_CLASS_PATH).split(File.pathSeparator)).contains(path)) {
                System.setProperty(JAVA_CLASS_PATH, System.getProperty(JAVA_CLASS_PATH) + File.pathSeparator + path);
            }

        } else {
            return false;
        }
        return true;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy