1 package org.apache.archiva.stagerepository.merge;
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.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.repository.MetadataRepository;
24 import org.apache.archiva.metadata.repository.filter.Filter;
25 import org.apache.archiva.metadata.repository.filter.IncludesFilter;
26 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
27 import org.apache.commons.io.IOUtils;
28 import org.apache.maven.archiva.common.utils.VersionComparator;
29 import org.apache.maven.archiva.common.utils.VersionUtil;
30 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.Configuration;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
34 import org.apache.maven.archiva.repository.RepositoryException;
35 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
36 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
37 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataWriter;
40 import java.io.FileInputStream;
41 import java.io.FileOutputStream;
42 import java.io.IOException;
43 import java.text.DateFormat;
44 import java.text.SimpleDateFormat;
45 import java.util.ArrayList;
46 import java.util.Calendar;
47 import java.util.Collections;
48 import java.util.Date;
49 import java.util.List;
50 import java.util.TimeZone;
53 * @plexus.component role="org.apache.archiva.stagerepository.merge.RepositoryMerger" role-hint="maven2"
55 public class Maven2RepositoryMerger
56 implements RepositoryMerger
59 * @plexus.requirement role-hint="default"
61 private ArchivaConfiguration configuration;
64 * @plexus.requirement role-hint="maven2"
66 private RepositoryPathTranslator pathTranslator;
68 private static final String METADATA_FILENAME = "maven-metadata.xml";
70 public void setConfiguration( ArchivaConfiguration configuration )
72 this.configuration = configuration;
75 public void merge( MetadataRepository metadataRepository, String sourceRepoId, String targetRepoId )
79 List<ArtifactMetadata> artifactsInSourceRepo = metadataRepository.getArtifacts( sourceRepoId );
80 for ( ArtifactMetadata artifactMetadata : artifactsInSourceRepo )
82 artifactMetadata.setRepositoryId( targetRepoId );
83 createFolderStructure( sourceRepoId, targetRepoId, artifactMetadata );
87 // TODO when UI needs a subset to merge
88 public void merge( MetadataRepository metadataRepository, String sourceRepoId, String targetRepoId,
89 Filter<ArtifactMetadata> filter )
92 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepoId );
93 for ( ArtifactMetadata metadata : sourceArtifacts )
95 if ( filter.accept( metadata ) )
97 createFolderStructure( sourceRepoId, targetRepoId, metadata );
102 private void createFolderStructure( String sourceRepoId, String targetRepoId, ArtifactMetadata artifactMetadata )
103 throws IOException, RepositoryException
105 Configuration config = configuration.getConfiguration();
107 ManagedRepositoryConfiguration targetRepoConfig = config.findManagedRepositoryById( targetRepoId );
109 ManagedRepositoryConfiguration sourceRepoConfig = config.findManagedRepositoryById( sourceRepoId );
111 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
113 TimeZone timezone = TimeZone.getTimeZone( "UTC" );
115 DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
117 fmt.setTimeZone( timezone );
119 String timestamp = fmt.format( lastUpdatedTimestamp );
121 String targetRepoPath = targetRepoConfig.getLocation();
123 String sourceRepoPath = sourceRepoConfig.getLocation();
125 String artifactPath = pathTranslator.toPath( artifactMetadata.getNamespace(), artifactMetadata.getProject(),
126 artifactMetadata.getProjectVersion(), artifactMetadata.getId() );
128 File sourceArtifactFile = new File( sourceRepoPath, artifactPath );
130 File targetArtifactFile = new File( targetRepoPath, artifactPath );
132 int lastIndex = artifactPath.lastIndexOf( '/' );
134 File targetFile = new File( targetRepoPath, artifactPath.substring( 0, lastIndex ) );
136 if ( !targetFile.exists() )
138 // create the folder structure when it does not exist
142 copyFile( sourceArtifactFile, targetArtifactFile );
145 String fileName = artifactMetadata.getProject() + "-" + artifactMetadata.getVersion() + ".pom";
148 // TODO need to use path translator to get the pom file path
149 // String fileName = artifactMetadata.getProject() + "-" + artifactMetadata.getVersion() + ".pom";
151 // File sourcePomFile =
152 // pathTranslator.toFile( new File( sourceRepoPath ), artifactMetadata.getId(), artifactMetadata.getProject(),
153 // artifactMetadata.getVersion(), fileName );
155 // String relativePathToPomFile = sourcePomFile.getAbsolutePath().split( sourceRepoPath )[1];
156 // File targetPomFile = new File( targetRepoPath, relativePathToPomFile );
158 //pom file copying (file path is taken with out using path translator)
160 String index = artifactPath.substring( lastIndex + 1 );
161 int last = index.lastIndexOf( '.' );
162 File sourcePomFile = new File( sourceRepoPath, artifactPath.substring( 0, lastIndex ) + "/" +
163 artifactPath.substring( lastIndex + 1 ).substring( 0, last ) + ".pom" );
164 File targetPomFile = new File( targetRepoPath, artifactPath.substring( 0, lastIndex ) + "/" +
165 artifactPath.substring( lastIndex + 1 ).substring( 0, last ) + ".pom" );
167 if ( !targetPomFile.exists() && sourcePomFile.exists() )
169 copyFile( sourcePomFile, targetPomFile );
172 // explicitly update only if metadata-updater consumer is not enabled!
173 if ( !config.getRepositoryScanning().getKnownContentConsumers().contains( "metadata-updater" ) )
176 // updating version metadata files
177 File versionMetaDataFileInSourceRepo = pathTranslator.toFile( new File( sourceRepoPath ),
178 artifactMetadata.getNamespace(),
179 artifactMetadata.getProject(),
180 artifactMetadata.getVersion(),
183 if ( versionMetaDataFileInSourceRepo.exists() )
185 String relativePathToVersionMetadataFile = versionMetaDataFileInSourceRepo.getAbsolutePath().split(
187 File versionMetaDataFileInTargetRepo = new File( targetRepoPath, relativePathToVersionMetadataFile );
189 if ( !versionMetaDataFileInTargetRepo.exists() )
191 copyFile( versionMetaDataFileInSourceRepo, versionMetaDataFileInTargetRepo );
195 updateVersionMetadata( versionMetaDataFileInTargetRepo, artifactMetadata, lastUpdatedTimestamp );
200 // updating project meta data file
201 String projectDirectoryInSourceRepo = new File( versionMetaDataFileInSourceRepo.getParent() ).getParent();
202 File projectMetadataFileInSourceRepo = new File( projectDirectoryInSourceRepo, METADATA_FILENAME );
204 if ( projectMetadataFileInSourceRepo.exists() )
206 String relativePathToProjectMetadataFile = projectMetadataFileInSourceRepo.getAbsolutePath().split(
208 File projectMetadataFileInTargetRepo = new File( targetRepoPath, relativePathToProjectMetadataFile );
210 if ( !projectMetadataFileInTargetRepo.exists() )
213 copyFile( projectMetadataFileInSourceRepo, projectMetadataFileInSourceRepo );
217 updateProjectMetadata( projectMetadataFileInTargetRepo, artifactMetadata, lastUpdatedTimestamp,
225 private void copyFile( File sourceFile, File targetFile )
228 FileOutputStream out = new FileOutputStream( targetFile );
229 FileInputStream input = new FileInputStream( sourceFile );
231 // IOUtils internally buffers the streams
234 IOUtils.copy( input, out );
243 private void updateProjectMetadata( File projectMetaDataFileIntargetRepo, ArtifactMetadata artifactMetadata,
244 Date lastUpdatedTimestamp, String timestamp )
245 throws RepositoryMetadataException
247 ArrayList<String> availableVersions = new ArrayList<String>();
248 String latestVersion = artifactMetadata.getProjectVersion();
250 ArchivaRepositoryMetadata projectMetadata = getMetadata( projectMetaDataFileIntargetRepo );
252 if ( projectMetaDataFileIntargetRepo.exists() )
254 availableVersions = (ArrayList<String>) projectMetadata.getAvailableVersions();
256 Collections.sort( availableVersions, VersionComparator.getInstance() );
258 if ( !availableVersions.contains( artifactMetadata.getVersion() ) )
260 availableVersions.add( artifactMetadata.getVersion() );
263 latestVersion = availableVersions.get( availableVersions.size() - 1 );
267 availableVersions.add( artifactMetadata.getProjectVersion() );
268 projectMetadata.setGroupId( artifactMetadata.getNamespace() );
269 projectMetadata.setArtifactId( artifactMetadata.getProject() );
272 if ( projectMetadata.getGroupId() == null )
274 projectMetadata.setGroupId( artifactMetadata.getNamespace() );
277 if ( projectMetadata.getArtifactId() == null )
279 projectMetadata.setArtifactId( artifactMetadata.getProject() );
282 projectMetadata.setLatestVersion( latestVersion );
283 projectMetadata.setAvailableVersions( availableVersions );
284 projectMetadata.setLastUpdated( timestamp );
285 projectMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
287 if ( !VersionUtil.isSnapshot( artifactMetadata.getVersion() ) )
289 projectMetadata.setReleasedVersion( latestVersion );
292 RepositoryMetadataWriter.write( projectMetadata, projectMetaDataFileIntargetRepo );
296 private void updateVersionMetadata( File versionMetaDataFileInTargetRepo, ArtifactMetadata artifactMetadata,
297 Date lastUpdatedTimestamp )
298 throws RepositoryMetadataException
300 ArchivaRepositoryMetadata versionMetadata = getMetadata( versionMetaDataFileInTargetRepo );
301 if ( !versionMetaDataFileInTargetRepo.exists() )
303 versionMetadata.setGroupId( artifactMetadata.getNamespace() );
304 versionMetadata.setArtifactId( artifactMetadata.getProject() );
305 versionMetadata.setVersion( artifactMetadata.getProjectVersion() );
308 versionMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
309 RepositoryMetadataWriter.write( versionMetadata, versionMetaDataFileInTargetRepo );
312 private ArchivaRepositoryMetadata getMetadata( File metadataFile )
313 throws RepositoryMetadataException
315 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
316 if ( metadataFile.exists() )
318 metadata = RepositoryMetadataReader.read( metadataFile );
323 public List<ArtifactMetadata> getConflictingArtifacts( MetadataRepository metadataRepository, String sourceRepo,
327 List<ArtifactMetadata> targetArtifacts = metadataRepository.getArtifacts( targetRepo );
328 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepo );
329 List<ArtifactMetadata> conflictsArtifacts = new ArrayList<ArtifactMetadata>();
331 for ( ArtifactMetadata targetArtifact : targetArtifacts )
333 for ( ArtifactMetadata sourceArtifact : sourceArtifacts )
335 if ( isEquals( targetArtifact, sourceArtifact ) )
337 if ( !conflictsArtifacts.contains( sourceArtifact ) )
339 conflictsArtifacts.add( sourceArtifact );
345 sourceArtifacts.removeAll( conflictsArtifacts );
346 Filter<ArtifactMetadata> artifactsWithOutConflicts = new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
347 // merge( sourceRepo, targetRepo, artifactsWithOutConflicts );
348 return conflictsArtifacts;
351 private boolean isEquals( ArtifactMetadata sourceArtifact, ArtifactMetadata targetArtifact )
353 boolean isSame = false;
355 if ( ( sourceArtifact.getNamespace().equals( targetArtifact.getNamespace() ) ) &&
356 ( sourceArtifact.getProject().equals( targetArtifact.getProject() ) ) && ( sourceArtifact.getId().equals(
357 targetArtifact.getId() ) ) && ( sourceArtifact.getProjectVersion().equals(
358 targetArtifact.getProjectVersion() ) ) )