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

org.bluestemsoftware.open.eoa.plugin.resources.ResourcesMojo Maven / Gradle / Ivy

/**
 * Copyright 2008 Bluestem Software LLC.  All Rights Reserved.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 * 
 */

package org.bluestemsoftware.open.eoa.plugin.resources;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.FileUtils;

/**
 * Copies resources to EOA-INF/classes directory instead of archive root, to copy contents of
 * 'lib' dir to EOA-INF/lib directory of archive and to copy contents of META-INF project dir
 * to META-INF dir of archive. Also removed filtering functionality (older version of
 * plexus-utils on classpath caused NoSuchMethodException. updating our pom with version 1.4.1
 * didn't work ...)
 * 
 * @goal resources
 * @phase process-resources
 */
public class ResourcesMojo extends AbstractResourcesMojo {

    public void execute() throws MojoExecutionException {
        copyResources(resources, new File(outputDirectory, "EOA-INF/classes/"));
        copyMetaInfDir(new File(basedir, "META-INF"), new File(outputDirectory, "META-INF/"));
        copyLibDir(new File(basedir, "lib"), new File(outputDirectory, "EOA-INF/lib/"));
    }

    /*
     * copies data from META-INF project dir to META-INF directory in output dir
     */
    private void copyMetaInfDir(File sourceDir, File targetDir) throws MojoExecutionException {

        if (sourceDir.exists() == false || sourceDir.isDirectory() == false) {
            throw new MojoExecutionException("Missing required project dir " + sourceDir.getAbsolutePath());
        }

        if (targetDir.exists() == false) {
            targetDir.mkdir();
        }

        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setBasedir(sourceDir);
        scanner.setIncludes(DEFAULT_INCLUDES);
        scanner.addDefaultExcludes();
        scanner.scan();

        List includedFiles = Arrays.asList(scanner.getIncludedFiles());

        for (Iterator j = includedFiles.iterator(); j.hasNext();) {
            String name = j.next();
            String destination = name;
            File source = new File(sourceDir, name);
            File destinationFile = new File(targetDir, destination);
            if (!destinationFile.getParentFile().exists()) {
                destinationFile.getParentFile().mkdirs();
            }
            try {
                FileUtils.copyFile(source, destinationFile);
            } catch (IOException e) {
                throw new MojoExecutionException("Error copying resource " + source, e);
            }
        }

    }

    /*
     * copies data from optional lib project dir to EOA-INF/lib directory in output dir
     */
    private void copyLibDir(File sourceDir, File targetDir) throws MojoExecutionException {

        if (sourceDir.exists() == false) {
            return;
        }

        if (sourceDir.isDirectory() == false) {
            throw new MojoExecutionException("Project dir " + sourceDir.getAbsolutePath() + " is not a directory.");
        }

        if (targetDir.exists() == false) {
            targetDir.mkdir();
        }

        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setBasedir(sourceDir);
        scanner.setIncludes(DEFAULT_INCLUDES);
        scanner.addDefaultExcludes();
        scanner.scan();

        List includedFiles = Arrays.asList(scanner.getIncludedFiles());

        for (Iterator j = includedFiles.iterator(); j.hasNext();) {
            String name = j.next();
            String destination = name;
            File source = new File(sourceDir, name);
            File destinationFile = new File(targetDir, destination);
            if (!destinationFile.getParentFile().exists()) {
                destinationFile.getParentFile().mkdirs();
            }
            try {
                FileUtils.copyFile(source, destinationFile);
            } catch (IOException e) {
                throw new MojoExecutionException("Error copying resource " + source, e);
            }
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy