001    package com.jnaerator;
002    
003    /*
004     * Copyright 2001-2005 The Apache Software Foundation.
005     *
006     * Licensed under the Apache License, Version 2.0 (the "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    import org.apache.maven.plugin.AbstractMojo;
020    import org.apache.maven.plugin.MojoExecutionException;
021    import org.apache.maven.project.MavenProject;
022    
023    import com.ochafik.lang.jnaerator.*;
024    
025    import java.io.File;
026    import java.io.FileWriter;
027    import java.io.IOException;
028    import java.util.*;
029    
030    /**
031     * Launch JNAerator to wrap native libraries in Java for use with JNA.
032     * @goal generate
033     * @phase generate-sources
034     * @description Launches JNAerator with the command-line arguments contained in src/main/jnaerator/config.jnaerator. To launch from command line, use "mvn jnaerator:generate"
035     */
036    public class JNAeratorMojo
037        extends AbstractMojo
038    {
039        /**
040         * Path to JNAerator configuration file.
041         * @parameter expression="${project.basedir}/src/main/jnaerator/config.jnaerator"
042         * @required
043         */
044        private File config;
045    
046    
047        /**
048         * Whether JNAerator should output helper Scala classes (experimental)
049         * @parameter expression="false"
050         * @required
051         */
052        private boolean generateScala;
053    
054        /**
055         * Output directory for JNAerated Java sources.
056         * @parameter expression="${project.build.directory}/generated-sources/java"
057         * @optional
058         */
059        private File javaOutputDirectory;
060    
061        /**
062         * Output directory for JNAerated Scala sources.
063         * @parameter expression="${project.build.directory}/generated-sources/scala"
064         * @optional
065         */
066        private File scalaOutputDirectory;
067    
068        /**
069         * @parameter expression="${project}"
070         * @required
071         * @readonly
072         * @since 1.0
073         */
074        private MavenProject project;
075    
076            static File canonizeDir(File f) throws IOException {
077            if (!f.exists())
078                f.mkdirs();
079            return f.getCanonicalFile();
080        }
081        public void execute()
082            throws MojoExecutionException
083        {
084            if (!config.exists()) {
085                    getLog().info("No JNAerator config file '" + config + "' found");
086                    return;
087            }
088            try
089            {
090                List<String> args = new ArrayList<String>();
091    
092                args.add(config.getAbsolutePath());
093                
094                // Override settings from config file :
095                args.add("-mode");
096                args.add(JNAeratorConfig.OutputMode.Directory.name());
097                args.add("-f");
098                args.add("-o");
099                
100                File javaDir = canonizeDir(javaOutputDirectory);
101                args.add(javaDir.toString());
102                
103                project.addCompileSourceRoot(javaDir.toString());
104    
105                if (generateScala) {
106                    args.add("-scalaOut");
107                    args.add(canonizeDir(scalaOutputDirectory).toString());
108                }
109    
110                com.ochafik.lang.jnaerator.JNAerator.main(args.toArray(new String[0]));
111            }
112            catch (Exception e )
113            {
114                throw new MojoExecutionException( "Error running JNAerator on " + config, e );
115            }
116        }
117    }