Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.saumya.mojo.jruby;
  
  import java.io.File;
  
 
Base for all JRuby mojos.

RequiresProject:
false
 
 public abstract class AbstractJRubyMojo extends AbstractMojo {
 
     protected static final String JRUBY_COMPLETE = "jruby-complete";
 
     protected static final String JRUBY_CORE = "jruby-core";
 
     protected static final Object JRUBY_STDLIB = "jruby-stdlib";
 
     protected static String DEFAULT_JRUBY_VERSION = "1.6.7.2";


    
common arguments
Command line -Dargs=...

Parameter:
expression="${args}"
 
     protected String args;

    
jvm arguments for the java command executing jruby
Command line -Djruby.jvmargs=...

Parameter:
expression="${jruby.jvmargs}"
 
     protected String jrubyJvmArgs;

    
switches for the jruby command, like '--1.9'
Command line -Djruby.switches=...

Parameter:
expression="${jruby.switches}"
 
     protected String jrubySwitches;

    
if the pom.xml has no runtime dependency to a jruby-complete.jar then this version is used to resolve the jruby-complete dependency from the local/remote maven repository. it overwrites the jruby version from the dependencies if any. i.e. you can easily switch jruby version from the commandline !
default: 1.6.7.2
Command line -Djruby.version=...

Parameter:
expression="${jruby.version}"
 
     protected String jrubyVersion;

    
fork the JRuby execution.
Command line -Djruby.fork=...

Parameter:
expression="${jruby.fork}" default-value="true"
 
     protected boolean jrubyFork;

    
verbose jruby related output
Command line -Djruby.verbose=...

Parameter:
expression="${jruby.verbose}" default-value="false"
 
     protected boolean jrubyVerbose;

    
directory with ruby sources - added to java classpath and ruby loadpath
Command line -Djruby.sourceDirectory=...

Parameter:
expression="${jruby.sourceDirectory}" default-value="src/main/ruby"
    protected File rubySourceDirectory;

    
directory with ruby sources - added to ruby loadpath only
Command line -Djruby.lib=...

Parameter:
expression="${jruby.lib}" default-value="lib"
    protected File libDirectory;

    
the launch directory for the JRuby execution.
Command line -Djruby.launchDirectory=...

Parameter:
default-value="${project.basedir}" expression="${jruby.launchDirectory}"
    private File launchDirectory;

    
reference to maven project for internal use.

Parameter:
expression="${project}"
Required:
ReadOnly:
    protected MavenProject project;

    
local repository for internal use.

Parameter:
default-value="${localRepository}"
Required:
Readonly:
    protected ArtifactRepository localRepository;

    
classrealm for internal use.

Parameter:
expression="${dummyExpression}"
Readonly:
    protected ClassRealm classRealm;

    

Component:
    protected Logger logger;
    protected ScriptFactory factory;
    protected ScriptFactory newScriptFactory() throws MojoExecutionException {
        try {
            return newScriptFactory(resolveJRubyArtifact());
        }
        catch (final DependencyResolutionRequiredException e) {
            throw new MojoExecutionException("could not resolve jruby"e);
        }
    }
    protected ScriptFactory newScriptFactory(Artifact artifactthrows MojoExecutionException {
        try {
            final ScriptFactory factory = new ScriptFactory(this.,
                    this.
                    artifact.getArtifactId().equals()? nullartifact.getFile(),
                    artifact.getArtifactId().equals()? retrieveStdlibArtifact().getFile(): artifact.getFile(),
                    this..getTestClasspathElements(), 
                    this.);
            return factory;
        } catch (final DependencyResolutionRequiredException e) {
            throw new MojoExecutionException("could not resolve jruby"e);
        } catch (final ScriptException e) {
            throw new MojoExecutionException(
                    "could not initialize script factory"e);
        } catch (final IOException e) {
            throw new MojoExecutionException(
                    "could not initialize script factory"e);
        }
    }
        for (final Dependency artifact : this..getDependencies()) {
            if (artifact.getArtifactId().equals()) {
                return resolveJRubyArtifact(this.
                        .createArtifact(artifact.getGroupId(), artifact
                                .getArtifactId(), artifact.getVersion(),
                                artifact.getType()));
            }
        }
        return null;
    }
    public void execute() throws MojoExecutionExceptionMojoFailureException {
        System.setProperty("jbundle.skip""true");
        this. = new MojoLogger(this.getLog());
        this. = newScriptFactory();
        this..addEnv("JBUNDLE_SKIP""true");
        this..addJvmArgs(this.);
        this..addSwitches(this.);
        if( != null && .exists()){
            if(){
                getLog().info("add to java classpath and ruby loadpath: " + .getAbsolutePath());
            }
            // add it to the load path for all scripts using that factory
            this..addSwitch("-I".getAbsolutePath());
            // add it to the classpath so java classes can find the ruby files
            Resource resource = new Resource();
            resource.setDirectory(.getAbsolutePath());
            .getBuild().getResources().add(resource);
        }
        if( != null && .exists()){
            if(){
                getLog().info("add to ruby loadpath: " + .getAbsolutePath());
            }
            // add it to the load path for all scripts using that factory
            this..addSwitch("-I".getAbsolutePath());
        }
        try {
            executeJRuby();
        } catch (final IOException e) {
            throw new MojoExecutionException("error in executing jruby"e);
        } catch (final ScriptException e) {
            throw new MojoExecutionException("error in executing jruby"e);
        }
    }
    abstract protected void executeJRuby() throws MojoExecutionException,
    protected File launchDirectory() {
        if (this. == null) {
            this. = this..getBasedir();
            if (this. == null || !this..exists()) {
                this. = new File(System.getProperty("user.dir"));
            }
        }
        return this.;
    }
    protected Artifact resolveJRubyCompleteArtifact(final String version)
            throws DependencyResolutionRequiredException {
        getLog().debug("resolve jruby for version " + version);
        final Artifact artifact = this..createArtifact(
                "org.jruby"version"jar");
        return resolveJRubyArtifact(artifact);
    }
    private Artifact resolveJRubyArtifact(final Artifact artifact)
            throws DependencyResolutionRequiredException {
        final ArtifactResolutionRequest request = new ArtifactResolutionRequest();
        request.setArtifact(artifact);
        request.setLocalRepository(this.);
        this..resolve(request);
        if (this.) {
            getLog().info("jruby version   : " + artifact.getVersion());
        }
        // set it so other plugins can retrieve the version in use
        this. = artifact.getVersion();
        return artifact;
    }
            MojoExecutionException {
        if (this. != null) {
            // preference to command line or property version
            return resolveJRubyCompleteArtifact(this.);
        } 
        else {
            // then take jruby from the dependencies either jruby-complete or jruby-core
            for (final Dependency artifact : this..getDependencies()) {
                if ((artifact.getArtifactId().equals()
                      ||  artifact.getArtifactId().equals())
                      // TODO this condition is not needed ?
                            && !artifact.getScope().equals(.)
                        && !artifact.getScope().equals(.)) {
                    return resolveJRubyArtifact(this.
                            .createArtifact(artifact.getGroupId(), artifact
                                    .getArtifactId(), artifact.getVersion(),
                                    artifact.getType()));
                }
            }
        }
        // finally fall back on the default version of jruby
    }
New to GrepCode? Check out our FAQ X