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 {
 
     private static String DEFAULT_JRUBY_VERSION = "1.6.1";
 
     public static final String GEM_RUBY_COMMAND = "META-INF/jruby.home/bin/gem";
 
     public static final String RAKE_RUBY_COMMAND = "META-INF/jruby.home/bin/rake";

    
common arguments
Command line -Dargs=...

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

    
arguments for the jruby command.
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.1
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;

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

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

    
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(resolveJRUBYCompleteArtifact());
        }
        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.resolveJRUBYCompleteArtifact().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);
        }
    }
    public void execute() throws MojoExecutionExceptionMojoFailureException {
        if(.exists()){
            Resource resource = new Resource();
            resource.setDirectory(.getAbsolutePath());
            .getBuild().getResources().add(resource);
        }
        this. = new MojoLogger(this.getLog());
        this. = newScriptFactory();
        this..addJvmArgs(this.);
        this..addSwitches(this.);
        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""jruby-complete"version"jar");
        return resolveJRUBYCompleteArtifact(artifact);
    }
    private Artifact resolveJRUBYCompleteArtifact(final Artifact artifact)
            throws DependencyResolutionRequiredException {
        final ArtifactResolutionRequest request = new ArtifactResolutionRequest();
        request.setArtifact(artifact);
        request.setLocalRepository(this.);
        request.setRemoteRepositories(this.
                .getRemoteArtifactRepositories());
        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;
    }
            throws DependencyResolutionRequiredException,
            MojoExecutionException {
        if (this. != null) {
            // preference to command line or property version
            return resolveJRUBYCompleteArtifact(this.);
        } else {
            // then take jruby from the dependencies
            for (final Object o : this..getDependencies()) {
                final Dependency artifact = (Dependencyo;
                if (artifact.getArtifactId().equals("jruby-complete")
                        && !artifact.getScope().equals(.)
                        && !artifact.getScope().equals(.)) {
                    return resolveJRUBYCompleteArtifact(this.
                            .createArtifact(artifact.getGroupId(), artifact
                                    .getArtifactId(), artifact.getVersion(),
                                    artifact.getType()));
                }
            }
        }
        // take the default version of jruby
    }
New to GrepCode? Check out our FAQ X