net.java.jpatch.maven.ReleaseMojo.java Source code

Java tutorial

Introduction

Here is the source code for net.java.jpatch.maven.ReleaseMojo.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;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import net.java.jpatch.maven.common.AbstractMavenMojo;
import org.apache.commons.lang.SystemUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.scm.ChangeFile;
import org.apache.maven.scm.ChangeSet;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
import org.apache.maven.scm.command.changelog.ChangeLogSet;
import org.apache.maven.scm.repository.ScmRepository;

/**
 * Creates the release package.
 * Download the artifact from the repository.
 * 
 * @author Andrej Petras <andrej@ajka-andrej.com>
 *
 * @goal release
 * @threadSafe 
 * @phase package
 */
public class ReleaseMojo extends AbstractMavenMojo {

    /**
     * Creates the release package.
     * 
     * @throws MojoExecutionException if the method fails.
     * @throws MojoFailureException if the method fails.
     */
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {

        // Search for existing release
        Artifact releaseArtifact = resolveReleaseArtifact();
        File releaseFile = releaseArtifact.getFile();

        // Check existing release
        if (releaseFile != null && releaseFile.exists()) {
            getLog().info(releaseFile.getAbsolutePath());
            getLog().error("Release file already exist! For new release execute first jpatch:release-clean.");
            throw new MojoFailureException("Release package already exist!");
        }

        // Create release directory
        File directory = createTargetDirectory(releaseArtifact.getVersion());

        // Create release properties file.
        Properties releaseProperties = new Properties();

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

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

        // Unpack the libraries in the output directory
        for (MavenProject project : filterProjects) {

            String revision = scmRevision(project);
            releaseProperties.setProperty(project.getId(), revision);

            Artifact artifact = project.getArtifact();
            resolveArtifactFromLocalRepository(artifact);
            unpackArtifactFile(directory, artifact.getFile());
        }

        // Setup the advanced properties to the release property
        SimpleDateFormat sdf = new SimpleDateFormat();
        releaseProperties.put(RELEASE_PROPERTY_DATE, sdf.format(new Date()));
        releaseProperties.put(RELEASE_PROPERTY_USER_NAME, SystemUtils.USER_NAME);
        releaseProperties.put(RELEASE_PROPERTY_JAVA_VERSION, SystemUtils.JAVA_VERSION);
        releaseProperties.put(RELEASE_PROPERTY_OS_NAME, SystemUtils.OS_NAME);

        // Save release properties file
        try {
            File propFile = new File(mavenProject.getBuild().getDirectory(),
                    mavenProject.getBuild().getFinalName() + ".properties");
            releaseProperties.store(new FileOutputStream(propFile), null);
        } catch (IOException e) {
            throw new MojoExecutionException("Error creating the release properties file!", e);
        }

        // Create package file
        String fileName = mavenProject.getBuild().getFinalName();
        File packFile = new File(mavenProject.getBuild().getDirectory(), fileName + ".zip");
        packDirectory(directory, packFile);

        // Attached the package to the project
        releaseArtifact.setFile(packFile);
        mavenProject.addAttachedArtifact(releaseArtifact);

        // Create build file (project.jpatch)
        createBuildFile();
    }

    /**
     * Gets the SCM status from the repository.
     * 
     * @param project the MAVEN project.
     * 
     * @return the SCM status.
     * 
     * @throws MojoExecutionException if the method fails.
     * 
     * TODO: rewrite this method. Exception handling.
     */
    private String scmRevision(MavenProject project) throws MojoExecutionException {
        String result = null;
        try {
            ScmRepository repository = scmManager.makeScmRepository(project.getScm().getConnection());
            ChangeLogScmResult scmResult = scmManager.changeLog(repository, new ScmFileSet(project.getBasedir()),
                    null, null, null);
            if (scmResult != null) {
                ChangeLogSet log = scmResult.getChangeLog();
                if (log != null) {
                    if (log.getChangeSets() != null) {
                        List<ChangeSet> changes = log.getChangeSets();
                        if (!changes.isEmpty()) {
                            ChangeSet change = changes.get(0);
                            if (!change.getFiles().isEmpty()) {
                                List<ChangeFile> files = change.getFiles();
                                ChangeFile file = files.get(0);
                                result = file.getRevision();
                            } else {
                                throw new MojoExecutionException("No revision found! Project: " + project.getId());
                            }
                        } else {
                            throw new MojoExecutionException(
                                    "Missing changes in the repository! Project: " + project.getId());
                        }
                    }
                } else {
                    throw new MojoExecutionException("Missing the SCM log! Project: " + project.getId());
                }
            } else {
                throw new MojoExecutionException("Missing the SCM result! Project: " + project.getId());
            }
        } catch (Exception e) {
            getLog().error("Error check SCM status for project " + project.getId());
            throw new MojoExecutionException("Couldn't configure SCM repository: " + e.getLocalizedMessage(), e);
        }
        return result;
    }

}