All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.util.maven.plugin.rmic.AbstractRmicMojo Maven / Gradle / Ivy

The newest version!
/**
 * OW2 Util
 * Copyright (C) 2007-2010 Bull S.A.S.
 * Contact: [email protected]
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id: AbstractRmicMojo.java 5515 2010-05-31 10:28:04Z loris $
 * --------------------------------------------------------------------------
 */

/*
 * Copyright 2005-2006 The Apache Software Foundation.
 *
 * 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 org.ow2.util.maven.plugin.rmic;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.taskdefs.Rmic;
import org.apache.tools.ant.taskdefs.Rmic.ImplementationSpecificArgument;
import org.apache.tools.ant.types.Path;
import org.codehaus.plexus.util.StringUtils;

/**
 * Maven Rmic Mojo.
 * @author Gael Lalire
 */
public abstract class AbstractRmicMojo extends AbstractMojo {

    /**
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject mavenProject;

    /**
     * This folder is added to the list of those folders containing source to be
     * compiled. Use this if your ant script generates source code.
     *
     * @parameter expression="${sourceRoot}"
     */
    private File sourceRoot;

    /**
     * This folder is added to the list of those folders containing source to be
     * compiled for testing. Use this if your ant script generates test source
     * code.
     *
     * @parameter expression="${testSourceRoot}"
     */
    private File testSourceRoot;

    /**
     * The plugin dependencies.
     *
     * @parameter expression="${plugin.artifacts}"
     * @required
     * @readonly
     */
    private List artifacts;

    /* BEGIN APACHE ANT RMIC (these variables are specific to rmic plugin) */

    /**
     * Compiler.
     *
     * @parameter expression="${compiler}"
     * @readonly
     */
    private String compiler;

    /**
     * Ant rmic task param.
     * @parameter expression="${classname}"
     * @required
     */
    private String classname;

    /**
     * Ant rmic task param.
     * @parameter expression="${stubVersion}"
     */
    private String stubVersion;

    /**
     * Ant rmic task param.
     * @parameter expression="${verify}" default-value="false"
     */
    private boolean verify;

    /**
     * Ant rmic task param.
     * @parameter expression="${filtering}" default-value="false"
     */
    private boolean filtering;

    /**
     * Ant rmic task param.
     * @parameter expression="${iiop}" default-value="false"
     */
    private boolean iiop;

    /**
     * Ant rmic task param.
     * @parameter expression="${iiopOpts}"
     */
    private String iiopOpts;

    /**
     * Ant rmic task param.
     * @parameter expression="${idl}" default-value="false"
     */
    private boolean idl;

    /**
     * Ant rmic task param.
     * @parameter expression="${idlOpts}"
     */
    private String idlOpts;

    /**
     * Ant rmic task param.
     * @parameter expression="${debug}" default-value="false"
     */
    private boolean debug;

    // TODO extDir create a pojo for creation of a path
    // IGNORE private Path compileClasspath;
    // IGNORE private Path extDirs;
    // IGNORE private boolean includeAntRuntime = true;
    // IGNORE private boolean includeJavaRuntime = false;
    // IGNORE private Vector compileList = new Vector();
    // IGNORE private ClassLoader loader = null;
    // IGNORE private FacadeTaskHelper facade;

    /* END APACHE ANT RMIC */

    /**
     * Compiler args.
     * @parameter expression="${compilerarg}"
     */
    private String compilerarg;

    /**
     * Create ant task with properties of the mojo.
     * @return Ant target which contain rmic Task
     * @throws DependencyResolutionRequiredException when getCompileClasspathElements failed
     */
    @SuppressWarnings("unchecked")
    public Target createAntTask() throws DependencyResolutionRequiredException {
        Target target = new Target();
        Project antProject = new Project();
        target.setName("");
        antProject.setName("DummyProject");
        target.setProject(antProject);
        antProject.addTarget(target);

        Rmic antRmic = new Rmic();
        antRmic.setTaskName("rmic");
        antRmic.setProject(antProject);
        antProject.init();

        final ArrayList taskClasspath = new ArrayList();
        taskClasspath.addAll(mavenProject.getCompileClasspathElements());
        for (Object a : mavenProject.getDependencyArtifacts()) {
            final File file = ((Artifact) a).getFile();
            if (file != null) {
                taskClasspath.add(file.getPath());
            }
        }

        Path p = new Path(antProject);
        p.setPath(StringUtils.join(taskClasspath.iterator(), File.pathSeparator));
        antRmic.setClasspath(p);

        antRmic.setBase(getBaseDir());
        antRmic.setClassname(classname);
        if (compiler == null || compiler.equalsIgnoreCase("sun")) {
            antRmic.setCompiler("org.ow2.util.maven.plugin.rmic.SunRmicProxy");
        } else {
            antRmic.setCompiler(compiler);
        }
        antRmic.setSourceBase(sourceRoot);
        antRmic.setStubVersion(stubVersion);
        antRmic.setVerify(verify);
        antRmic.setFiltering(filtering);
        antRmic.setIiop(iiop);
        antRmic.setIiopopts(iiopOpts);
        antRmic.setIdl(idl);
        antRmic.setIdlopts(idlOpts);
        antRmic.setDebug(debug);
        if (compilerarg != null) {
            ImplementationSpecificArgument implementationSpecificArgument = antRmic.createCompilerArg();
            implementationSpecificArgument.setValue(compilerarg);
        }
        target.addTask(antRmic);
        return target;
    }

    /**
     * Execute method of the mojo.
     * @throws MojoExecutionException on error
     */
    public void execute() throws MojoExecutionException {
        try {
            executeTasks(createAntTask());
        } catch (DependencyResolutionRequiredException e) {
            throw new MojoExecutionException("DependencyResolutionRequiredException : ", e);
        }
        if (sourceRoot != null) {
            getLog().info("Registering compile source root " + sourceRoot);
            mavenProject.addCompileSourceRoot(sourceRoot.toString());
        }

        if (testSourceRoot != null) {
            getLog().info("Registering compile test source root " + testSourceRoot);
            mavenProject.addTestCompileSourceRoot(testSourceRoot.toString());
        }
    }

    /**
     * Execute Tasks.
     * @param antTasks the target to execute
     * @throws MojoExecutionException on error
     */
    private void executeTasks(final Target antTasks) throws MojoExecutionException {
        try {
            // TODO refactor - place the manipulation of the expressionEvaluator
            // into a separated class.
            // ExpressionEvaluator exprEvaluator = (ExpressionEvaluator)
            // antTasks.getProject().getReference(AntTargetConverter.MAVEN_EXPRESSION_EVALUATOR_ID);

            Project antProject = antTasks.getProject();

            // PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(
            // antProject );
            // propertyHelper.setNext( new AntPropertyHelper( exprEvaluator,
            // getLog() ) );

            DefaultLogger antLogger = new DefaultLogger();
            antLogger.setOutputPrintStream(System.out);
            antLogger.setErrorPrintStream(System.err);
            antLogger.setMessageOutputLevel(Project.MSG_INFO);

            antProject.addBuildListener(antLogger);
            antProject.setBaseDir(mavenProject.getBasedir());

            Path p = new Path(antProject);
            p.setPath(StringUtils.join(mavenProject.getArtifacts().iterator(), File.pathSeparator));
            antProject.addReference("maven.dependency.classpath", p);
            p = new Path(antProject);
            p.setPath(StringUtils.join(mavenProject.getCompileClasspathElements().iterator(), File.pathSeparator));
            antProject.addReference("maven.compile.classpath", p);
            p = new Path(antProject);
            p.setPath(StringUtils.join(mavenProject.getRuntimeClasspathElements().iterator(), File.pathSeparator));
            antProject.addReference("maven.runtime.classpath", p);
            p = new Path(antProject);
            p.setPath(StringUtils.join(mavenProject.getTestClasspathElements().iterator(), File.pathSeparator));
            antProject.addReference("maven.test.classpath", p);
            /* set maven.plugin.classpath with plugin dependencies */
            List list = new ArrayList(artifacts.size());
            for (Artifact a : artifacts) {
                File file = a.getFile();
                if (file == null) {
                    throw new DependencyResolutionRequiredException(a);
                }
                list.add(file.getPath());
            }
            p = new Path(antProject);
            p.setPath(StringUtils.join(list.iterator(), File.pathSeparator));
            antProject.addReference("maven.plugin.classpath", p);
            antTasks.execute();
        } catch (Throwable e) {
            throw new MojoExecutionException("Error executing ant tasks ", e);
        }
    }

    /**
     * Return the location to get the compiled files.
     * @return the location to get the compiled files
     */
    public abstract File getBaseDir();

    /**
     * @return the mavenProject
     */
    public MavenProject getMavenProject() {
        return mavenProject;
    }

    /**
     * @param mavenProject the mavenProject to set
     */
    public void setMavenProject(final MavenProject mavenProject) {
        this.mavenProject = mavenProject;
    }

    /**
     * @return the sourceRoot
     */
    public File getSourceRoot() {
        return sourceRoot;
    }

    /**
     * @param sourceRoot the sourceRoot to set
     */
    public void setSourceRoot(final File sourceRoot) {
        this.sourceRoot = sourceRoot;
    }

    /**
     * @return the testSourceRoot
     */
    public File getTestSourceRoot() {
        return testSourceRoot;
    }

    /**
     * @param testSourceRoot the testSourceRoot to set
     */
    public void setTestSourceRoot(final File testSourceRoot) {
        this.testSourceRoot = testSourceRoot;
    }

    /**
     * @return the artifacts
     */
    public List getArtifacts() {
        return artifacts;
    }

    /**
     * @param artifacts the artifacts to set
     */
    public void setArtifacts(final List artifacts) {
        this.artifacts = artifacts;
    }

    /**
     * @return the compiler
     */
    public String getCompiler() {
        return compiler;
    }

    /**
     * @param compiler the compiler to set
     */
    public void setCompiler(final String compiler) {
        this.compiler = compiler;
    }

    /**
     * @return the classname
     */
    public String getClassname() {
        return classname;
    }

    /**
     * @param classname the classname to set
     */
    public void setClassname(final String classname) {
        this.classname = classname;
    }

    /**
     * @return the stubVersion
     */
    public String getStubVersion() {
        return stubVersion;
    }

    /**
     * @param stubVersion the stubVersion to set
     */
    public void setStubVersion(final String stubVersion) {
        this.stubVersion = stubVersion;
    }

    /**
     * @return the iiopOpts
     */
    public String getIiopOpts() {
        return iiopOpts;
    }

    /**
     * @param iiopOpts the iiopOpts to set
     */
    public void setIiopOpts(final String iiopOpts) {
        this.iiopOpts = iiopOpts;
    }

    /**
     * @return the idlOpts
     */
    public String getIdlOpts() {
        return idlOpts;
    }

    /**
     * @param idlOpts the idlOpts to set
     */
    public void setIdlOpts(final String idlOpts) {
        this.idlOpts = idlOpts;
    }

    /**
     * @return the compilerarg
     */
    public String getCompilerarg() {
        return compilerarg;
    }

    /**
     * @param compilerarg the compilerarg to set
     */
    public void setCompilerarg(final String compilerarg) {
        this.compilerarg = compilerarg;
    }

}