1 package org.apache.archiva.rest.services;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.common.utils.VersionUtil;
24 import org.apache.archiva.maven2.model.Artifact;
25 import org.apache.archiva.metadata.model.ArtifactMetadata;
26 import org.apache.archiva.metadata.model.facets.AuditEvent;
27 import org.apache.archiva.metadata.repository.MetadataRepository;
28 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
29 import org.apache.archiva.metadata.repository.RepositorySession;
30 import org.apache.archiva.metadata.repository.filter.Filter;
31 import org.apache.archiva.metadata.repository.filter.IncludesFilter;
32 import org.apache.archiva.repository.ReleaseScheme;
33 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
34 import org.apache.archiva.rest.api.services.MergeRepositoriesService;
35 import org.apache.archiva.stagerepository.merge.Maven2RepositoryMerger;
36 import org.apache.archiva.stagerepository.merge.RepositoryMergerException;
37 import org.springframework.stereotype.Service;
39 import javax.inject.Inject;
40 import javax.inject.Named;
41 import java.util.ArrayList;
42 import java.util.List;
45 * @author Olivier Lamy
48 @Service ( "mergeRepositoriesService#rest" )
49 public class DefaultMergeRepositoriesService
50 extends AbstractRestService
51 implements MergeRepositoriesService
54 // FIXME check archiva-merge-repository to sourceRepoId
57 @Named ( value = "repositoryMerger#maven2" )
58 private Maven2RepositoryMerger repositoryMerger;
62 public List<Artifact> getMergeConflictedArtifacts( String sourceRepositoryId, String targetRepositoryId )
63 throws ArchivaRestServiceException
65 RepositorySession repositorySession = repositorySessionFactory.createSession();
68 List<ArtifactMetadata> artifactMetadatas =
69 repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepositoryId,
72 return buildArtifacts( artifactMetadatas, sourceRepositoryId );
74 catch ( RepositoryMergerException e )
76 throw new ArchivaRestServiceException( e.getMessage(), e );
80 repositorySession.close();
85 public void mergeRepositories( String sourceRepositoryId, String targetRepositoryId, boolean skipConflicts )
86 throws ArchivaRestServiceException
92 mergeBySkippingConflicts( sourceRepositoryId, targetRepositoryId );
96 doMerge( sourceRepositoryId, targetRepositoryId );
100 catch ( RepositoryMergerException e )
102 throw new ArchivaRestServiceException( e.getMessage(), e );
108 protected void doMerge( String sourceRepositoryId, String targetRepositoryId )
109 throws RepositoryMergerException, ArchivaRestServiceException
111 RepositorySession repositorySession = repositorySessionFactory.createSession();
115 org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(targetRepositoryId);
116 MetadataRepository metadataRepository = repositorySession.getRepository();
117 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepositoryId );
119 if ( managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) && !managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) )
121 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepositoryId, targetRepositoryId );
125 repositoryMerger.merge( metadataRepository, sourceRepositoryId, targetRepositoryId );
127 for ( ArtifactMetadata metadata : sourceArtifacts )
129 triggerAuditEvent( targetRepositoryId, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
133 doScanRepository( targetRepositoryId, false );
135 catch ( MetadataRepositoryException e )
137 throw new ArchivaRestServiceException( e.getMessage(), e );
140 repositorySession.close();
144 public void mergeBySkippingConflicts( String sourceRepositoryId, String targetRepositoryId )
145 throws RepositoryMergerException, ArchivaRestServiceException
148 RepositorySession repositorySession = repositorySessionFactory.createSession();
151 List<ArtifactMetadata> conflictSourceArtifacts =
152 repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepositoryId,
153 targetRepositoryId );
154 MetadataRepository metadataRepository = repositorySession.getRepository();
155 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepositoryId );
156 sourceArtifacts.removeAll( conflictSourceArtifacts );
158 org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(targetRepositoryId);
160 if ( managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) && !managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT))
162 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepositoryId, targetRepositoryId );
167 Filter<ArtifactMetadata> artifactsWithOutConflicts =
168 new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
169 repositoryMerger.merge( metadataRepository, sourceRepositoryId, targetRepositoryId,
170 artifactsWithOutConflicts );
171 for ( ArtifactMetadata metadata : sourceArtifacts )
173 triggerAuditEvent( targetRepositoryId, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
177 doScanRepository( targetRepositoryId, false );
179 catch ( MetadataRepositoryException e )
181 throw new ArchivaRestServiceException( e.getMessage(), e );
184 repositorySession.close();
188 private void mergeWithOutSnapshots( MetadataRepository metadataRepository, List<ArtifactMetadata> sourceArtifacts,
189 String sourceRepoId, String repoid )
190 throws RepositoryMergerException
192 List<ArtifactMetadata> artifactsWithOutSnapshots = new ArrayList<>();
193 for ( ArtifactMetadata metadata : sourceArtifacts )
195 if ( VersionUtil.isSnapshot( metadata.getProjectVersion() ) )
196 //if ( metadata.getProjectVersion().contains( VersionUtil.SNAPSHOT ) )
198 artifactsWithOutSnapshots.add( metadata );
202 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
206 sourceArtifacts.removeAll( artifactsWithOutSnapshots );
208 Filter<ArtifactMetadata> artifactListWithOutSnapShots = new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
209 repositoryMerger.merge( metadataRepository, sourceRepoId, repoid, artifactListWithOutSnapShots );