Search in sources :

Example 1 with Repository

use of org.apache.maven.settings.Repository in project karaf by apache.

the class MavenConfigurationSupport method repositories.

/**
 * Returns list of configured remote (<code>remote=true</code>) or default (<code>remote=false</code>)
 * repositories.
 * @param remote
 * @return
 */
protected MavenRepositoryURL[] repositories(Dictionary<String, Object> config, boolean remote) throws Exception {
    String property = remote ? PID + "." + PROPERTY_REPOSITORIES : PID + "." + PROPERTY_DEFAULT_REPOSITORIES;
    String[] repositories = listOfValues((String) config.get(property));
    if (remote) {
        if (repositories.length == 0 || repositories[0].charAt(0) == '+') {
            if (repositories.length > 0) {
                repositories[0] = repositories[0].substring(1);
            }
            List<String> newRepositories = new LinkedList<>();
            newRepositories.addAll(Arrays.asList(repositories));
            // append all repositories from all active profiles from available settings.xml
            if (mavenSettings != null) {
                // see org.ops4j.pax.url.mvn.internal.config.MavenConfigurationImpl.getRepositories()
                Set<String> activeProfiles = new LinkedHashSet<>(mavenSettings.getActiveProfiles());
                Map<String, Profile> profiles = (Map<String, Profile>) mavenSettings.getProfilesAsMap();
                profiles.values().stream().filter((profile) -> profile.getActivation() != null && profile.getActivation().isActiveByDefault()).map(Profile::getId).forEach(activeProfiles::add);
                for (String activeProfile : activeProfiles) {
                    Profile profile = profiles.get(activeProfile);
                    if (profile == null) {
                        continue;
                    }
                    for (Repository repo : profile.getRepositories()) {
                        StringBuilder builder = new StringBuilder();
                        builder.append(repo.getUrl());
                        builder.append("@id=").append(repo.getId());
                        builder.append("@_from=").append(MavenRepositoryURL.FROM.SETTINGS);
                        if (repo.getReleases() != null) {
                            if (!repo.getReleases().isEnabled()) {
                                builder.append(ServiceConstants.SEPARATOR_OPTIONS + ServiceConstants.OPTION_DISALLOW_RELEASES);
                            }
                            SourceAnd<String> up = updatePolicy(repo.getReleases().getUpdatePolicy());
                            addPolicy(builder, "".equals(up.val()) ? "never" : up.val(), ServiceConstants.OPTION_RELEASES_UPDATE);
                        // not used in pax-url-aether
                        // addPolicy(builder, repo.getReleases().getChecksumPolicy(), "releasesChecksum");
                        }
                        if (repo.getSnapshots() != null) {
                            if (repo.getSnapshots().isEnabled()) {
                                builder.append(ServiceConstants.SEPARATOR_OPTIONS + ServiceConstants.OPTION_ALLOW_SNAPSHOTS);
                            }
                            SourceAnd<String> up = updatePolicy(repo.getSnapshots().getUpdatePolicy());
                            addPolicy(builder, "".equals(up.val()) ? "never" : up.val(), ServiceConstants.OPTION_SNAPSHOTS_UPDATE);
                        // not used in pax-url-aether
                        // addPolicy(builder, repo.getSnapshots().getChecksumPolicy(), "snapshotsChecksum");
                        }
                        newRepositories.add(builder.toString());
                    }
                }
            }
            repositories = newRepositories.toArray(new String[newRepositories.size()]);
        }
    }
    List<MavenRepositoryURL> result = new LinkedList<>();
    for (String repo : repositories) {
        result.add(new MavenRepositoryURL(repo));
    }
    return result.toArray(new MavenRepositoryURL[result.size()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LinkedList(java.util.LinkedList) Profile(org.apache.maven.settings.Profile) Repository(org.apache.maven.settings.Repository) MavenRepositoryURL(org.apache.karaf.maven.core.MavenRepositoryURL) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with Repository

use of org.apache.maven.settings.Repository in project karaf by apache.

the class RepositoryChangeCommand method edit.

@Override
protected void edit(String prefix, Dictionary<String, Object> config, MavenRepositoryURL[] allRepos, MavenRepositoryURL[] pidRepos, MavenRepositoryURL[] settingsRepos) throws Exception {
    Optional<MavenRepositoryURL> first = Arrays.stream(allRepos).filter((repo) -> id.equals(repo.getId())).findFirst();
    if (!first.isPresent()) {
        System.err.printf("Can't find %s repository with ID \"%s\"\n", (defaultRepository ? "default" : "remote"), id);
        return;
    }
    MavenRepositoryURL changedRepository = first.get();
    changedRepository.setSnapshotsEnabled(snapshots);
    changedRepository.setReleasesEnabled(!noReleases);
    if (updatePolicy != null) {
        SourceAnd<String> up = updatePolicy(updatePolicy);
        if (!up.valid) {
            System.err.println("Unknown value of update policy: \"" + updatePolicy + "\"");
            return;
        }
        changedRepository.setReleasesUpdatePolicy(up.val());
        changedRepository.setSnapshotsUpdatePolicy(up.val());
    }
    if (checksumPolicy != null) {
        SourceAnd<String> cp = checksumPolicy(checksumPolicy);
        if (!cp.valid) {
            System.err.println("Unknown value of checksum policy: \"" + checksumPolicy + "\"");
            return;
        }
        changedRepository.setReleasesChecksumPolicy(cp.val());
        changedRepository.setSnapshotsChecksumPolicy(cp.val());
    }
    if (uri != null) {
        SourceAnd<String> urlResolved = validateRepositoryURL(uri, defaultRepository);
        if (!urlResolved.valid) {
            return;
        }
        changedRepository.setURL(new URL(urlResolved.val()));
    }
    boolean hasUsername = username != null && !"".equals(username.trim());
    boolean hasPassword = password != null && !"".equals(password.trim());
    boolean hasCredentials = hasUsername && hasPassword;
    if ((hasUsername && !hasPassword) || (!hasUsername && hasPassword)) {
        System.err.println("Please specify both username and password");
        return;
    }
    if (defaultRepository && hasCredentials) {
        System.out.println("User credentials won't be used for default repository");
    // no return
    }
    boolean credentialsUpdated = false;
    // credentials for remote repository can be stored only in settings.xml
    if (!defaultRepository && hasCredentials) {
        if (!updateCredentials(force, id, username, password, prefix, config)) {
            return;
        }
        credentialsUpdated = true;
    }
    if (!defaultRepository && changedRepository.getFrom() == MavenRepositoryURL.FROM.SETTINGS) {
        // find <repository> in any active profile to change it
        for (Profile profile : mavenSettings.getProfiles()) {
            Optional<Repository> repository = profile.getRepositories().stream().filter((r) -> id.equals(r.getId())).findFirst();
            if (repository.isPresent()) {
                // I can't imagine it's not...
                Repository r = repository.get();
                r.setUrl(changedRepository.getURL().toString());
                if (!changedRepository.isSnapshotsEnabled()) {
                    r.setSnapshots(new RepositoryPolicy());
                    r.getSnapshots().setEnabled(false);
                } else {
                    RepositoryPolicy rp = r.getSnapshots() == null ? new RepositoryPolicy() : r.getSnapshots();
                    rp.setEnabled(true);
                    if (checksumPolicy != null) {
                        rp.setChecksumPolicy(changedRepository.getSnapshotsChecksumPolicy());
                    } else if (rp.getChecksumPolicy() == null) {
                        rp.setChecksumPolicy("warn");
                    }
                    if (updatePolicy != null) {
                        rp.setUpdatePolicy(changedRepository.getSnapshotsUpdatePolicy());
                    } else if (rp.getUpdatePolicy() == null) {
                        rp.setUpdatePolicy("daily");
                    }
                    r.setSnapshots(rp);
                }
                if (!changedRepository.isReleasesEnabled()) {
                    r.setReleases(new RepositoryPolicy());
                    r.getReleases().setEnabled(false);
                } else {
                    RepositoryPolicy rp = r.getReleases() == null ? new RepositoryPolicy() : r.getReleases();
                    rp.setEnabled(true);
                    if (checksumPolicy != null) {
                        rp.setChecksumPolicy(changedRepository.getReleasesChecksumPolicy());
                    } else if (rp.getChecksumPolicy() == null) {
                        rp.setChecksumPolicy("warn");
                    }
                    if (updatePolicy != null) {
                        rp.setUpdatePolicy(changedRepository.getReleasesUpdatePolicy());
                    } else if (rp.getUpdatePolicy() == null) {
                        rp.setUpdatePolicy("daily");
                    }
                    r.setReleases(rp);
                }
                updateSettings(prefix, config);
                break;
            }
        }
    } else if (changedRepository.getFrom() == MavenRepositoryURL.FROM.PID) {
        List<MavenRepositoryURL> newRepos = new LinkedList<>();
        for (MavenRepositoryURL repo : pidRepos) {
            MavenRepositoryURL _r = repo;
            if (id.equals(repo.getId())) {
                _r = changedRepository;
            }
            newRepos.add(_r);
        }
        updatePidRepositories(prefix, config, defaultRepository, newRepos, settingsRepos.length > 0);
        if (credentialsUpdated) {
            updateSettings(prefix, config);
        }
    }
    Configuration cmConfig = cm.getConfiguration(PID);
    cmConfig.update(config);
    success = true;
}
Also used : Arrays(java.util.Arrays) Repository(org.apache.maven.settings.Repository) URL(java.net.URL) Argument(org.apache.karaf.shell.api.action.Argument) Command(org.apache.karaf.shell.api.action.Command) List(java.util.List) MavenRepositoryURL(org.apache.karaf.maven.core.MavenRepositoryURL) Configuration(org.osgi.service.cm.Configuration) Profile(org.apache.maven.settings.Profile) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Optional(java.util.Optional) RepositoryPolicy(org.apache.maven.settings.RepositoryPolicy) LinkedList(java.util.LinkedList) Option(org.apache.karaf.shell.api.action.Option) Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) RepositoryPolicy(org.apache.maven.settings.RepositoryPolicy) URL(java.net.URL) MavenRepositoryURL(org.apache.karaf.maven.core.MavenRepositoryURL) Profile(org.apache.maven.settings.Profile) Repository(org.apache.maven.settings.Repository) List(java.util.List) LinkedList(java.util.LinkedList) MavenRepositoryURL(org.apache.karaf.maven.core.MavenRepositoryURL)

Example 3 with Repository

use of org.apache.maven.settings.Repository in project fabric8 by jboss-fuse.

the class AetherResolutionWithHintsTest method settingsWithProxy.

private Settings settingsWithProxy() {
    Settings settings = new Settings();
    Proxy proxy = new Proxy();
    proxy.setId("proxy");
    proxy.setHost("localhost");
    proxy.setPort(port);
    proxy.setProtocol("http");
    settings.setProxies(Collections.singletonList(proxy));
    Profile defaultProfile = new Profile();
    defaultProfile.setId("default");
    Repository repo1 = new Repository();
    repo1.setId("repo1");
    repo1.setUrl("http://localhost:1111/repository");
    Repository repo2 = new Repository();
    repo2.setId("repo2");
    repo2.setUrl("http://localhost:2222/repository");
    Repository repo3 = new Repository();
    repo3.setId("repo3");
    repo3.setUrl("http://localhost:3333/repository");
    defaultProfile.addRepository(repo1);
    defaultProfile.addRepository(repo2);
    defaultProfile.addRepository(repo3);
    settings.addProfile(defaultProfile);
    settings.addActiveProfile("default");
    return settings;
}
Also used : Proxy(org.apache.maven.settings.Proxy) Repository(org.apache.maven.settings.Repository) Settings(org.apache.maven.settings.Settings) Profile(org.apache.maven.settings.Profile)

Example 4 with Repository

use of org.apache.maven.settings.Repository in project gate-core by GateNLP.

the class Utils method getRepositoryList.

public static List<RemoteRepository> getRepositoryList() throws SettingsBuildingException {
    List<RemoteRepository> repos = new ArrayList<RemoteRepository>();
    RemoteRepository central = new RemoteRepository.Builder("central", "default", "http://repo1.maven.org/maven2/").build();
    // Without this we wouldn't be able to find SNAPSHOT builds of plugins we
    // haven't built and installed locally ourselves
    RemoteRepository gateRepo = new RemoteRepository.Builder("gate", "default", "http://repo.gate.ac.uk/content/groups/public/").build();
    // Add all repos from settings.xml
    // http://stackoverflow.com/questions/27818659/loading-mavens-settings-xml-for-jcabi-aether-to-use
    Settings effectiveSettings = loadMavenSettings();
    org.apache.maven.settings.Proxy proxy = effectiveSettings.getActiveProxy();
    DefaultProxySelector selector = null;
    if (proxy != null) {
        selector = new DefaultProxySelector();
        selector.add(new Proxy(proxy.getProtocol(), proxy.getHost(), proxy.getPort(), new AuthenticationBuilder().addUsername(proxy.getUsername()).addPassword(proxy.getPassword()).build()), proxy.getNonProxyHosts());
    }
    Map<String, Profile> profilesMap = effectiveSettings.getProfilesAsMap();
    for (String profileName : effectiveSettings.getActiveProfiles()) {
        Profile profile = profilesMap.get(profileName);
        List<Repository> repositories = profile.getRepositories();
        for (Repository repo : repositories) {
            RemoteRepository remoteRepo = new RemoteRepository.Builder(repo.getId(), "default", repo.getUrl()).build();
            if (selector != null) {
                remoteRepo = new RemoteRepository.Builder(remoteRepo).setProxy(selector.getProxy(remoteRepo)).build();
            }
            repos.add(remoteRepo);
        }
    }
    repos.add(central);
    repos.add(gateRepo);
    return repos;
}
Also used : AuthenticationBuilder(org.eclipse.aether.util.repository.AuthenticationBuilder) DefaultProxySelector(org.eclipse.aether.util.repository.DefaultProxySelector) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) Profile(org.apache.maven.settings.Profile) Proxy(org.eclipse.aether.repository.Proxy) Repository(org.apache.maven.settings.Repository) LocalRepository(org.eclipse.aether.repository.LocalRepository) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) Settings(org.apache.maven.settings.Settings)

Example 5 with Repository

use of org.apache.maven.settings.Repository in project pom-manipulation-ext by release-engineering.

the class MavenLocationExpander method addSettingsProfileRepositoriesTo.

private void addSettingsProfileRepositoriesTo(final Set<Location> locs, final Settings settings, final List<String> activeProfiles, final MirrorSelector mirrorSelector) throws MalformedURLException {
    if (settings != null) {
        final Map<String, Profile> profiles = settings.getProfilesAsMap();
        if (profiles != null && activeProfiles != null && !activeProfiles.isEmpty()) {
            final LinkedHashSet<String> active = new LinkedHashSet<>(activeProfiles);
            final List<String> settingsActiveProfiles = settings.getActiveProfiles();
            if (settingsActiveProfiles != null && !settingsActiveProfiles.isEmpty()) {
                active.addAll(settingsActiveProfiles);
            }
            for (final String profileId : active) {
                final Profile profile = profiles.get(profileId);
                if (profile != null) {
                    final List<Repository> repositories = profile.getRepositories();
                    if (repositories != null) {
                        final List<Mirror> mirrors = settings.getMirrors();
                        final ArtifactRepositoryLayout layout = new DefaultRepositoryLayout();
                        for (final Repository repo : repositories) {
                            String id = repo.getId();
                            String url = repo.getUrl();
                            if (mirrors != null) {
                                final ArtifactRepositoryPolicy snapshots = convertPolicy(repo.getSnapshots());
                                final ArtifactRepositoryPolicy releases = convertPolicy(repo.getReleases());
                                final MavenArtifactRepository arepo = new MavenArtifactRepository(id, url, layout, snapshots, releases);
                                final Mirror mirror = mirrorSelector == null ? null : mirrorSelector.getMirror(arepo, mirrors);
                                if (mirror != null) {
                                    id = mirror.getId();
                                    url = mirror.getUrl();
                                }
                                SimpleHttpLocation addition = new SimpleHttpLocation(id, url, snapshots.isEnabled(), releases.isEnabled(), true, false, null);
                                addition.setAttribute(Location.CONNECTION_TIMEOUT_SECONDS, 60);
                                locs.add(addition);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArtifactRepositoryLayout(org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout) SimpleHttpLocation(org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation) DefaultRepositoryLayout(org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) Profile(org.apache.maven.settings.Profile) Repository(org.apache.maven.settings.Repository) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) ArtifactRepositoryPolicy(org.apache.maven.artifact.repository.ArtifactRepositoryPolicy) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) Mirror(org.apache.maven.settings.Mirror)

Aggregations

Repository (org.apache.maven.settings.Repository)13 Profile (org.apache.maven.settings.Profile)12 Settings (org.apache.maven.settings.Settings)6 MavenRepositoryURL (org.apache.karaf.maven.core.MavenRepositoryURL)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Dictionary (java.util.Dictionary)2 LinkedHashSet (java.util.LinkedHashSet)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Optional (java.util.Optional)2 Command (org.apache.karaf.shell.api.action.Command)2 Service (org.apache.karaf.shell.api.action.lifecycle.Service)2 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)2 ArtifactRepositoryPolicy (org.apache.maven.artifact.repository.ArtifactRepositoryPolicy)2 MavenArtifactRepository (org.apache.maven.artifact.repository.MavenArtifactRepository)2 ArtifactRepositoryLayout (org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout)2 DefaultRepositoryLayout (org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout)2 Mirror (org.apache.maven.settings.Mirror)2