org.eclipse.aether.repository.LocalRepositoryManager Java Examples

The following examples show how to use org.eclipse.aether.repository.LocalRepositoryManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SingerMojo.java    From component-runtime with Apache License 2.0 6 votes vote down vote up
private Path findMain() {
    final LocalRepositoryManager lrm = repositorySystemSession.getLocalRepositoryManager();
    final Artifact artifact = new DefaultArtifact(GAV.GROUP, "component-kitap", "fatjar", "jar", GAV.VERSION);
    final File location = new File(lrm.getRepository().getBasedir(), lrm.getPathForLocalArtifact(artifact));
    if (!location.exists()) {
        final ArtifactRequest artifactRequest =
                new ArtifactRequest().setArtifact(artifact).setRepositories(remoteRepositories);
        try {
            final ArtifactResult result =
                    repositorySystem.resolveArtifact(repositorySystemSession, artifactRequest);
            if (result.isMissing()) {
                throw new IllegalStateException("Can't find " + artifact);
            }
            return result.getArtifact().getFile().toPath();
        } catch (final ArtifactResolutionException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }
    return location.toPath();
}
 
Example #2
Source File: MeecrowaveBundleMojo.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
private File resolve(final String group, final String artifact, final String version, final String classifier) {
    final DefaultArtifact art = new DefaultArtifact(group, artifact, classifier, "jar", version);
    final ArtifactRequest artifactRequest = new ArtifactRequest().setArtifact(art).setRepositories(remoteRepositories);

    final LocalRepositoryManager lrm = session.getLocalRepositoryManager();
    art.setFile(new File(lrm.getRepository().getBasedir(), lrm.getPathForLocalArtifact(artifactRequest.getArtifact())));

    try {
        final ArtifactResult result = repositorySystem.resolveArtifact(session, artifactRequest);
        if (result.isMissing()) {
            throw new IllegalStateException("Can't find commons-cli, please add it to the pom.");
        }
        return result.getArtifact().getFile();
    } catch (final ArtifactResolutionException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #3
Source File: MavenSystemManager.java    From archiva with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new aether repository system session for the given directory and assigns the
 * repository to this session.
 *
 * @param localRepoDir The repository directory
 * @return The newly created session object.
 */
public static RepositorySystemSession newRepositorySystemSession(String localRepoDir) {
    DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();

    LocalRepository repo = new LocalRepository(localRepoDir);

    DependencySelector depFilter = new AndDependencySelector(new ExclusionDependencySelector());
    session.setDependencySelector(depFilter);
    SimpleLocalRepositoryManagerFactory repFactory = new SimpleLocalRepositoryManagerFactory();
    try {
        LocalRepositoryManager manager = repFactory.newInstance(session, repo);
        session.setLocalRepositoryManager(manager);
    } catch (NoLocalRepositoryManagerException e) {
        log.error("Could not assign the repository manager to the session: {}", e.getMessage(), e);
    }

    return session;
}
 
Example #4
Source File: PluginAddCommand.java    From gyro with Apache License 2.0 5 votes vote down vote up
private boolean validate(String plugin) {
    try {
        DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();

        locator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
        locator.addService(TransporterFactory.class, FileTransporterFactory.class);
        locator.addService(TransporterFactory.class, HttpTransporterFactory.class);

        RepositorySystem system = locator.getService(RepositorySystem.class);
        DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
        String localDir = Paths.get(System.getProperty("user.home"), ".m2", "repository").toString();
        LocalRepository local = new LocalRepository(localDir);
        LocalRepositoryManager manager = system.newLocalRepositoryManager(session, local);

        session.setLocalRepositoryManager(manager);

        Artifact artifact = new DefaultArtifact(plugin);
        Dependency dependency = new Dependency(artifact, JavaScopes.RUNTIME);
        DependencyFilter filter = DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME);
        CollectRequest collectRequest = new CollectRequest(dependency, repositories);
        DependencyRequest request = new DependencyRequest(collectRequest, filter);
        system.resolveDependencies(session, request);

        return true;
    } catch (DependencyResolutionException e) {
        GyroCore.ui().write("@|bold %s|@ was not installed for the following reason(s):\n", plugin);

        for (Exception ex : e.getResult().getCollectExceptions()) {
            GyroCore.ui().write("   @|red %s|@\n", ex.getMessage());
        }

        GyroCore.ui().write("\n");

        return false;
    }
}
 
Example #5
Source File: ComponentDependenciesBase.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
protected Artifact resolve(final Artifact dep, final String classifier, final String type) {
    final LocalRepositoryManager lrm = repositorySystemSession.getLocalRepositoryManager();
    final Artifact artifact =
            new DefaultArtifact(dep.getGroupId(), dep.getArtifactId(), classifier, type, getVersion(dep));
    final File location = new File(lrm.getRepository().getBasedir(), lrm.getPathForLocalArtifact(artifact));
    if (!location.exists()) {
        return resolve(artifact);
    }
    return artifact.setFile(location);
}
 
Example #6
Source File: MavenArtifactResolvingHelperTest.java    From thorntail with Apache License 2.0 5 votes vote down vote up
public MavenArtifactResolvingHelperTest() {
    sessionMock = Mockito.mock(RepositorySystemSession.class);
    MirrorSelector mirrorSelectorMock = Mockito.mock(MirrorSelector.class);
    Mockito.when(sessionMock.getMirrorSelector()).thenReturn(mirrorSelectorMock);
    ProxySelector proxySelectorMock = Mockito.mock(ProxySelector.class);
    Mockito.when(sessionMock.getProxySelector()).thenReturn(proxySelectorMock);
    localRepositoryManager = Mockito.mock(LocalRepositoryManager.class);
    Mockito.when(sessionMock.getLocalRepositoryManager()).thenReturn(localRepositoryManager);
    resolver = Mockito.mock(ArtifactResolver.class);
    system = Mockito.mock(RepositorySystem.class);

}
 
Example #7
Source File: DependencyResolver.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
static DefaultRepositorySystemSession newRepositorySystemSession(RepositorySystem system) {
    DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
    session.setCache(newRepositoryCache());
    String localRepositoryPath = resolveLocalRepository();
    if (logger.isLoggable(Level.INFO)) {
        logger.info("Local repository: " + localRepositoryPath);
    }
    LocalRepository localRepository = new LocalRepository(localRepositoryPath);

    LocalRepositoryManager localRepositoryManager = system.newLocalRepositoryManager(session, localRepository);
    session.setLocalRepositoryManager(localRepositoryManager);

    return session;
}
 
Example #8
Source File: RepositoryCacheResolver.java    From frontend-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public File resolve(CacheDescriptor cacheDescriptor) {
  LocalRepositoryManager manager = repositorySystemSession.getLocalRepositoryManager();
  File localArtifact = new File(
      manager.getRepository().getBasedir(),
      manager.getPathForLocalArtifact(createArtifact(cacheDescriptor))
  );
  return localArtifact;
}
 
Example #9
Source File: PluginPreprocessor.java    From gyro with Apache License 2.0 4 votes vote down vote up
@Override
public List<Node> preprocess(List<Node> nodes, RootScope scope) {
    PluginSettings settings = scope.getSettings(PluginSettings.class);

    List<String> artifactCoords = new ArrayList<>();
    List<Node> repositoryNodes = new ArrayList<>();

    for (Node node : nodes) {
        if (node instanceof DirectiveNode) {
            if ("plugin".equals(((DirectiveNode) node).getName())) {
                artifactCoords.add(getArtifactCoord((DirectiveNode) node));
            } else if ("repository".equals(((DirectiveNode) node).getName())) {
                repositoryNodes.add(node);
            }
        }
    }

    if (artifactCoords.stream().allMatch(settings::pluginInitialized)) {
        return nodes;
    }

    NodeEvaluator evaluator = new NodeEvaluator();
    evaluator.evaluate(scope, repositoryNodes);

    for (String ac : artifactCoords) {
        if (settings.pluginInitialized(ac)) {
            continue;
        }

        try {
            GyroCore.ui().write("@|magenta ↓ Loading plugin:|@ %s\n", ac);

            DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();

            locator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
            locator.addService(TransporterFactory.class, FileTransporterFactory.class);
            locator.addService(TransporterFactory.class, HttpTransporterFactory.class);

            RepositorySystem system = locator.getService(RepositorySystem.class);
            DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
            String localDir = Paths.get(System.getProperty("user.home"), ".m2", "repository").toString();
            LocalRepository local = new LocalRepository(localDir);
            LocalRepositoryManager manager = system.newLocalRepositoryManager(session, local);

            session.setLocalRepositoryManager(manager);

            Artifact artifact = new DefaultArtifact(ac);
            Dependency dependency = new Dependency(artifact, JavaScopes.RUNTIME);
            DependencyFilter filter = DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME);
            List<RemoteRepository> repositories = scope.getSettings(RepositorySettings.class).getRepositories();
            CollectRequest collectRequest = new CollectRequest(dependency, repositories);
            DependencyRequest request = new DependencyRequest(collectRequest, filter);
            DependencyResult result = system.resolveDependencies(session, request);

            settings.putDependencyResult(ac, result);

            for (ArtifactResult ar : result.getArtifactResults()) {
                settings.putArtifactIfNewer(ar.getArtifact());
            }
        } catch (Exception error) {
            throw new GyroException(
                String.format("Can't load the @|bold %s|@ plugin!", ac),
                error);
        }
    }

    settings.addAllUrls();

    return nodes;
}
 
Example #10
Source File: MavenLocalRepositoryManager.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public MavenLocalRepositoryManager(LocalRepositoryManager delegate, Path userLocalRepo) {
    this(delegate, userLocalRepo, false);
}
 
Example #11
Source File: MavenLocalRepositoryManager.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public MavenLocalRepositoryManager(LocalRepositoryManager delegate, Path userLocalRepo, boolean relinkResolvedArtifacts) {
    this.delegate = delegate;
    this.userLocalRepo = userLocalRepo;
    this.appCreatorRepo = delegate.getRepository().getBasedir().toPath();
    this.relinkResolvedArtifacts = relinkResolvedArtifacts;
}
 
Example #12
Source File: RepositorySystemSessionWrapper.java    From wildfly-swarm with Apache License 2.0 4 votes vote down vote up
@Override
public LocalRepositoryManager getLocalRepositoryManager() {
    return delegate.getLocalRepositoryManager();
}
 
Example #13
Source File: RepositorySystemSessionWrapper.java    From thorntail with Apache License 2.0 4 votes vote down vote up
@Override
public LocalRepositoryManager getLocalRepositoryManager() {
    return delegate.getLocalRepositoryManager();
}
 
Example #14
Source File: UpdateStageDependenciesMojo.java    From gitflow-helper-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Override
protected void execute(final GitBranchInfo branchInfo) throws MojoExecutionException, MojoFailureException {
    getLog().debug("update-stage-dependencies setting up Repository session...");

    DefaultRepositorySystemSession reresolveSession = new DefaultRepositorySystemSession(repositorySystemSession);
    reresolveSession.setUpdatePolicy(org.eclipse.aether.repository.RepositoryPolicy.UPDATE_POLICY_ALWAYS);
    reresolveSession.setCache(new DefaultRepositoryCache());

    LocalRepositoryManager localRepositoryManager = reresolveSession.getLocalRepositoryManager();

    getLog().debug("configuring stage as the remote repository for artifact resolution requests...");
    List<RemoteRepository> stageRepo = Arrays.asList(RepositoryUtils.toRepo(getDeploymentRepository(stageDeploymentRepository)));

    boolean itemsPurged = false;

    try {
        DependencyResolutionResult depencencyResult = dependenciesResolver.resolve(
                new DefaultDependencyResolutionRequest(project, reresolveSession));

        for (Dependency dependency : depencencyResult.getResolvedDependencies()) {
            if (!dependency.getArtifact().isSnapshot()) {
                // Find the artifact in the local repo, and if it came from the 'stageRepo', populate that info
                // as the 'repository' on the artifact.
                LocalArtifactResult localResult = localRepositoryManager.find(reresolveSession,
                        new LocalArtifactRequest(dependency.getArtifact(), stageRepo, null));

                // If the result has a file... and the getRepository() matched the stage repo id...
                if (localResult.getFile() != null && localResult.getRepository() != null) {
                    getLog().info("Purging: " + dependency + " from remote repository: " + localResult.getRepository() + ".");
                    File deleteTarget = new File(localRepositoryManager.getRepository().getBasedir(), localRepositoryManager.getPathForLocalArtifact(dependency.getArtifact()));

                    if (deleteTarget.isDirectory()) {
                        try {
                            FileUtils.deleteDirectory(deleteTarget);
                        } catch (IOException ioe) {
                            getLog().warn("Failed to purge stage artifact from local repository: " + deleteTarget, ioe);
                        }
                    } else if (!deleteTarget.delete()) {
                        getLog().warn("Failed to purge stage artifact from local repository: " + deleteTarget);
                    }
                    itemsPurged = true;
                }
            }
        }
    } catch (DependencyResolutionException dre) {
        throw new MojoExecutionException("Initial dependency resolution to resolve dependencies which may have been provided by the 'stage' repository failed.", dre);
    }


    if (itemsPurged) {
        try {
            getLog().info("Resolving purged dependencies...");
            dependenciesResolver.resolve(new DefaultDependencyResolutionRequest(project, reresolveSession));
            getLog().info("All stage dependencies purged and re-resolved.");
        } catch (DependencyResolutionException e) {
            throw new MojoExecutionException("Post-purge dependency resolution failed!", e);
        }
    }
}
 
Example #15
Source File: ArchiveCache.java    From phantomjs-maven-plugin with MIT License 4 votes vote down vote up
public File getFile(Archive archive) {
  Artifact artifact = artifactBuilder.createArtifact(archive);
  LocalRepositoryManager manager = repositorySystemSession.getLocalRepositoryManager();
  return new File(manager.getRepository().getBasedir(), manager.getPathForLocalArtifact(artifact));
}