4

there are many questions related to

<dependency>
    <groupId>com.jcabi</groupId>
    <artifactId>jcabi-aether</artifactId>
    <version>0.9</version>
</dependency>

It works perfectly when I need to get an artifact from remote repo. Unfotunately, I can't find a way to force aether to get artifact from local repo.

It prints to console:

2014-07-21 18:11:40 ERROR MethodValidator.error: - JSR-303 validator failed to initialize: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath. (see http://www.jcabi.com/jcabi-aspects/jsr-303.html)
2014-07-21 18:11:40 INFO  NamedThreads.info: - jcabi-aspects 0.7.22/fd7496f started new daemon thread jcabi-loggable for watching of @Loggable annotated methods
2014-07-21 18:11:41 WARN  LogTransferListener.warn: - #transferFailed('GET FAILED https://my.remote.repo/nexus/cont..243..tory-uploader-1.0.0-${revision.suffix}.pom'): in 29µs
2014-07-21 18:11:41 WARN  LogTransferListener.warn: - #transferFailed('GET FAILED http://repo.maven.apache.org/maven2/ru..220..tory-uploader-1.0.0-${revision.suffix}.pom'): in 21µs
2014-07-21 18:11:41 ERROR Aether.error: - #resolve(my.group.id:my-artifact-i-want-to-get:groovy:installer:1.0.0-SNAPSHOT, 'runtime', org.sonatype.aether.util.filter.ScopeDependencyFilter@9076fc75): thrown org.sonatype.aether.resolution.DependencyResolutionException(failed to load 'my.group.id:my-artifact-i-want-to-get:groovy:installer:1.0.0-SNAPSHOT (runtime)' from ["shared-nexus (https://my.remote.repo/nexus/content/groups/all-repos/, releases+snapshots) with kyc.developer", "central (http://repo.maven.apache.org/maven2, releases) without authentication"] into /home/ssa/.m2/repository) out of com.jcabi.aether.Aether#fetch[198] in 166ms
2014-07-21 18:11:41 ERROR Aether.error: - #resolve(my.group.id:my-artifact-i-want-to-get:groovy:installer:1.0.0-SNAPSHOT, 'runtime'): thrown org.sonatype.aether.resolution.DependencyResolutionException(failed to load 'my.group.id:my-artifact-i-want-to-get:groovy:installer:1.0.0-SNAPSHOT (runtime)' from ["shared-nexus (https://my.remote.repo/nexus/content/groups/all-repos/, releases+snapshots) with kyc.developer", "central (http://repo.maven.apache.org/maven2, releases) without authentication"] into /home/ssa/.m2/repository) out of com.jcabi.aether.Aether#fetch[

artifact is in local repo, but aether fails to find it... what do I do wrong?

My code: List remoteRepos, File localRepoRoot are "injected" from maven plugin. This class is used from maven-plugin.

MavenHelperAether(List<RemoteRepository> remoteRepos, File localRepoRoot) {
        this.remoteRepos = remoteRepos
        this.localRepoRoot = localRepoRoot
    }

    /**
     * Resolves artifact using artifact coordinates
     * @param artifactCoords is an artifact you need
     * @param remoteLookup is ignored. It's a part of backward compatibility.
     *
     * @return {@link File} pointing to artifact
     * */
    public File resolveArtifact(String artifactCoords, boolean remoteLookup = false){
        LOG.debug("resolving artifact $artifactCoords ... using localRepo[$localRepoRoot.absolutePath] and remoteRepos [$remoteRepos]")

        def aether = new Aether(remoteRepos, localRepoRoot)
        def artifact = new DefaultArtifact(artifactCoords)
        Collection<Artifact> deps = []
        try{
            deps = resolveInRemoteRepositories(aether, artifact)
            LOG.debug("Resolved artifact path ${deps.iterator().next().file.absolutePath }")
            return deps.iterator().next().file
        }
        catch (DependencyResolutionException | IllegalArgumentException e){
            LOG.warn("Can't fetch $artifact from remote repos. Falling back to local repository...", e)
            def localArtifact = resolveInLocalRepo(artifact)
            if(localArtifact.exists()){
                return  localArtifact;
            }else{
                throw new InstallerException("Can't locate artifact [$artifact] in local repo using path [$localArtifact.absolutePath]")
            }
        }
    }

    private Collection<Artifact> resolveInRemoteRepositories(Aether aether, DefaultArtifact artifact){
        LOG.debug("Trying to resolve $artifact in remote repositories...")
        aether.resolve(artifact,JavaScopes.RUNTIME)
    }

    private  File resolveInLocalRepo(DefaultArtifact artifact){
        LOG.debug("Trying to resolve $artifact in local repository...")
        def localRepoManager = new SimpleLocalRepositoryManager(localRepoRoot)
        new File(localRepoManager.getPathForArtifact(artifact, true))
    }
yegor256
  • 102,010
  • 123
  • 446
  • 597
Capacytron
  • 3,425
  • 6
  • 47
  • 80

3 Answers3

3

You can just create local-remote repo and give it to request:


RemoteRepository local = new RemoteRepository.Builder("local", "default", "file:c:/Users/blah/.m2/repository").build();
RemoteRepository central = new RemoteRepository.Builder("central", "default", "http://central.maven.org/maven2/").build();

collectRequest.setRepositories( Arrays.asList(local, central));

2

Take look at this code:

https://github.com/liferay/liferay-blade-cli/blob/28556e7e8560dd27d4a5153cb93196ca059ac081/com.liferay.blade.cli/src/com/liferay/blade/cli/aether/AetherClient.java

Aether uses local repository by default, so simply set to not use remote if this is not required:

    DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession()
    session.setUpdatePolicy(RepositoryPolicy.UPDATE_POLICY_NEVER)
MariuszS
  • 30,646
  • 12
  • 114
  • 155
1

I can't say is it good or bad, but I've used this code, it works:

 private  File resolveInLocalRepo(DefaultArtifact artifact){
        LOG.debug("Trying to resolve $artifact in local repository...")
        def localRepoManager = new SimpleLocalRepositoryManager(localRepoRoot)
        def pathToLocalArtifact = localRepoManager.getPathForLocalArtifact(artifact)
        LOG.debug("pathToLocalArtifact: [$pathToLocalArtifact]")
        new File("$localRepoRoot.absolutePath/$pathToLocalArtifact")
    }

localRepoRoot is a file pointing to the root of local repo.

Capacytron
  • 3,425
  • 6
  • 47
  • 80
  • Be careful, I did it for maven 3.0.x, maven 3.1.x has incompatible api changes. You would have change package names for Aether – Capacytron Sep 18 '14 at 09:29