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.plugins.annotations.Component;
024import org.apache.maven.plugins.annotations.LifecyclePhase;
025import org.apache.maven.plugins.annotations.Mojo;
026import org.apache.maven.plugins.annotations.Parameter;
027import org.apache.maven.project.MavenProject;
028import org.apache.maven.project.MavenProjectHelper;
029import org.codehaus.plexus.archiver.Archiver;
030import org.codehaus.plexus.archiver.manager.ArchiverManager;
031
032/**
033 * This goal creates a source zip file of the native build
034 * module and attaches it to the build so that it can get 
035 * deployed.
036 * 
037 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
038 */
039@Mojo(name = "package-source", defaultPhase = LifecyclePhase.PACKAGE)
040public class PackageSourceMojo extends AbstractMojo {
041
042    /**
043     * The maven project.
044     */
045    @Parameter(defaultValue = "${project}", readonly = true)
046    protected MavenProject project;
047
048    /**
049     */
050    @Component
051    private ArchiverManager archiverManager;
052    
053    /**
054     */
055    @Component
056    private MavenProjectHelper projectHelper;    
057    
058    /**
059     * The directory where the generated native files are located..
060     */
061    @Parameter(defaultValue = "${project.build.directory}/generated-sources/hawtjni/native-package")
062    private File packageDirectory;
063    
064    /**
065     * The classifier of the package archive that will be created.
066     */
067    @Parameter(defaultValue = "native-src")
068    private String sourceClassifier;
069    
070    /**
071     * Should we skip executing the autogen.sh file.
072     */
073    @Parameter(defaultValue = "${skip-autogen}")
074    private boolean skipAutogen;
075    
076    
077    public void execute() throws MojoExecutionException {
078        try {
079
080            String packageName = project.getArtifactId()+"-"+project.getVersion()+"-"+sourceClassifier;
081            File packageFile = new File(new File(project.getBuild().getDirectory()), packageName+".zip");
082
083            // Verify the the configure script got generated before packaging.
084            File configure = new File(packageDirectory, "configure");
085            if( !skipAutogen && !configure.exists() ) {
086                // Looks like this platform could not generate the 
087                // configure script.  So don't install deploy
088                // partially created source package.
089                getLog().info("");
090                getLog().warn("Will NOT package the native sources to: "+packageFile);
091                getLog().info("  Native source build directory did not contain a 'configure' script.");
092                getLog().info("  To ignore this warning and package it up anyways, configure the plugin with: <skipAutogen>true</skipAutogen>");
093                getLog().info("");
094                return;
095            }        
096            
097            Archiver archiver = archiverManager.getArchiver( "zip" );
098            archiver.setDestFile( packageFile);
099            archiver.setIncludeEmptyDirs(true);
100            archiver.addDirectory(packageDirectory, packageName+"/");
101            archiver.createArchive();
102            projectHelper.attachArtifact( project, "zip", sourceClassifier, packageFile );
103            
104        } catch (Exception e) {
105            throw new MojoExecutionException("packageing failed: "+e, e);
106        } 
107    }
108
109}