Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.saumya.mojo.gem;
  
  import java.io.File;
  import java.util.List;
  import java.util.Map;
  
 
 
 public abstract class AbstractGemMojo extends AbstractJRubyMojo {

    

Component:
role-hint="zip"
 
     protected UnArchiver unzip;
    
    

Parameter:
expression="${plugin}" @readonly
 
     protected PluginDescriptor  plugin;

    
flag whether to include open-ssl gem or not
Command line -Dgem.includeOpenSSL=...

Parameter:
expression="${gem.includeOpenSSL}" default-value="true"
 
     protected boolean       includeOpenSSL;

    
flag whether to include all gems to test-resources, i.e. to test-classpath or not
Command line -Dgem.includeRubygemsInTestResources=...

Parameter:
expression="${gem.includeRubygemsInTestResources}" default-value="true"
 
     protected boolean       includeRubygemsInTestResources;

    
flag whether to include all gems to resources, i.e. to classpath or not
Command line -Dgem.includeRubygemsInResources=...

Parameter:
expression="${gem.includeRubygemsInResources}" default-value="false"
 
     protected boolean       includeRubygemsInResources;
    
    
EXPERIMENTAL this gives the scope of the gems which shall be included to resources. flag whether to include all gems to resources, i.e. to classpath or not. the difference to the includeRubygemsInResources is that it does not depend on rubygems during runtime since the required_path of the gems gets added to resources. note that it expect the required_path of the gem to be lib which is the default BUT that is not true for all gems. in this sense this feature is incomplete and might not work for you !!! IMPORTANT: it only adds the gems with provided scope since they are packed with the jar and then the pom.xml will not have them (since they are marked 'provided') as transitive dependencies. this feature can be helpful in situations where the classloader does not work for rubygems due to rubygems uses file system globs to find the gems and this only works if the classloader reveals the jar url of its jars (i.e. URLClassLoader). for example OSGi classloader can not work with rubygems !!
Command line -Dgem.includeGemsInResources=...

Parameter:
expression="${gem.includeGemsInResources}"
 
     protected String       includeGemsInResources;

    
/** flag whether to include file under the lib directory
Command line -Dgem.includeLibDirectoryInResources=...

Parameter:
expression="${gem.iincludeLibDirectoryInResources}" default-value="false"
    protected boolean       includeLibDirectoryInResources;

    
    
flag whether to install rdocs of the used gems or not
Command line -Dgem.installRDoc=...

Parameter:
expression="${gem.installRDoc}" default-value="false"
    protected boolean         installRDoc;

    
flag whether to install ri of the used gems or not
Command line -Dgem.installRDoc=...

Parameter:
expression="${gem.installRI}" default-value="false"
    protected boolean         installRI;

    
use system gems instead of setting up GemPath/GemHome inside the build directory and ignores any set gemHome and gemPath. you need to have both GEM_HOME and GEM_PATH environment variable set to make it work.
Command line -Dgem.useSystem=...

Parameter:
expression="${gem.useSystem}" default-value="false"
    protected boolean           gemUseSystem;

    
map different install locations for rubygems (GEM_HOME) to a directory. for example compile dependencies will be installed in ${project.build.directory}/rubygems and provided dependencies in ${project.build.directory}/rubygems-provided, and ${project.build.directory}/rubygems-test for the test scope. this mapping here allows to map those different directories onto a single one, i.e.: test => ${gem.home}, provided => ${gem.home}

Parameter:
    protected Map<StringStringgemHomes;

    
directory of gem home to use when forking JRuby. default will be ignored when gemUseSystem is true.
Command line -Dgem.home=...

Parameter:
expression="${gem.home}" default-value="${project.build.directory}/rubygems"
    protected File               gemHome;

    
directory of JRuby path to use when forking JRuby. default will be ignored when gemUseSystem is true.
Command line -Dgem.path=...

Parameter:
expression="${gem.path}" default-value="${project.build.directory}/rubygems"
    protected File          gemPath;

    
directory of JRuby bin path to use when forking JRuby.
Command line -Dgem.binDirectory=...

Parameter:
expression="${gem.binDirectory}"
    protected File          binDirectory;

    
flag to indicate to setup jruby's native support for C-extensions
Command line -Dgem.supportNative=...

Parameter:
expression="${gem.supportNative}" default-value="false"
    protected boolean        supportNative;
    
    

Component:
    protected GemManager    manager;
    protected GemsConfig    gemsConfig;
    protected GemsInstaller gemsInstaller;
    
    @Override
    public void execute() throws MojoExecutionExceptionMojoFailureException{
        if (this..getBasedir() == null) {
            this. = new File(this..getAbsolutePath()
                    .replace("/${project.basedir}/""/"));
            this. = new File(this..getAbsolutePath()
                    .replace("/${project.basedir}/""/"));
        }
        this. = new GemsConfig();
        try {
            this..setGemHome(this..getCanonicalFile());
            this..addGemPath(this..getCanonicalFile());
        }
        catch (IOException e) {
            // fallback to the given files
            this..setGemHome(this.);
            this..addGemPath(this.);
        }
        if (this. && 
                (System.getenv("GEM_HOME") == null || System.getenv"GEM_PATH") == null) ){
            getLog().warn("with gemUseSystem set to true and no GEM_HOME and GEM_PATH is set, " +
            		" then some maven goals might not work as expected");
        }
        this..setSystemInstall(this.);
        
        this..setAddRdoc(this.);
        this..setAddRI(this.);
        this..setBinDirectory(this.);
        // this.gemsConfig.setUserInstall(userInstall);
        // this.gemsConfig.setSystemInstall(systemInstall);
        this..setSkipJRubyOpenSSL( ! (this. && getJrubyVersion().needsOpenSSL() ) );
        super.execute();
    }
    @Override
    protected ScriptFactory newScriptFactory(Artifact artifactthrows MojoExecutionException {
        try {
            final GemScriptFactory factory = 
                    artifact == null ? 
                            new GemScriptFactory(this.,
                                    this.
                                    null,
                                    getProjectClasspath(), 
                                    this.,
                                    this.):
                        (.equals(artifact.getArtifactId()) ?
                    new GemScriptFactory(this.,
                                  this.
                                  artifact.getFile(),
                                  resolveJRubyStdlibArtifact(artifact).getFile(),
                                  getProjectClasspath(), 
                                  this.
                                  this.) :
                    new GemScriptFactory(this.,
                                  this.
                                  artifact.getFile(),
                                  getProjectClasspath(), 
                                  this.
                                  this.));
                  
            if(){
                factory.addJvmArgs("-Djruby.home=" + setupNativeSupport().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());
            }
            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);
        }
    }
    private File setupNativeSupport() throws MojoExecutionException {
        File target = new File(this..getBuild().getDirectory());
        File jrubyDir = new File(target"jruby-" + getJrubyVersion());
        if (!jrubyDir.exists()){
            Artifact dist = .createArtifact("org.jruby",
                                                   "jruby-dist",
                                                   getJrubyVersion().toString(),
                                                   "bin",
                                                   "zip");
            try {
                .resolve(dist,
                                ,
                                .getRemoteArtifactRepositories());
            }
            catch (final GemException e) {
                throw new MojoExecutionException("could not setup jruby distribution for native support",
                        e);
            }
            if () {
                getLog().info("unzip " + dist.getFile());
            }
            target.mkdirs();
            .setSourceFile(dist.getFile());
            .setDestDirectory(target);
            File f = null;
            try {
                .extract();
                f = new File(target"jruby-" + getJrubyVersion() + "/bin/jruby");
                // use reflection so it compiles with java1.5 as well but does not set executable
                Method m = f.getClass().getMethod("setExecutable"boolean.class);
                m.invoke(fnew Boolean(true));
            }
            catch (ArchiverException e) {
                throw new MojoExecutionException("could unzip jruby distribution for native support",
                        e);
            }
            catch (Exception e) {
                getLog().warn("can not set executable flag: " + f.getAbsolutePath() + " (" + e.getMessage() + ")");
            }
        }
        return jrubyDir;
    }
    protected File gemHomeString baseString key )
    {
       if ( != null && .containsKey(key)) {
           return new File(.get(key));
       }
       else {
           return new File(base + "-" + key);
       }
    }
    @Override
    protected void executeJRuby() throws MojoExecutionException,
        
        this. = new GemsInstaller(this.,
                this.,
                this.);
        // remember gem_home
        File home = this..getGemHome();
        // use a common bindir, i.e. the one from the configured gemHome
        // remove default by setting it explicitly
        // use gemHome as base for other gems installation directories
        String base = this..getGemHome() != null ? 
                this..getGemHome().getAbsolutePath() : 
                    (.getBuild().getDirectory() + "/rubygems");
        try {
            // install the gem dependencies from the pom
            if (  )
            {
                getLog().info("installing gems for compile scope . . .");
            }
            this..installPom(this.
                                          this."compile");
            if (  )
            {
                getLog().info("installing gems for runtime scope . . .");
            }
            this..installPom(this.
                                          this."runtime");
            String[] SCOPES = new String[] { "provided""test" };
            forString scopeSCOPES ){
                File gemHome = gemHomebasescope );
                this..setGemHomegemHome );
                this..addGemPathgemHome );
                
                if (  )
                {
                    getLog().info("installing gems for " + scope + " scope . . .");
                }
                // install the gem dependencies from the pom
                this..installPom(this.
                                              this.
                                              scope);
               
            }
 
            File pluginGemHome = gemHomebase.getArtifactId() );
            // use plugin home for plugin gems
            this..setGemHome(pluginGemHome);
            this..addGemPath(pluginGemHome);
            if (  )
            {
                getLog().info("installing gems for plugin " + .getGroupId() + ":" + .getArtifactId() 
                              + " . . .");
            }
            this..installGems(this.,
                                           this..getArtifacts(), 
                                           this.
                                           getRemoteRepos());
        }
        catch (final GemException e) {
            throw new MojoExecutionException("error in installing gems"e);
        }
        finally
        {
            // reset old gem home again
            this..setGemHome(home);
        }
        if (this.) {
            for (File path : this..getGemPath()) {
                if ( path.exists() ) {
                    if () {
                        getLog().info("add gems to test-classpath from: "
                                + path.getAbsolutePath());
                    }
                    // add it to the classpath so java classes can find the ruby
                    // files
                    Resource resource = new Resource();
                    resource.setDirectory(path.getAbsolutePath());
                    resource.addInclude("gems/**");
                    resource.addInclude("specifications/**");
                    addResource(.getBuild().getTestResources(), resource);
                }
            }
        }
        if (this.) {
            if () {
                getLog().info("add gems to classpath from: "
                        + home.getAbsolutePath());
            }
            // add it to the classpath so java classes can find the ruby files
            Resource resource = new Resource();
            resource.setDirectory(home.getAbsolutePath());
            resource.addInclude("gems/**");
            resource.addInclude("specifications/**");
            // no java sources since resins application server tries to compile those
            resource.addExclude("gems/**/*.java");
            addResource(.getBuild().getResources(), resource);
        }
        if (this.) {
            if () {
                getLog().info("add to classpath: "
                        + .getAbsolutePath());
            }
            // add it to the classpath so java classes can find the ruby files
            Resource resource = new Resource();
            resource.setDirectory(.getAbsolutePath());
            addResource(.getBuild().getResources(), resource);
        }
        if (this. != null ) {
            String dir = "compile".equals ) ? base : base + "-" + ;
            File gems = new File(dir"gems");
            if ( gems.exists() )
            {
                forFile g : gems.listFiles() ) {
                    File lib = new File(g"lib");
                    if () {
                        getLog().info("add to resource: "
                                + lib.getAbsolutePath());
                    }
                    Resource resource = new Resource();
                    resource.setDirectory(lib.getAbsolutePath());
                    .getBuild().getResources().add(resource);
                }
            }
        }   
         
        try {
            executeWithGems();
        }
        catch (final GemException e) {
            throw new MojoExecutionException("error in executing with gems"e);
        }
    }
    protected void addResource(List<ResourceresourcesResource resource) {
        String ref = resource.toString();
        forResource r : resources ) {
            if (r.toString().equals(ref)) {
                return;
            }
        }
        if () {
            .info("add resource: " + resource);
        }
        resources.add(resource);
    }
    abstract protected void executeWithGems() throws MojoExecutionException,
    protected List<ArtifactRepositorygetRemoteRepos() {
        List<ArtifactRepositoryremotes = new LinkedList<ArtifactRepository>();
        remotes.addAll(.getPluginArtifactRepositories() );
        remotes.addAll(.getRemoteArtifactRepositories() );
        return remotes;
    }
New to GrepCode? Check out our FAQ X