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.filter.Filter;
24 import org.apache.archiva.metadata.repository.filter.IncludesFilter;
25 import org.apache.archiva.metadata.repository.MetadataRepository;
26 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
27 import org.apache.maven.archiva.repository.RepositoryException;
28 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
29 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataWriter;
30 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
31 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
32 import org.apache.maven.archiva.configuration.Configuration;
33 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
35 import org.apache.maven.archiva.common.utils.VersionComparator;
36 import org.apache.maven.archiva.common.utils.VersionUtil;
38 import java.util.List;
39 import java.util.Date;
40 import java.util.Calendar;
41 import java.util.TimeZone;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.io.IOException;
46 import java.io.FileOutputStream;
47 import java.io.FileInputStream;
48 import java.text.DateFormat;
49 import java.text.SimpleDateFormat;
52 * @plexus.component role="org.apache.archiva.stagerepository.merge.RepositoryMerger" role-hint="maven2"
54 public class Maven2RepositoryMerger
55 implements RepositoryMerger
59 * @plexus.requirement role-hint="default"
61 private MetadataRepository metadataRepository;
64 * @plexus.requirement role-hint="default"
66 private ArchivaConfiguration configuration;
69 * @plexus.requirement role-hint="maven2"
71 private RepositoryPathTranslator pathTranslator;
73 private static final String METADATA_FILENAME = "maven-metadata.xml";
75 public void setConfiguration( ArchivaConfiguration configuration )
77 this.configuration = configuration;
80 public void setMetadataRepository( MetadataRepository metadataRepository )
82 this.metadataRepository = metadataRepository;
85 public void merge( String sourceRepoId, String targetRepoId )
89 List<ArtifactMetadata> artifactsInSourceRepo = metadataRepository.getArtifacts( sourceRepoId );
90 for ( ArtifactMetadata artifactMetadata : artifactsInSourceRepo )
92 artifactMetadata.setRepositoryId( targetRepoId );
93 createFolderStructure( sourceRepoId, targetRepoId, artifactMetadata );
97 // TODO when UI needs a subset to merge
98 public void merge( String sourceRepoId, String targetRepoId, Filter<ArtifactMetadata> filter )
101 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepoId );
102 for ( ArtifactMetadata metadata : sourceArtifacts )
104 if ( filter.accept( metadata ) )
106 createFolderStructure( sourceRepoId, targetRepoId, metadata );
111 private void createFolderStructure( String sourceRepoId, String targetRepoId, ArtifactMetadata artifactMetadata )
112 throws IOException, RepositoryException
114 Configuration config = configuration.getConfiguration();
116 ManagedRepositoryConfiguration targetRepoConfig = config.findManagedRepositoryById( targetRepoId );
118 ManagedRepositoryConfiguration sourceRepoConfig = config.findManagedRepositoryById( sourceRepoId );
120 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
122 TimeZone timezone = TimeZone.getTimeZone( "UTC" );
124 DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
126 fmt.setTimeZone( timezone );
128 String timestamp = fmt.format( lastUpdatedTimestamp );
130 String targetRepoPath = targetRepoConfig.getLocation();
132 String sourceRepoPath = sourceRepoConfig.getLocation();
134 String artifactPath = pathTranslator.toPath( artifactMetadata.getNamespace(), artifactMetadata.getProject(),
135 artifactMetadata.getProjectVersion(), artifactMetadata.getId() );
137 File sourceArtifactFile = new File( sourceRepoPath, artifactPath );
139 File targetArtifactFile = new File( targetRepoPath, artifactPath );
141 int lastIndex = artifactPath.lastIndexOf( '/' );
143 File targetFile = new File( targetRepoPath, artifactPath.substring( 0, lastIndex ) );
145 if ( !targetFile.exists() )
147 // create the folder structure when it does not exist
151 copyFile( sourceArtifactFile, targetArtifactFile );
154 String fileName = artifactMetadata.getProject() + "-" + artifactMetadata.getVersion() + ".pom";
157 // TODO need to use path translator to get the pom file path
158 // String fileName = artifactMetadata.getProject() + "-" + artifactMetadata.getVersion() + ".pom";
160 // File sourcePomFile =
161 // pathTranslator.toFile( new File( sourceRepoPath ), artifactMetadata.getId(), artifactMetadata.getProject(),
162 // artifactMetadata.getVersion(), fileName );
164 // String relativePathToPomFile = sourcePomFile.getAbsolutePath().split( sourceRepoPath )[1];
165 // File targetPomFile = new File( targetRepoPath, relativePathToPomFile );
167 //pom file copying (file path is taken with out using path translator)
169 String index = artifactPath.substring( lastIndex + 1 );
170 int last = index.lastIndexOf( '.' );
171 File sourcePomFile = new File( sourceRepoPath, artifactPath.substring( 0, lastIndex ) + "/" +
172 artifactPath.substring( lastIndex + 1 ).substring( 0, last ) + ".pom" );
173 File targetPomFile = new File( targetRepoPath, artifactPath.substring( 0, lastIndex ) + "/" +
174 artifactPath.substring( lastIndex + 1 ).substring( 0, last ) + ".pom" );
176 if ( !targetPomFile.exists() && sourcePomFile.exists() )
178 copyFile( sourcePomFile, targetPomFile );
181 // explicitly update only if metadata-updater consumer is not enabled!
182 if ( !config.getRepositoryScanning().getKnownContentConsumers().contains( "metadata-updater" ) )
185 // updating version metadata files
186 File versionMetaDataFileInSourceRepo =
187 pathTranslator.toFile( new File( sourceRepoPath ), artifactMetadata.getNamespace(),
188 artifactMetadata.getProject(), artifactMetadata.getVersion(),
191 if( versionMetaDataFileInSourceRepo.exists() )
193 String relativePathToVersionMetadataFile =
194 versionMetaDataFileInSourceRepo.getAbsolutePath().split( sourceRepoPath )[1];
195 File versionMetaDataFileInTargetRepo = new File( targetRepoPath, relativePathToVersionMetadataFile );
197 if ( !versionMetaDataFileInTargetRepo.exists() )
199 copyFile( versionMetaDataFileInSourceRepo, versionMetaDataFileInTargetRepo );
203 updateVersionMetadata( versionMetaDataFileInTargetRepo, artifactMetadata, lastUpdatedTimestamp );
208 // updating project meta data file
209 String projectDirectoryInSourceRepo = new File( versionMetaDataFileInSourceRepo.getParent() ).getParent();
210 File projectMetadataFileInSourceRepo = new File( projectDirectoryInSourceRepo, METADATA_FILENAME );
212 if( projectMetadataFileInSourceRepo.exists() )
214 String relativePathToProjectMetadataFile =
215 projectMetadataFileInSourceRepo.getAbsolutePath().split( sourceRepoPath )[1];
216 File projectMetadataFileInTargetRepo = new File( targetRepoPath, relativePathToProjectMetadataFile );
218 if ( !projectMetadataFileInTargetRepo.exists() )
221 copyFile( projectMetadataFileInSourceRepo, projectMetadataFileInSourceRepo );
225 updateProjectMetadata( projectMetadataFileInTargetRepo, artifactMetadata, lastUpdatedTimestamp,
233 private void copyFile( File sourceFile, File targetFile )
236 FileOutputStream out = new FileOutputStream( targetFile );
237 FileInputStream input = new FileInputStream( sourceFile );
242 while ( ( i = input.read() ) != -1 )
255 private void updateProjectMetadata( File projectMetaDataFileIntargetRepo, ArtifactMetadata artifactMetadata,
256 Date lastUpdatedTimestamp, String timestamp )
257 throws RepositoryMetadataException
259 ArrayList<String> availableVersions = new ArrayList<String>();
260 String latestVersion = artifactMetadata.getProjectVersion();
262 ArchivaRepositoryMetadata projectMetadata = getMetadata( projectMetaDataFileIntargetRepo );
264 if ( projectMetaDataFileIntargetRepo.exists() )
266 availableVersions = (ArrayList<String>) projectMetadata.getAvailableVersions();
268 Collections.sort( availableVersions, VersionComparator.getInstance() );
270 if ( !availableVersions.contains( artifactMetadata.getVersion() ) )
272 availableVersions.add( artifactMetadata.getVersion() );
275 latestVersion = availableVersions.get( availableVersions.size() - 1 );
279 availableVersions.add( artifactMetadata.getProjectVersion() );
280 projectMetadata.setGroupId( artifactMetadata.getNamespace() );
281 projectMetadata.setArtifactId( artifactMetadata.getProject() );
284 if ( projectMetadata.getGroupId() == null )
286 projectMetadata.setGroupId( artifactMetadata.getNamespace() );
289 if ( projectMetadata.getArtifactId() == null )
291 projectMetadata.setArtifactId( artifactMetadata.getProject() );
294 projectMetadata.setLatestVersion( latestVersion );
295 projectMetadata.setAvailableVersions( availableVersions );
296 projectMetadata.setLastUpdated( timestamp );
297 projectMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
299 if ( !VersionUtil.isSnapshot( artifactMetadata.getVersion() ) )
301 projectMetadata.setReleasedVersion( latestVersion );
304 RepositoryMetadataWriter.write( projectMetadata, projectMetaDataFileIntargetRepo );
308 private void updateVersionMetadata( File versionMetaDataFileInTargetRepo, ArtifactMetadata artifactMetadata,
309 Date lastUpdatedTimestamp )
310 throws RepositoryMetadataException
312 ArchivaRepositoryMetadata versionMetadata = getMetadata( versionMetaDataFileInTargetRepo );
313 if ( !versionMetaDataFileInTargetRepo.exists() )
315 versionMetadata.setGroupId( artifactMetadata.getNamespace() );
316 versionMetadata.setArtifactId( artifactMetadata.getProject() );
317 versionMetadata.setVersion( artifactMetadata.getProjectVersion() );
320 versionMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
321 RepositoryMetadataWriter.write( versionMetadata, versionMetaDataFileInTargetRepo );
324 private ArchivaRepositoryMetadata getMetadata( File metadataFile )
325 throws RepositoryMetadataException
327 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
328 if ( metadataFile.exists() )
330 metadata = RepositoryMetadataReader.read( metadataFile );
335 public List<ArtifactMetadata> getConflictingArtifacts( String sourceRepo, String targetRepo )
338 List<ArtifactMetadata> targetArtifacts = metadataRepository.getArtifacts( targetRepo );
339 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepo );
340 List<ArtifactMetadata> conflictsArtifacts = new ArrayList<ArtifactMetadata>();
342 for ( ArtifactMetadata targerArtifact : targetArtifacts )
344 for ( ArtifactMetadata sourceArtifact : sourceArtifacts )
346 if ( isEquals( targerArtifact, sourceArtifact ) )
348 if ( !conflictsArtifacts.contains( sourceArtifact ) )
350 conflictsArtifacts.add( sourceArtifact );
356 sourceArtifacts.removeAll( conflictsArtifacts );
357 Filter<ArtifactMetadata> artifactsWithOutConflicts = new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
358 // merge( sourceRepo, targetRepo, artifactsWithOutConflicts );
359 return conflictsArtifacts;
362 private boolean isEquals( ArtifactMetadata sourceArtifact, ArtifactMetadata targetArtifact )
364 boolean isSame = false;
366 if ( ( sourceArtifact.getNamespace().equals( targetArtifact.getNamespace() ) ) &&
367 ( sourceArtifact.getProject().equals( targetArtifact.getProject() ) ) &&
368 ( sourceArtifact.getId().equals( targetArtifact.getId() ) ) &&
369 ( sourceArtifact.getProjectVersion().equals( targetArtifact.getProjectVersion() ) ) )