001package org.apache.archiva.repository.maven2; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.dependency.tree.maven2.ArchivaRepositoryConnectorFactory; 023import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader; 024import org.apache.maven.repository.internal.DefaultVersionRangeResolver; 025import org.apache.maven.repository.internal.DefaultVersionResolver; 026import org.apache.maven.repository.internal.MavenRepositorySystemUtils; 027import org.eclipse.aether.DefaultRepositorySystemSession; 028import org.eclipse.aether.RepositorySystem; 029import org.eclipse.aether.RepositorySystemSession; 030import org.eclipse.aether.collection.DependencySelector; 031import org.eclipse.aether.impl.ArtifactDescriptorReader; 032import org.eclipse.aether.impl.DefaultServiceLocator; 033import org.eclipse.aether.impl.VersionRangeResolver; 034import org.eclipse.aether.impl.VersionResolver; 035import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; 036import org.eclipse.aether.repository.LocalRepository; 037import org.eclipse.aether.repository.LocalRepositoryManager; 038import org.eclipse.aether.repository.NoLocalRepositoryManagerException; 039import org.eclipse.aether.spi.connector.RepositoryConnectorFactory; 040import org.eclipse.aether.util.graph.selector.AndDependencySelector; 041import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector; 042import org.slf4j.Logger; 043import org.slf4j.LoggerFactory; 044import org.springframework.stereotype.Service; 045 046import javax.annotation.PostConstruct; 047 048/** 049 * Some static utility methods that are used by different classes. 050 */ 051@Service("mavenSystemManager") 052public class MavenSystemManager { 053 054 static Logger log = LoggerFactory.getLogger(MavenSystemManager.class); 055 056 private DefaultServiceLocator locator; 057 private RepositorySystem system; 058 059 @PostConstruct 060 private synchronized void init() { 061 locator = newLocator(); 062 system = newRepositorySystem(locator); 063 064 } 065 066 /** 067 * Creates a new aether repository system session for the given directory and assigns the 068 * repository to this session. 069 * 070 * @param localRepoDir The repository directory 071 * @return The newly created session object. 072 */ 073 public static RepositorySystemSession newRepositorySystemSession(String localRepoDir) { 074 DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession(); 075 076 LocalRepository repo = new LocalRepository(localRepoDir); 077 078 DependencySelector depFilter = new AndDependencySelector(new ExclusionDependencySelector()); 079 session.setDependencySelector(depFilter); 080 SimpleLocalRepositoryManagerFactory repFactory = new SimpleLocalRepositoryManagerFactory(); 081 try { 082 LocalRepositoryManager manager = repFactory.newInstance(session, repo); 083 session.setLocalRepositoryManager(manager); 084 } catch (NoLocalRepositoryManagerException e) { 085 log.error("Could not assign the repository manager to the session: {}", e.getMessage(), e); 086 } 087 088 return session; 089 } 090 091 public RepositorySystem getRepositorySystem() { 092 return system; 093 } 094 095 public DefaultServiceLocator getLocator() { 096 return locator; 097 } 098 099 /** 100 * Finds the 101 * 102 * @return 103 */ 104 public static RepositorySystem newRepositorySystem(DefaultServiceLocator locator) { 105 return locator.getService(RepositorySystem.class); 106 } 107 108 public static DefaultServiceLocator newLocator() { 109 DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator(); 110 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); 116 117 return locator; 118 } 119}