Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.saumya.mojo.jruby;
  
  import java.io.File;
  import java.io.FileWriter;
  import java.util.HashSet;
  import java.util.List;
  import java.util.Map;
 import java.util.Set;
 
 
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 String JRUBY_STDLIB = "jruby-stdlib";
 
     protected static final String DEFAULT_JRUBY_VERSION = "1.7.12";


    
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;

    
environment values passed on to the jruby process. needs jrubyFork true.

Parameter:
 
     protected Map<StringStringenv;

    
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.7.12
Command line -Djruby.version=...

Parameter:
expression="${jruby.version}"
 
     private 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;

    
add project test class path to JVM classpath.

Parameter:
default-value=true expression="${gem.addProjectClasspath}"
    protected boolean addProjectClasspath;
    
    
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;
    private JRubyVersion jRubyVersion;

    

Component:
    private BuildContext buildContext;
    
    
    

Parameter:
expression="${m2e.jruby.watch}"
    protected List<StringeclipseWatches = new ArrayList<String>();
    
    

Parameter:
expression="${m2e.jruby.refresh}"
    protected List<StringeclipseRefresh = new ArrayList<String>(); 
    
    protected JRubyVersion getJrubyVersion()
    {
        if ( == null )
        {
            this. = new JRubyVersion == null ?  :  );
        }
        return ;
    }
    
    	ScriptFactory factory = createScriptFactory();
    	if != null ){
    		forMap.Entry<StringStringentry.entrySet() ){
    			factory.addEnventry.getKey(), entry.getValue() );
    		}
    	}
    	return factory;
    }
        try
        {
            .getWorld().disposeRealm("jruby-all");
        }
        catchNoSuchRealmException ignore )
        {
            // ignore
        }
        try {
			ClassRealm realm = .getWorld().newRealm("jruby-all");
			for (String path : getProjectClasspath()) {
				realm.addConstituent(new File(path).toURI().toURL());
			}
			if (this. != null) {
	            // preference to command line or property version
	            return newScriptFactoryresolveJRubyCompleteArtifact(this.) );
	        } 
	        // check if there is jruby present
			Class<?> clazz = realm.loadClass("org.jruby.runtime.Constants");
			if (  ){
				String version = clazz.getField"VERSION" ).get(clazz).toString();
				getLog().info("found jruby on classpath");
				getLog().info("jruby version   : " + version);
			}
			this. = realm;
			return newScriptFactorynull );
catch (final Exception e) {
		    //TODO debug
		    //e.printStackTrace();
			try {
catch (final DependencyResolutionRequiredException ee) {
				throw new MojoExecutionException("could not resolve jruby"e);
			}
		}
	}
    protected ScriptFactory newScriptFactory(Artifact artifactthrows MojoExecutionException {
        try {
            final ScriptFactory factory = 
            		artifact == null ? 
                            new ScriptFactory(this.,
                                    this.
                                    null,
                                    getProjectClasspath(), 
                                    this.):
            			(.equals(artifact.getArtifactId()) ?
                    new ScriptFactory(this.,
                            this.
                            artifact.getFile(),
                            resolveJRubyStdlibArtifact(artifact).getFile(),
                            getProjectClasspath(), 
                            this.) :
                    new ScriptFactory(this.,
                            this.
                            artifact.getFile(),
                            getProjectClasspath(), 
                            this.) );
            if( != null && .exists()){
                if(){
                    getLog().info("add to ruby loadpath: " + .getAbsolutePath());
                }
                // add it to the load path for all scripts using that factory
                factory.addSwitch("-I".getAbsolutePath());
            }
            if( != null && .exists()){
                if(){
                    getLog().info("add to ruby loadpath: " + .getAbsolutePath());
                }
                // add it to the load path for all scripts using that factory
                factory.addSwitch("-I".getAbsolutePath());
            }
            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 {
    	boolean shouldCheckChanges = .isIncremental() && !.isEmpty();    	
    	if (shouldCheckChanges && !.hasDelta()) {
    		return;
    	}
    	
    	 
        System.setProperty("jbundle.skip""true");
        this. = new MojoLogger(this.getLog());
        this. = newScriptFactory();
        // skip installing jars via jbundler
        this..addEnv("JBUNDLE_SKIP""true");
        // skip installing jars via jar-dependencies
        this..addEnv("JARS_SKIP""true");
        this..addJvmArgs(this.);
        this..addSwitches(this.);
        if( != null && .exists()){
            if(){
                getLog().info("add to java classpath: " + .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);
        }
        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);
        } finally {
        	// ensure that eclipse update any changes, include errors reports
        	if (!.isEmpty()) {
        		for (String fileName : ) {
        			File file = new File(fileName);
        			.refresh(file);;
        		}
        	}
        }
    }
    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
    }
            MojoExecutionException {
        final ArtifactResolutionRequest request = new ArtifactResolutionRequest();
        for (final Dependency artifact : this..getDependencies()) {
            if (artifact.getArtifactId().equals()
                    // TODO this condition is not needed ?
                    && !artifact.getScope().equals(.)
                    && !artifact.getScope().equals(.)) {
                request.setArtifact(this.
                        .createArtifact(artifact.getGroupId(), artifact
                                .getArtifactId(), artifact.getVersion(),
                                artifact.getType()));
                break;
            }
        }
        if (request.getArtifact() == null){
            request.setResolveTransitively(true);
            request.setArtifact(jruby);
        }
        request.setLocalRepository(this.);
        Set<Artifactset = this..resolve(request).getArtifacts();
        for (Artifact aset){
            if (.equals(a.getArtifactId())) {
                return a;
            }
        }
        throw new MojoExecutionException("failed to resolve jruby stdlib artifact");
    }
    
    	if () {
    	} else {
    		return new ArrayList<String>();
    	}
    }
    
New to GrepCode? Check out our FAQ X