1 package org.apache.archiva.repository.maven;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.repository.maven.dependency.tree.ArchivaRepositoryConnectorFactory;
23 import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader;
24 import org.apache.maven.repository.internal.DefaultVersionRangeResolver;
25 import org.apache.maven.repository.internal.DefaultVersionResolver;
26 import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
27 import org.eclipse.aether.DefaultRepositorySystemSession;
28 import org.eclipse.aether.RepositorySystem;
29 import org.eclipse.aether.RepositorySystemSession;
30 import org.eclipse.aether.collection.DependencySelector;
31 import org.eclipse.aether.impl.ArtifactDescriptorReader;
32 import org.eclipse.aether.impl.DefaultServiceLocator;
33 import org.eclipse.aether.impl.VersionRangeResolver;
34 import org.eclipse.aether.impl.VersionResolver;
35 import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
36 import org.eclipse.aether.repository.LocalRepository;
37 import org.eclipse.aether.repository.LocalRepositoryManager;
38 import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
39 import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
40 import org.eclipse.aether.util.graph.selector.AndDependencySelector;
41 import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.stereotype.Service;
46 import javax.annotation.PostConstruct;
49 * Some static utility methods that are used by different classes.
51 @Service("mavenSystemManager")
52 public class MavenSystemManager {
54 static Logger log = LoggerFactory.getLogger(MavenSystemManager.class);
56 private DefaultServiceLocator locator;
57 private RepositorySystem system;
60 private synchronized void init() {
61 locator = newLocator();
62 system = newRepositorySystem(locator);
67 * Creates a new aether repository system session for the given directory and assigns the
68 * repository to this session.
70 * @param localRepoDir The repository directory
71 * @return The newly created session object.
73 public static RepositorySystemSession newRepositorySystemSession(String localRepoDir) {
74 DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
76 LocalRepository repo = new LocalRepository(localRepoDir);
78 DependencySelector depFilter = new AndDependencySelector(new ExclusionDependencySelector());
79 session.setDependencySelector(depFilter);
80 SimpleLocalRepositoryManagerFactory repFactory = new SimpleLocalRepositoryManagerFactory();
82 LocalRepositoryManager manager = repFactory.newInstance(session, repo);
83 session.setLocalRepositoryManager(manager);
84 } catch (NoLocalRepositoryManagerException e) {
85 log.error("Could not assign the repository manager to the session: {}", e.getMessage(), e);
91 public RepositorySystem getRepositorySystem() {
95 public DefaultServiceLocator getLocator() {
104 public static RepositorySystem newRepositorySystem(DefaultServiceLocator locator) {
105 return locator.getService(RepositorySystem.class);
108 public static DefaultServiceLocator newLocator() {
109 DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
111 locator.addService(RepositoryConnectorFactory.class,
112 ArchivaRepositoryConnectorFactory.class);// FileRepositoryConnectorFactory.class );
113 locator.addService(VersionResolver.class, DefaultVersionResolver.class);
114 locator.addService(VersionRangeResolver.class, DefaultVersionRangeResolver.class);
115 locator.addService(ArtifactDescriptorReader.class, DefaultArtifactDescriptorReader.class);