net.java.jpatch.maven.common.AbstractPatchMojo.java Source code

Java tutorial

Introduction

Here is the source code for net.java.jpatch.maven.common.AbstractPatchMojo.java

Source

/*
 * Copyright 2012 Andrej Petras <andrej@ajka-andrej.com>.
 *
 * 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 net.java.jpatch.maven.common;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.scm.ScmFile;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmFileStatus;
import org.apache.maven.scm.ScmRevision;
import org.apache.maven.scm.command.diff.DiffScmResult;
import org.apache.maven.scm.repository.ScmRepository;
import org.codehaus.plexus.util.FileUtils;

/**
 *
 * @author Andrej Petras <andrej@ajka-andrej.com>
 */
public abstract class AbstractPatchMojo extends AbstractMavenMojo {

    /**
     * Create the build item.
     *
     * @return the build event.
     *
     * @throws MojoExecutionException if the method fails.
     */
    protected List<MavenProject> loadMavenProjectsForPatch(Properties releaseProperties, Artifact patchArtifact)
            throws MojoExecutionException {

        // Load projects
        List<MavenProject> mavenProjects = loadMavenProjects();

        // Filter the projects
        List<MavenProject> filterProjects = filterMavenProjects(mavenProjects, getPackages());

        // Create patch target directory
        File diffDirectory = createTargetDirectory(patchArtifact.getVersion());

        // Check the SCM status
        List<MavenProject> scmProjects = new ArrayList<MavenProject>();
        for (MavenProject project : filterProjects) {
            String revision = releaseProperties.getProperty(project.getId(), null);
            if (revision != null) {
                if (checkSCM(project, revision, diffDirectory)) {
                    scmProjects.add(project);
                }
            } else {
                getLog().warn("Missing revision for the project " + project.getId());
            }
        }
        return scmProjects;
    }

    /**
     * Check the SCM status of the project.
     *
     * @param project the MAVEN project.
     * @param begin the SCM ID.
     * @param end the SCM ID.
     *
     * @return
     *
     * @throws MojoExecutionException if the method fails.
     */
    private boolean checkSCM(MavenProject project, String revision, File diffDirectory)
            throws MojoExecutionException {

        getLog().info("Check SCM for the project " + project.getId());

        boolean result = false;
        boolean ignoreUnknown = true;

        DiffScmResult scmResult = null;
        try {
            ScmRepository repository = scmManager.makeScmRepository(project.getScm().getConnection());
            scmResult = scmManager.diff(repository, new ScmFileSet(project.getBasedir()), new ScmRevision(revision),
                    null);
        } catch (Exception e) {
            getLog().error("Error check SCM status for project " + project.getId());
            throw new MojoExecutionException("Couldn't configure SCM repository: " + e.getLocalizedMessage(), e);
        }

        if (scmResult != null && scmResult.getChangedFiles() != null) {
            List<ScmFile> changedFiles = scmResult.getChangedFiles();
            for (ScmFile changedScmFile : changedFiles) {
                ScmFileStatus status = changedScmFile.getStatus();
                if (!status.isStatus()) {
                    getLog().debug("Not a diff: " + status);
                    continue;
                }
                if (ignoreUnknown && ScmFileStatus.UNKNOWN.equals(status)) {
                    getLog().debug("Ignoring unknown");
                    continue;
                }

                getLog().info(changedScmFile.getStatus().toString() + " " + changedScmFile.getPath());
                result = true;
            }

            if (result) {
                File diffFile = new File(diffDirectory, project.getArtifactId() + project.getVersion() + ".diff");
                try {
                    getLog().info("Create new diff file: " + diffFile.getAbsolutePath());
                    diffFile.createNewFile();
                    FileUtils.fileWrite(diffFile.getAbsolutePath(), scmResult.getPatch());
                } catch (IOException ex) {
                    Logger.getLogger(AbstractPatchMojo.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        getLog().info("");
        return result;
    }
}