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.audit.AuditEvent;
24 import org.apache.archiva.common.utils.VersionUtil;
25 import org.apache.archiva.maven2.model.Artifact;
26 import org.apache.archiva.metadata.model.ArtifactMetadata;
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.rest.api.services.ArchivaRestServiceException;
33 import org.apache.archiva.rest.api.services.MergeRepositoriesService;
34 import org.apache.archiva.stagerepository.merge.Maven2RepositoryMerger;
35 import org.apache.archiva.stagerepository.merge.RepositoryMergerException;
36 import org.springframework.stereotype.Service;
38 import javax.inject.Inject;
39 import javax.inject.Named;
40 import java.util.ArrayList;
41 import java.util.List;
44 * @author Olivier Lamy
47 @Service ( "mergeRepositoriesService#rest" )
48 public class DefaultMergeRepositoriesService
49 extends AbstractRestService
50 implements MergeRepositoriesService
53 // FIXME check archiva-merge-repository to sourceRepoId
56 @Named ( value = "repositoryMerger#maven2" )
57 private Maven2RepositoryMerger repositoryMerger;
61 public List<Artifact> getMergeConflictedArtifacts( String sourceRepositoryId, String targetRepositoryId )
62 throws ArchivaRestServiceException
64 RepositorySession repositorySession = repositorySessionFactory.createSession();
67 List<ArtifactMetadata> artifactMetadatas =
68 repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepositoryId,
71 return buildArtifacts( artifactMetadatas, sourceRepositoryId );
73 catch ( RepositoryMergerException e )
75 throw new ArchivaRestServiceException( e.getMessage(), e );
79 repositorySession.close();
84 public void mergeRepositories( String sourceRepositoryId, String targetRepositoryId, boolean skipConflicts )
85 throws ArchivaRestServiceException
91 mergeBySkippingConflicts( sourceRepositoryId, targetRepositoryId );
95 doMerge( sourceRepositoryId, targetRepositoryId );
99 catch ( RepositoryMergerException e )
101 throw new ArchivaRestServiceException( e.getMessage(), e );
107 protected void doMerge( String sourceRepositoryId, String targetRepositoryId )
108 throws RepositoryMergerException, ArchivaRestServiceException
110 RepositorySession repositorySession = repositorySessionFactory.createSession();
114 ManagedRepository repository = managedRepositoryAdmin.getManagedRepository( targetRepositoryId );
115 MetadataRepository metadataRepository = repositorySession.getRepository();
116 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepositoryId );
118 if ( repository.isReleases() && !repository.isSnapshots() )
120 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepositoryId, targetRepositoryId );
124 repositoryMerger.merge( metadataRepository, sourceRepositoryId, targetRepositoryId );
126 for ( ArtifactMetadata metadata : sourceArtifacts )
128 triggerAuditEvent( targetRepositoryId, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
132 doScanRepository( targetRepositoryId, false );
134 catch ( MetadataRepositoryException e )
136 throw new ArchivaRestServiceException( e.getMessage(), e );
138 catch ( RepositoryAdminException e )
140 throw new ArchivaRestServiceException( e.getMessage(), e );
144 repositorySession.close();
148 public void mergeBySkippingConflicts( String sourceRepositoryId, String targetRepositoryId )
149 throws RepositoryMergerException, ArchivaRestServiceException
152 RepositorySession repositorySession = repositorySessionFactory.createSession();
155 List<ArtifactMetadata> conflictSourceArtifacts =
156 repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepositoryId,
157 targetRepositoryId );
158 MetadataRepository metadataRepository = repositorySession.getRepository();
159 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepositoryId );
160 sourceArtifacts.removeAll( conflictSourceArtifacts );
162 ManagedRepository repository = managedRepositoryAdmin.getManagedRepository( targetRepositoryId );
164 if ( repository.isReleases() && !repository.isSnapshots() )
166 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepositoryId, targetRepositoryId );
171 Filter<ArtifactMetadata> artifactsWithOutConflicts =
172 new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
173 repositoryMerger.merge( metadataRepository, sourceRepositoryId, targetRepositoryId,
174 artifactsWithOutConflicts );
175 for ( ArtifactMetadata metadata : sourceArtifacts )
177 triggerAuditEvent( targetRepositoryId, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
181 doScanRepository( targetRepositoryId, false );
183 catch ( MetadataRepositoryException e )
185 throw new ArchivaRestServiceException( e.getMessage(), e );
187 catch ( RepositoryAdminException e )
189 throw new ArchivaRestServiceException( e.getMessage(), e );
194 repositorySession.close();
198 private void mergeWithOutSnapshots( MetadataRepository metadataRepository, List<ArtifactMetadata> sourceArtifacts,
199 String sourceRepoId, String repoid )
200 throws RepositoryMergerException
202 List<ArtifactMetadata> artifactsWithOutSnapshots = new ArrayList<>();
203 for ( ArtifactMetadata metadata : sourceArtifacts )
205 if ( VersionUtil.isSnapshot( metadata.getProjectVersion() ) )
206 //if ( metadata.getProjectVersion().contains( VersionUtil.SNAPSHOT ) )
208 artifactsWithOutSnapshots.add( metadata );
212 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
216 sourceArtifacts.removeAll( artifactsWithOutSnapshots );
218 Filter<ArtifactMetadata> artifactListWithOutSnapShots = new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
219 repositoryMerger.merge( metadataRepository, sourceRepoId, repoid, artifactListWithOutSnapShots );