001/**
002 * Copyright (C) 2009-2011 FuseSource Corp.
003 * http://fusesource.com
004 * 
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 * 
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.fusesource.hawtjni.maven;
018
019import java.io.File;
020
021import org.apache.maven.plugin.AbstractMojo;
022import org.apache.maven.plugin.MojoExecutionException;
023import org.apache.maven.project.MavenProject;
024import org.apache.maven.project.MavenProjectHelper;
025import org.codehaus.plexus.archiver.Archiver;
026import org.codehaus.plexus.archiver.manager.ArchiverManager;
027
028/**
029 * This goal creates a source zip file of the native build
030 * module and attaches it to the build so that it can get 
031 * deployed.
032 * 
033 * @goal package-source
034 * @phase package
035 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
036 */
037public class PackageSourceMojo extends AbstractMojo {
038
039    /**
040     * The maven project.
041     * 
042     * @parameter expression="${project}"
043     * @required
044     * @readonly
045     */
046    protected MavenProject project;
047
048    /**
049     * @component
050     * @required
051     * @readonly
052     */
053    private ArchiverManager archiverManager;
054    
055    /**
056     * @component
057     * @required
058     * @readonly
059     */
060    private MavenProjectHelper projectHelper;    
061    
062    /**
063     * The directory where the generated native files are located..
064     * 
065     * @parameter default-value="${project.build.directory}/generated-sources/hawtjni/native-package"
066     */
067    private File packageDirectory;
068    
069    /**
070     * The classifier of the package archive that will be created.
071     * 
072     * @parameter default-value="native-src"
073     */
074    private String sourceClassifier;
075    
076    /**
077     * Should we skip executing the autogen.sh file.
078     * 
079     * @parameter default-value="${skip-autogen}"
080     */
081    private boolean skipAutogen;
082    
083    
084    public void execute() throws MojoExecutionException {
085        try {
086
087            String packageName = project.getArtifactId()+"-"+project.getVersion()+"-"+sourceClassifier;
088            File packageFile = new File(new File(project.getBuild().getDirectory()), packageName+".zip");
089
090            // Verify the the configure script got generated before packaging.
091            File configure = new File(packageDirectory, "configure");
092            if( !skipAutogen && !configure.exists() ) {
093                // Looks like this platform could not generate the 
094                // configure script.  So don't install deploy
095                // partially created source package.
096                getLog().info("");
097                getLog().warn("Will NOT package the native sources to: "+packageFile);
098                getLog().info("  Native source build directory did not contain a 'configure' script.");
099                getLog().info("  To ignore this warnning and package it up anyways configure the plugin with: <skipAutogen>true</skipAutogen>");
100                getLog().info("");
101                return;
102            }        
103            
104            Archiver archiver = archiverManager.getArchiver( "zip" );
105            archiver.setDestFile( packageFile);
106            archiver.setIncludeEmptyDirs(true);
107            archiver.addDirectory(packageDirectory, packageName+"/");
108            archiver.createArchive();
109            projectHelper.attachArtifact( project, "zip", sourceClassifier, packageFile );
110            
111        } catch (Exception e) {
112            throw new MojoExecutionException("packageing failed: "+e, e);
113        } 
114    }
115
116}