/*
 * #%L
 * Wisdom-Framework
 * %%
 * Copyright (C) 2013 - 2014 Wisdom Framework
 * %%
 * 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.
 * #L%
 */
package org.wisdom.maven.mojos;


import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
import org.wisdom.maven.Constants;
import org.wisdom.maven.utils.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Mojo preparing the Wisdom runtime.
 */
@Mojo(name = "initialize", threadSafe = false,
        requiresDependencyResolution = ResolutionScope.COMPILE,
        requiresProject = true,
        defaultPhase = LifecyclePhase.INITIALIZE)
public class InitializeMojo extends AbstractWisdomMojo {

    /**
     * If set to {@literal true}, it does not collect transitive dependencies. This means that bundles that are
     * transitive dependencies of the current project won't be copied.
     */
    @Parameter(defaultValue = "false")
    private boolean excludeTransitive;

    /**
     * If set to {@literal false}, it enables the analysis and the collection of transitive webjars.
     */
    @Parameter(defaultValue = "true")
    private boolean excludeTransitiveWebJars;

    /**
     * Deploy the test dependencies to run tests. This option should be used with caution as it may add to much
     * bundles to your runtime.
     */
    @Parameter(defaultValue = "false")
    private boolean deployTestDependencies;

    /**
     * The dependency graph builder to use.
     */
    @Component(hint = "default")
    private DependencyGraphBuilder dependencyGraphBuilder;


    /**
     * A parameter indicating that the current project is using the 'base runtime' instead of the 'full runtime'. This
     * option should only be used by components developed by Wisdom and being part of the 'full runtime'.
     * <p>
     * This parameter is deprecated, used the "distribution" parameter instead.
     */
    @Parameter
    @Deprecated
    boolean useBaseRuntime;

    /**
     * A parameter to select the Wisdom distribution to use. Are accepted a profile's name among: {base, equinox,
     * regular} (regular is the default distribution), or artifact coordinated given under the form:
     * GROUP_ID:ARTIFACT_ID:EXTENSION:CLASSIFIER:VERSION. If not set the regular distribution is used.
     */
    @Parameter(defaultValue = "regular")
    String wisdomRuntime;

    /**
     * A parameter indicating whether or not we should remove from the bundle transitive copy some well-known
     * error-prone bundles.
     */
    @Parameter(defaultValue = "true")
    public boolean useDefaultExclusions;

    /**
     * Execute, first expands the wisdom runtime from zip if needed and if the {@link
     * #wisdomDirectory} parameter is not set. Then copies dependencies from the {@literal
     * compile} scope (including transitives if not disabled).
     *
     * @throws MojoExecutionException when the copy of compile dependencies fails,
     *                                or OSGi packaging fails, or storing dependencies in a JSON
     *                                file fails.
     */
    @Override
    public void execute() throws MojoExecutionException {
        getLog().debug("Wisdom Maven Plugin version: " + BuildConstants.get("WISDOM_PLUGIN_VERSION"));

        if (useBaseRuntime) {
            wisdomRuntime = "base";
        }

        // Expands if needed.
        if (wisdomDirectory == null && WisdomRuntimeExpander.expand(this,
                getWisdomRootDirectory(), wisdomRuntime)) {
            getLog().info("Wisdom Runtime installed in " + getWisdomRootDirectory().getAbsolutePath());
        }

        // Copy compile dependencies that are bundles to the application directory.
        try {
            DependencyCopy.copyBundles(this, dependencyGraphBuilder, !excludeTransitive, deployTestDependencies,
                    !useDefaultExclusions);
            DependencyCopy.extractWebJars(this, dependencyGraphBuilder, !excludeTransitiveWebJars);
        } catch (IOException e) {
            throw new MojoExecutionException("Cannot copy dependencies", e);
        }

        // Install node.
        try {
            getNodeManager().installIfNotInstalled();
        } catch (IOException e) {
            getLog().error("Cannot install node and npm - asset processing won't work", e);
        }

        // Prepare OSGi packaging
        try {
            Properties properties = MavenUtils.getDefaultProperties(project);
            write(properties);
        } catch (IOException e) {
            throw new MojoExecutionException("Cannot write the OSGi metadata to " + Constants.OSGI_PROPERTIES, e);
        }

        // Store dependencies as JSON
        try {
            MavenUtils.dumpDependencies(project);
        } catch (IOException e) {
            throw new MojoExecutionException("Cannot write the dependency metadata to " + Constants.DEPENDENCIES_FILE, e);
        }

        // Ensure that the conf file as a secret.
        try {
            ApplicationSecretGenerator.ensureOrGenerateSecret(project, getLog());
        } catch (IOException e) {
            throw new MojoExecutionException("Update the application configuration to set the secret key", e);
        }
    }

    private void write(Properties properties) throws IOException {
        File file = new File(project.getBasedir(), Constants.OSGI_PROPERTIES);
        file.getParentFile().mkdirs();
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            properties.store(fos, "");
        } finally {
            IOUtils.closeQuietly(fos);
        }
    }


}

@@ -684,16 +684,56 @@
 ojos;%0A%0A%0A
+import org.apache.commons.io.FileUtils;%0A
 import o
@@ -5931,32 +5931,524 @@
 %22, e);%0A        %7D
+%0A%0A        // Cleanup pipeline error is any%0A        clearPipelineError();%0A    %7D%0A%0A    /**%0A     * Deletes all error report from the pipeline error directory.%0A     */%0A    private void clearPipelineError() %7B%0A        File dir = new File(buildDirectory, %22pipeline%22);%0A        if (dir.isDirectory()) %7B%0A            try %7B%0A                FileUtils.cleanDirectory(dir);%0A            %7D catch (IOException e) %7B%0A                getLog().warn(%22Cannot clean the pipeline directory%22, e);%0A            %7D%0A        %7D
 %0A    %7D%0A%0A    priv

Fix #250