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.RepositoryContentFactory;
28 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
29 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
30 import org.apache.maven.archiva.repository.RepositoryException;
31 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
32 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataWriter;
33 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
34 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
35 import org.apache.maven.archiva.configuration.Configuration;
36 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
37 import org.apache.maven.archiva.model.ArchivaArtifact;
38 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
39 import org.apache.maven.archiva.common.utils.VersionComparator;
40 import org.apache.maven.archiva.common.utils.VersionUtil;
42 import java.util.List;
43 import java.util.Date;
44 import java.util.Calendar;
45 import java.util.TimeZone;
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.io.IOException;
50 import java.io.FileOutputStream;
51 import java.io.FileInputStream;
52 import java.text.DateFormat;
53 import java.text.SimpleDateFormat;
56 * @plexus.component role="org.apache.archiva.stagerepository.merge.RepositoryMerger" role-hint="maven2"
58 public class Maven2RepositoryMerger
59 implements RepositoryMerger
63 * @plexus.requirement role-hint="default"
65 private MetadataRepository metadataRepository;
68 * @plexus.requirement role-hint="default"
70 private ArchivaConfiguration configuration;
73 * @plexus.requirement role-hint="maven2"
75 private RepositoryPathTranslator pathTranslator;
77 private static final String METADATA_FILENAME = "maven-metadata.xml";
79 public void setConfiguration( ArchivaConfiguration configuration )
81 this.configuration = configuration;
84 public void setMetadataRepository( MetadataRepository metadataRepository )
86 this.metadataRepository = metadataRepository;
89 public void merge( String sourceRepoId, String targetRepoId )
93 List<ArtifactMetadata> artifactsInSourceRepo = metadataRepository.getArtifacts( sourceRepoId );
94 for ( ArtifactMetadata artifactMetadata : artifactsInSourceRepo )
96 createFolderStructure( sourceRepoId, targetRepoId, artifactMetadata );
100 // TODO when UI needs a subset to merge
101 public void merge( String sourceRepoId, String targetRepoId, Filter<ArtifactMetadata> filter )
106 private void createFolderStructure( String sourceRepoId, String targetRepoId, ArtifactMetadata artifactMetadata )
107 throws IOException, RepositoryException
109 Configuration config = configuration.getConfiguration();
111 ManagedRepositoryConfiguration targetRepoConfig = config.findManagedRepositoryById( targetRepoId );
113 ManagedRepositoryConfiguration sourceRepoConfig = config.findManagedRepositoryById( sourceRepoId );
115 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
117 TimeZone timezone = TimeZone.getTimeZone( "UTC" );
119 DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
121 fmt.setTimeZone( timezone );
123 String timestamp = fmt.format( lastUpdatedTimestamp );
125 String targetRepoPath = targetRepoConfig.getLocation();
127 String sourceRepoPath = sourceRepoConfig.getLocation();
129 String artifactPath =
130 pathTranslator.toPath( artifactMetadata.getNamespace(), artifactMetadata.getProject(),
131 artifactMetadata.getProjectVersion(), artifactMetadata.getId() );
133 File sourceArtifactFile = new File( sourceRepoPath, artifactPath );
135 File targetArtifactFile = new File( targetRepoPath, artifactPath );
137 int lastIndex = artifactPath.lastIndexOf( '/' );
139 File targetFile = new File( targetRepoPath, artifactPath.substring( 0, lastIndex ) );
141 if ( !targetFile.exists() )
143 // create the folder structure when it does not exist
147 copyFile( sourceArtifactFile, targetArtifactFile );
150 String fileName = artifactMetadata.getProject() + "-" + artifactMetadata.getVersion() + ".pom";
153 // TODO need to use path translator to get the pom file path
154 // String fileName = artifactMetadata.getProject() + "-" + artifactMetadata.getVersion() + ".pom";
156 // File sourcePomFile =
157 // pathTranslator.toFile( new File( sourceRepoPath ), artifactMetadata.getId(), artifactMetadata.getProject(),
158 // artifactMetadata.getVersion(), fileName );
160 // String relativePathToPomFile = sourcePomFile.getAbsolutePath().split( sourceRepoPath )[1];
161 // File targetPomFile = new File( targetRepoPath, relativePathToPomFile );
163 //pom file copying (file path is taken with out using path translator)
165 String index = artifactPath.substring( lastIndex + 1 );
166 int last = index.lastIndexOf( '.' );
167 File sourcePomFile = new File( sourceRepoPath, artifactPath.substring( 0, lastIndex ) + "/" +
168 artifactPath.substring( lastIndex + 1 ).substring( 0, last ) + ".pom" );
169 File targetPomFile = new File( targetRepoPath, artifactPath.substring( 0, lastIndex ) + "/" +
170 artifactPath.substring( lastIndex + 1 ).substring( 0, last ) + ".pom" );
172 if ( !targetPomFile.exists() )
174 copyFile( sourcePomFile, targetPomFile );
177 // explicitly update only if metadata-updater consumer is not enabled!
178 if ( !config.getRepositoryScanning().getKnownContentConsumers().contains( "metadata-updater" ) )
181 // updating version metadata files
182 File versionMetaDataFileInSourceRepo =
183 pathTranslator.toFile( new File( sourceRepoPath ), artifactMetadata.getNamespace(),
184 artifactMetadata.getProject(), artifactMetadata.getVersion(), METADATA_FILENAME );
185 String relativePathToVersionMetadataFile =
186 versionMetaDataFileInSourceRepo.getAbsolutePath().split( sourceRepoPath )[1];
187 File versionMetaDataFileInTargetRepo = new File( targetRepoPath, relativePathToVersionMetadataFile );
189 if ( !versionMetaDataFileInTargetRepo.exists() )
191 copyFile( versionMetaDataFileInSourceRepo, versionMetaDataFileInTargetRepo );
195 updateVersionMetadata( versionMetaDataFileInTargetRepo, artifactMetadata, lastUpdatedTimestamp );
199 // updating project meta data file
200 String projectDirectoryInSourceRepo = new File( versionMetaDataFileInSourceRepo.getParent() ).getParent();
201 File projectMetadataFileInSourceRepo = new File( projectDirectoryInSourceRepo, METADATA_FILENAME );
203 String relativePathToProjectMetadataFile =
204 projectMetadataFileInSourceRepo.getAbsolutePath().split( sourceRepoPath )[1];
205 File projectMetadataFileInTargetRepo = new File( targetRepoPath, relativePathToProjectMetadataFile );
207 if ( !projectMetadataFileInTargetRepo.exists() )
210 copyFile( versionMetaDataFileInSourceRepo, projectMetadataFileInSourceRepo );
214 updateProjectMetadata( projectMetadataFileInTargetRepo, artifactMetadata, lastUpdatedTimestamp,
221 private void copyFile( File sourceFile, File targetFile )
225 FileOutputStream out = new FileOutputStream( targetFile );
226 FileInputStream input = new FileInputStream( sourceFile );
231 while ( ( i = input.read() ) != -1 )
244 private void updateProjectMetadata( File projectMetaDataFileIntargetRepo, ArtifactMetadata artifactMetadata,
245 Date lastUpdatedTimestamp, String timestamp )
246 throws RepositoryMetadataException
248 ArrayList<String> availableVersions = new ArrayList<String>();
249 String latestVersion = artifactMetadata.getProjectVersion();
251 ArchivaRepositoryMetadata projectMetadata = getMetadata( projectMetaDataFileIntargetRepo );
253 if ( projectMetaDataFileIntargetRepo.exists() )
255 availableVersions = (ArrayList<String>) projectMetadata.getAvailableVersions();
257 Collections.sort( availableVersions, VersionComparator.getInstance() );
259 if ( !availableVersions.contains( artifactMetadata.getVersion() ) )
261 availableVersions.add( artifactMetadata.getVersion() );
264 latestVersion = availableVersions.get( availableVersions.size() - 1 );
268 availableVersions.add( artifactMetadata.getProjectVersion() );
269 projectMetadata.setGroupId( artifactMetadata.getNamespace() );
270 projectMetadata.setArtifactId( artifactMetadata.getProject() );
273 if ( projectMetadata.getGroupId() == null )
275 projectMetadata.setGroupId( artifactMetadata.getNamespace() );
278 if ( projectMetadata.getArtifactId() == null )
280 projectMetadata.setArtifactId( artifactMetadata.getProject() );
283 projectMetadata.setLatestVersion( latestVersion );
284 projectMetadata.setAvailableVersions( availableVersions );
285 projectMetadata.setLastUpdated( timestamp );
286 projectMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
288 if ( !VersionUtil.isSnapshot( artifactMetadata.getVersion() ) )
290 projectMetadata.setReleasedVersion( latestVersion );
293 RepositoryMetadataWriter.write( projectMetadata, projectMetaDataFileIntargetRepo );
297 private void updateVersionMetadata( File versionMetaDataFileInTargetRepo, ArtifactMetadata artifactMetadata,
298 Date lastUpdatedTimestamp )
299 throws RepositoryMetadataException
301 ArchivaRepositoryMetadata versionMetadata = getMetadata( versionMetaDataFileInTargetRepo );
302 if ( !versionMetaDataFileInTargetRepo.exists() )
304 versionMetadata.setGroupId( artifactMetadata.getNamespace() );
305 versionMetadata.setArtifactId( artifactMetadata.getProject() );
306 versionMetadata.setVersion( artifactMetadata.getProjectVersion() );
309 versionMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
310 RepositoryMetadataWriter.write( versionMetadata, versionMetaDataFileInTargetRepo );
313 private ArchivaRepositoryMetadata getMetadata( File metadataFile )
314 throws RepositoryMetadataException
316 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
317 if ( metadataFile.exists() )
319 metadata = RepositoryMetadataReader.read( metadataFile );