1 package org.apache.maven.archiva.repository.assembly;
4 * Copyright 2001-2005 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
21 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
22 import org.apache.maven.artifact.repository.ArtifactRepository;
23 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
24 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
25 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
26 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
27 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
28 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
29 import org.apache.maven.artifact.resolver.ArtifactResolver;
30 import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
31 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
32 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
33 import org.codehaus.plexus.logging.AbstractLogEnabled;
34 import org.codehaus.plexus.util.FileUtils;
35 import org.codehaus.plexus.util.IOUtil;
38 import java.io.FileWriter;
39 import java.io.IOException;
40 import java.io.Writer;
41 import java.lang.reflect.Field;
42 import java.security.NoSuchAlgorithmException;
43 import java.text.DateFormat;
44 import java.text.SimpleDateFormat;
45 import java.util.ArrayList;
46 import java.util.Date;
47 import java.util.HashMap;
48 import java.util.Iterator;
49 import java.util.List;
52 import java.util.TimeZone;
55 * @author Jason van Zyl
56 * @plexus.component role="org.apache.maven.archiva.RepositoryAssembler" role-hint="default"
59 // todo will need to pop the processed project cache using reflection
60 public class DefaultRepositoryAssembler
61 extends AbstractLogEnabled
62 implements RepositoryAssembler
64 protected static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
66 protected static final String UTC_TIMESTAMP_PATTERN = "yyyyMMddHHmmss";
71 protected ArtifactFactory artifactFactory;
76 protected ArtifactResolver artifactResolver;
81 protected ArtifactRepositoryLayout repositoryLayout;
86 protected ArtifactRepositoryFactory artifactRepositoryFactory;
91 protected ArtifactMetadataSource metadataSource;
93 public void assemble( Set artifacts,
95 Set remoteRepositories,
96 ArtifactRepositoryLayout layout,
97 File repositoryDirectory )
98 throws RepositoryAssemblyException
103 public void assemble( Set artifacts,
104 File localRepository,
105 Set remoteRepositories,
106 ArtifactFilter artifactFilter,
107 ArtifactRepositoryLayout layout,
108 File repositoryDirectory )
109 throws RepositoryAssemblyException
111 ArtifactRepository targetRepository = createLocalRepository( repositoryDirectory );
113 ArtifactResolutionResult result = null;
115 Artifact originatingArtifact = null;
117 ArtifactRepository localRepo = createLocalRepository( localRepository );
120 // i have to get everything first as a filter or transformation here
121 // doesn't seem to work
122 // to align everything. If I use a filter to change the version on
123 // the fly then I get the
124 // I get JARs but no POMs, and in some directories POMs with no
127 // FIXME I'm not getting runtime dependencies here
128 result = artifactResolver.resolveTransitively( artifacts, originatingArtifact,
129 new ArrayList( remoteRepositories ),
130 localRepo, metadataSource );
132 catch ( ArtifactResolutionException e )
134 throw new RepositoryAssemblyException( "Error resolving artifacts: " + e.getMessage(), e );
136 catch ( ArtifactNotFoundException e )
138 throw new RepositoryAssemblyException( "Error resolving artifacts: " + e.getMessage(), e );
141 assembleRepositoryArtifacts( result, artifactFilter, localRepo, targetRepository, repositoryDirectory );
143 ArtifactRepository centralRepository = findCentralRepository( project );
145 assembleRepositoryMetadata( result, artifactFilter, centralRepository, targetRepository );
148 private void assembleRepositoryArtifacts( ArtifactResolutionResult result,
149 ArtifactFilter filter,
150 List remoteRepositories,
151 ArtifactRepository localRepository,
152 ArtifactRepository targetRepository,
153 File repositoryDirectory )
154 throws RepositoryAssemblyException
158 // Now that we have the graph, let's try to align it to versions
159 // that we want and remove
160 // the assembly we previously populated.
161 FileUtils.deleteDirectory( repositoryDirectory );
163 FileUtils.mkdir( repositoryDirectory.getAbsolutePath() );
165 for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); )
167 Artifact a = (Artifact) i.next();
169 if ( filter.include( a ) )
171 // We need to flip it back to not being resolved so we can
172 // look for it again!
173 a.setResolved( false );
175 artifactResolver.resolve( a, remoteRepositories, localRepository );
177 File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( a ) );
178 FileUtils.copyFile( a.getFile(), targetFile );
180 writeChecksums( targetFile );
182 if ( !"pom".equals( a.getType() ) )
184 a = artifactFactory.createProjectArtifact( a.getGroupId(), a.getArtifactId(), a.getVersion() );
186 MavenProject p = projectBuilder.buildFromRepository( a, project.getRemoteArtifactRepositories(),
191 a = artifactFactory.createProjectArtifact( p.getGroupId(), p.getArtifactId(), p
194 setAlignment( a, groupVersionAlignments );
196 File sourceFile = new File( localRepository.getBasedir(), localRepository.pathOf( a ) );
198 if ( !sourceFile.exists() )
203 targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( a ) );
205 FileUtils.copyFile( sourceFile, targetFile );
207 writeChecksums( targetFile );
216 catch ( ArtifactResolutionException e )
218 throw new RepositoryAssemblyException( "Error resolving artifacts: " + e.getMessage(), e );
220 catch ( ArtifactNotFoundException e )
222 throw new RepositoryAssemblyException( "Error resolving artifacts: " + e.getMessage(), e );
224 catch ( IOException e )
226 throw new RepositoryAssemblyException( "Error writing artifact metdata.", e );
230 private void assembleRepositoryMetadata( ArtifactResolutionResult result,
231 ArtifactFilter filter,
232 ArtifactRepository centralRepository,
233 ArtifactRepository targetRepository )
234 throws RepositoryAssemblyException
236 for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); )
238 Artifact a = (Artifact) i.next();
240 if ( filter.include( a ) )
242 Versioning v = new Versioning();
244 v.setRelease( a.getVersion() );
246 v.setLatest( a.getVersion() );
248 v.addVersion( a.getVersion() );
250 v.setLastUpdated( getUtcDateFormatter().format( new Date() ) );
252 ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( a, v );
253 String path = targetRepository.pathOfLocalRepositoryMetadata( metadata, centralRepository );
254 File metadataFile = new File( targetRepository.getBasedir(), path );
256 MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer();
258 Writer writer = null;
261 writer = new FileWriter( metadataFile );
263 metadataWriter.write( writer, metadata.getMetadata() );
265 catch ( IOException e )
267 throw new RepositoryAssemblyException( "Error writing artifact metdata.", e );
271 IOUtil.close( writer );
276 writeChecksums( metadataFile );
278 File metadataFileRemote = new File( targetRepository.getBasedir(), targetRepository
279 .pathOfRemoteRepositoryMetadata( metadata ) );
281 FileUtils.copyFile( metadataFile, metadataFileRemote );
283 FileUtils.copyFile( new File( metadataFile.getParentFile(), metadataFile.getName() + ".sha1" ),
284 new File( metadataFileRemote.getParentFile(),
285 metadataFileRemote.getName() + ".sha1" ) );
287 FileUtils.copyFile( new File( metadataFile.getParentFile(), metadataFile.getName() + ".md5" ),
288 new File( metadataFileRemote.getParentFile(),
289 metadataFileRemote.getName() + ".md5" ) );
291 catch ( IOException e )
293 throw new RepositoryAssemblyException( "Error writing artifact metdata.", e );
299 private void writeChecksums( File file )
300 throws IOException, RepositoryAssemblyException
304 String md5 = DigestUtils.createChecksum( file, "MD5" );
305 String sha1 = DigestUtils.createChecksum( file, "SHA-1" );
307 FileUtils.fileWrite( new File( file.getParentFile(), file.getName() + ".md5" ).getAbsolutePath(), md5
309 FileUtils.fileWrite( new File( file.getParentFile(), file.getName() + ".sha1" ).getAbsolutePath(), sha1
312 catch ( NoSuchAlgorithmException e )
314 throw new RepositoryAssemblyException( "Unable to get write checksums: " + e.getMessage(), e );
318 protected static DateFormat getUtcDateFormatter()
320 DateFormat utcDateFormatter = new SimpleDateFormat( UTC_TIMESTAMP_PATTERN );
321 utcDateFormatter.setTimeZone( UTC_TIME_ZONE );
322 return utcDateFormatter;
325 protected ArtifactRepository createLocalRepository( File directory )
327 String localRepositoryUrl = directory.getAbsolutePath();
329 if ( !localRepositoryUrl.startsWith( "file:" ) )
331 localRepositoryUrl = "file://" + localRepositoryUrl;
334 return createRepository( "local", localRepositoryUrl, false, true,
335 ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );
338 public ArtifactRepository createRepository( String repositoryId,
339 String repositoryUrl,
341 boolean updateSnapshots,
342 String globalChecksumPolicy )
344 ArtifactRepository localRepository =
345 new DefaultArtifactRepository( repositoryId, repositoryUrl, repositoryLayout );
347 boolean snapshotPolicySet = false;
351 snapshotPolicySet = true;
354 if ( !snapshotPolicySet && updateSnapshots )
356 artifactRepositoryFactory.setGlobalUpdatePolicy( ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS );
359 artifactRepositoryFactory.setGlobalChecksumPolicy( globalChecksumPolicy );
361 return localRepository;