Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.saumya.mojo.gem;
  
  import java.io.File;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import  org.apache.maven.artifact.resolver.ArtifactResolutionResult;
 
 
 public abstract class AbstractGemMojo extends AbstractJRubyMojo {

    

Parameter:
expression="${settings.offline}"
 
     private boolean                    offline;

    
allow to overwrite the version by explicitly declaring a dependency in the pom. will not check any dependencies on gemspecs level.

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

    

Parameter:
default-value="${plugin.artifacts}"
 
     protected java.util.List<ArtifactpluginArtifacts;
 
     public class UpdateCheckManager {
 
         public static final String  LAST_UPDATE_TAG = ".lastUpdated";
 
         private static final String TOUCHFILE_NAME  = "resolver-status.properties";
 
         public void touch(final Artifact artifact,
                 final ArtifactRepository repository) {
             final File file = artifact.getFile();
 
             final File touchfile = getTouchfile(artifact);
 
             if (file.exists()) {
                 touchfile.delete();
             }
             else {
                 writeLastUpdated(touchfilegetRepositoryKey(repository));
             }
         }
 
         public void touch(final RepositoryMetadata metadata,
                 final ArtifactRepository repositoryfinal File file) {
             final File touchfile = getTouchfile(metadatafile);
 
             final String key = getMetadataKey(repositoryfile);
 
             writeLastUpdated(touchfilekey);
         }
 
         String getMetadataKey(final ArtifactRepository repository,
                 final File file) {
             return repository.getId() + '.' + file.getName() + ;
         }
 
         String getRepositoryKey(final ArtifactRepository repository) {
             final StringBuilder buffer = new StringBuilder(256);
 
            // consider the URL (instead of the id) as this most closely relates
            // to the contents in the repo
            buffer.append(repository.getUrl());
            return buffer.toString();
        }
        private void writeLastUpdated(final File touchfilefinal String key) {
            synchronized (touchfile.getAbsolutePath().intern()) {
                if (!touchfile.getParentFile().exists()
                        && !touchfile.getParentFile().mkdirs()) {
                    getLog().debug("Failed to create directory: "
                            + touchfile.getParent()
                            + " for tracking artifact metadata resolution.");
                    return;
                }
                FileChannel channel = null;
                FileLock lock = null;
                try {
                    final Properties props = new Properties();
                    channel = new RandomAccessFile(touchfile"rw").getChannel();
                    lock = channel.lock(0, channel.size(), false);
                    if (touchfile.canRead()) {
                        getLog().debug("Reading resolution-state from: "
                                + touchfile);
                        final ByteBuffer buffer = ByteBuffer.allocate((intchannel.size());
                        channel.read(buffer);
                        buffer.flip();
                        final ByteArrayInputStream stream = new ByteArrayInputStream(buffer.array());
                        props.load(stream);
                    }
                    props.setProperty(key,
                                      Long.toString(System.currentTimeMillis()));
                    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    getLog().debug("Writing resolution-state to: " + touchfile);
                    props.store(stream"Last modified on: " + new Date());
                    final byte[] data = stream.toByteArray();
                    final ByteBuffer buffer = ByteBuffer.allocate(data.length);
                    buffer.put(data);
                    buffer.flip();
                    channel.position(0);
                    channel.write(buffer);
                }
                catch (final IOException e) {
                    getLog().debug("Failed to record lastUpdated information for resolution.\nFile: "
                                           + touchfile.toString()
                                           + "; key: "
                                           + key,
                                   e);
                }
                finally {
                    if (lock != null) {
                        try {
                            lock.release();
                        }
                        catch (final IOException e) {
                            getLog().debug("Error releasing exclusive lock for resolution tracking file: "
                                                   + touchfile,
                                           e);
                        }
                    }
                    if (channel != null) {
                        try {
                            channel.close();
                        }
                        catch (final IOException e) {
                            getLog().debug("Error closing FileChannel for resolution tracking file: "
                                                   + touchfile,
                                           e);
                        }
                    }
                }
            }
        }
        File getTouchfile(final Artifact artifact) {
            final StringBuilder sb = new StringBuilder(128);
            sb.append(artifact.getArtifactId());
            sb.append('-').append(artifact.getBaseVersion());
            if (artifact.getClassifier() != null) {
                sb.append('-').append(artifact.getClassifier());
            }
            sb.append('.').append(artifact.getType()).append();
            return new File(artifact.getFile().getParentFile(), sb.toString());
        }
        File getTouchfile(final RepositoryMetadata metadatafinal File file) {
            return new File(file.getParent(), );
        }
    }
    private static final int               ONE_DAY_IN_MILLIS  = 86400000;
    private final List<ArtifactRepositorygemRepositories    = new ArrayList<ArtifactRepository>();
    private final UpdateCheckManager       updateCheckManager = new UpdateCheckManager();
    @Override
    public void execute() throws MojoExecutionException {
        if (this..getBasedir() == null) {
            this. = new File(this..getAbsolutePath()
                    .replace("${project.basedir}"""));
            this. = new File(this..getAbsolutePath()
                    .replace("${project.basedir}"""));
        }
        execute(this.);
        executeWithGems();
    }
    public void execute(final Collection<Artifactartifacts)
            throws MojoExecutionException {
        for (final ArtifactRepository repository : this.) {
            // instanceof does not work probably a classloader issue !!!
            if (repository.getLayout()
                    .getClass()
                    .getName()
                    .equals(GemRepositoryLayout.class.getName())) {
                this..add(repository);
            }
        }
        if (this..size() == 0) {
            final ArtifactRepositoryPolicy releases = new ArtifactRepositoryPolicy();
            releases.setChecksumPolicy("ignore");
            releases.setUpdatePolicy("never");
            final ArtifactRepositoryPolicy snapshots = new ArtifactRepositoryPolicy();
            snapshots.setEnabled(false);
            final DefaultArtifactRepository rubygemsRepo = new DefaultArtifactRepository("rubygems",
                    "http://rubygems.org/gems",
                    new GemRepositoryLayout(),
                    snapshots,
                    releases);
            getLog().info("gem plugin configured but no gem repository found - fall back to "
                    + rubygemsRepo.getUrl());
            this..add(rubygemsRepo);
            this..add(rubygemsRepo);
        }
        final File gemsDir = new File(this."gems");
        final StringBuilder gems = new StringBuilder();
        final Map<StringArtifactcollectedArtifacts = new LinkedHashMap<StringArtifact>();
        for (final Artifact artifact : artifacts) {
            if (artifact.getType().contains("gem")
                    || artifact == this..getArtifact()) {
                collectArtifacts(artifactcollectedArtifactstrue);
            }
        }
        collectedArtifacts.remove(key(this..getArtifact()));
        String extraFlag = "";
        if (this.) {
            // allow to overwrite resolved version with version of project
            // dependencies
            for (final Object o : this..getDependencies()) {
                final Dependency artifact = (Dependencyo;
                final Artifact a = collectedArtifacts.get(artifact.getGroupId()
                        + ":" + artifact.getArtifactId());
                if (!a.getVersion().equals(artifact.getVersion())) {
                    extraFlag = "--force";
                    a.setVersion(artifact.getVersion());
                    a.setResolved(false);
                    a.setFile(null);
                    try {
                        this..resolve(a,
                                              this.,
                                              this.);
                    }
                    catch (final ArtifactResolutionException e) {
                        throw new MojoExecutionException("error resolving " + a,
                                e);
                    }
                    catch (final ArtifactNotFoundException e) {
                        throw new MojoExecutionException("error resolving " + a,
                                e);
                    }
                }
            }
        }
        // collect all uninstalled gems in a reverse dependency order
        for (final Artifact collectedArtifact : collectedArtifacts.values()) {
            if (collectedArtifact.getType().contains("gem")) {
                final String prefix = collectedArtifact.getGroupId()
                        .equals("rubygems")
                        ? ""
                        : collectedArtifact.getGroupId() + ".";
                final File gemDir = new File(gemsDirprefix
                        + (collectedArtifact.getFile()
                                .getName()
                                .replaceAll(".gem$""").replace("-SNAPSHOT",
                                                                 "")));
                // TODO force flag to install gems via command line
                // argument
                if (!(this. && gemDir.exists())) {
                    gems.append(" ").append(collectedArtifact.getFile()
                            .getAbsolutePath());
                }
                else {
                    getLog().debug("already installed: " + collectedArtifact);
                }
            }
        }
        if (gems.length() > 0) {
            execute("-S gem install --no-ri --no-rdoc " + extraFlag + " -l "
                    + gemsfalse);
        }
        else {
            getLog().debug("no gems found to install");
        }
    }
    abstract protected void executeWithGems() throws MojoExecutionException;
    private boolean createMissingPom(final Artifact artifact)
            throws MojoExecutionException {
        final File pom = new File(artifact.getFile()
                .getPath()
                .replaceFirst("(-java)?.gem$"".pom"));
        if (artifact.getGroupId().equals("rubygems")) {
            boolean isPom = false;
            if (pom.lastModified() == artifact.getFile().lastModified()) {
                try {
                    isPom = FileUtils.fileRead(pom).startsWith("<?xml");
                }
                catch (final IOException e) {
                    // well just create a new pom now
                }
            }
            if (!isPom) {
                if (this.) {
                    getLog().debug("<gems> offline mode - skip creating pom for "
                            + artifact);
                }
                else {
                    getLog().debug("<gems> creating pom for " + artifact);
                    // use temporary file until complete file is written out to
                    // disk
                    final File tmp = new File(pom.getParentFile(),
                            pom.getName() + ".tmp");
                    // TODO use embedded ruby best via a component to reuse the
                    // state of the spec fetcher
                    execute(new String[] {
                                    "-e",
                                    "ARGV[0] = '"
                                            + artifact.getFile()
                                                    .getAbsolutePath()
                                            + "'\nrequire('"
                                            + fileFromClassloader("spec2pom.rb")
                                            + "')" },
                            pom,
                            false);
                    tmp.renameTo(artifact.getFile());
                    pom.setLastModified(artifact.getFile().lastModified());
                    return true;
                }
            }
        }
        return false;
    }
    private String key(final Artifact artifact) {
        return artifact.getGroupId() + ":" + artifact.getArtifactId();
    }
    @SuppressWarnings("unchecked")
    private void collectArtifacts(final Artifact artifact,
            final Map<StringArtifactvisitedArtifacts,
            final boolean includeTestthrows MojoExecutionException {
        getLog().debug("<gems> collect artifacts for " + artifact);
        resolve(artifact);
        try {
            final MavenProject project = artifact != this..getArtifact()
                    ? this..buildFromRepository(artifact,
                                                       this.,
                                                       this.)
                    : this.;
            project.setDependencyArtifacts(project.createArtifacts(this.,
                                                                   artifact.getScope(),
                                                                   null));
            createMetadatasForDependencies(project);
            project.setRemoteArtifactRepositories(this.);
            ArtifactResolutionResult result = null;
            boolean retry = true;
            while (retry) {
                try {
                    retry = false;
                    result = this..resolveTransitively(project.getDependencyArtifacts(),
                                                               project.getArtifact(),
                                                               this.,
                                                               this.,
                                                               this.,
                                                               null);
                }
                catch (final AbstractArtifactResolutionException e) {
                    retry = createMetadata(e.getArtifact());
                    if (!retry) {
                        getLog().error("error resolving "
                                               + project.getArtifact(),
                                       e);
                    }
                }
            }
            project.setArtifacts(result.getArtifacts());
            for (final Artifact dependencyArtifact : (Set<Artifact>) project.getDependencyArtifacts()) {
                if ("gem".equals(dependencyArtifact.getType())) {
                    if (!visitedArtifacts.containsKey(key(dependencyArtifact))) {
                        collectArtifacts(dependencyArtifact,
                                         visitedArtifacts,
                                         false);
                    }
                }
            }
            visitedArtifacts.put(key(artifact), artifact);
        }
        catch (final InvalidDependencyVersionException e) {
            throw new MojoExecutionException("resolve error"e);
        }
        catch (final ProjectBuildingException e) {
            throw new MojoExecutionException("Unable to build project due to an invalid dependency version: "
                    + e.getMessage(),
                    e);
        }
        catch (final InvalidVersionSpecificationException e) {
            throw new MojoExecutionException("resolve error"e);
        }
    }
    @SuppressWarnings("unchecked")
    private void createMetadatasForDependencies(final MavenProject project)
        getLog().debug("<gems> process metadata for " + project.getArtifact()
                + " " + project.getDependencies());
        for (final Dependency dep : (List<Dependency>) project.getDependencies()) {
            if ("gem".equals(dep.getType())
                    && (. + .).contains(dep.getScope())) {
                final Artifact dependencyArtifact = this..createDependencyArtifact(dep.getGroupId(),
                                                                                                  dep.getArtifactId(),
                                                                                                  VersionRange.createFromVersionSpec(dep.getVersion()),
                                                                                                  dep.getType(),
                                                                                                  dep.getClassifier(),
                                                                                                  dep.getScope());
                createMetadata(dependencyArtifact);
            }
        }
    }
    private boolean createMetadata(final Artifact dependencyArtifact)
            throws MojoExecutionException {
        final ArtifactRepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata(dependencyArtifact);
        // TODO do not assume to have only ONE gem repository
        final ArtifactRepository repository = this..get(0);
        final File metadataFile = new File(this..getBasedir(),
                this..pathOfLocalRepositoryMetadata(repositoryMetadata,
                                                                   repository));
        // update them only once a day
        if (System.currentTimeMillis() - metadataFile.lastModified() > ) {
            if (this.) {
                getLog().debug("<gems> offline mode - skip updating metadata for "
                        + dependencyArtifact);
                return false;
            }
            else {
                getLog().debug("<gems> "
                        + (metadataFile.exists() ? "updating" : "creating")
                        + " metadata for " + dependencyArtifact);
                metadataFile.getParentFile().mkdirs();
                // use temporary file until new file is completely written
                final File tmp = new File(metadataFile.getParentFile(),
                        metadataFile.getName() + ".tmp");
                final String script = "ARGV[0] = '"
                        + dependencyArtifact.getArtifactId() + "'\nrequire('"
                        + fileFromClassloader("metadata.rb") + "')";
                try {
                    execute(new String[] { "-e"script }, tmpfalse);
                    tmp.renameTo(metadataFile);
                    // TODO is that needed ?
                    metadataFile.setLastModified(System.currentTimeMillis());
                }
                catch (final MojoExecutionException e) {
                    // retry due to often timeout errors
                    // TODO make the retry on timeout errors only !!!
                    try {
                        execute(new String[] { "-e"script }, tmpfalse);
                        tmp.renameTo(metadataFile);
                        // TODO is that needed ?
                        metadataFile.setLastModified(System.currentTimeMillis());
                    }
                    catch (final MojoExecutionException ee) {
                        // TODO maybe it is possible to obey fail-fast and
                        // fail-at-end from the command line switches
                        if (metadataFile.exists()) {
                            getLog().warn("failed to update metadata for "
                                                  + this.
                                                  + ", use old one",
                                          ee);
                        }
                        else {
                            throw ee;
                        }
                    }
                }
                tmp.delete();
                this..touch(repositoryMetadata,
                                              repository,
                                              metadataFile);
                return true;
            }
        }
        else {
            return false;
        }
    }
    private void resolve(final Artifact artifactthrows MojoExecutionException {
        if (artifact != null && this..getArtifact() != artifact) {
            if (artifact.getFile() == null || !artifact.getFile().exists()) {
                getLog().debug("<gems> resolve " + artifact);
                // final ArtifactResolutionRequest request = new
                // ArtifactResolutionReqquest().setArtifact(artifact)
                // .setLocalRepository(this.localRepository)
                // .setRemoteRepositories(this.project.getRemoteArtifactRepositories());
                try {
                    this..resolve(artifact,
                                          this.,
                                          this.);
                }
                catch (final ArtifactResolutionException e) {
                    throw new MojoExecutionException("resolve error"e);
                }
                catch (final ArtifactNotFoundException e) {
                    throw new MojoExecutionException("resolve error"e);
                }
            }
            createMissingPom(artifact);
        }
    }
New to GrepCode? Check out our FAQ X