View Javadoc

1   package org.apache.maven.project;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.InvalidRepositoryException;
23  import org.apache.maven.artifact.repository.ArtifactRepository;
24  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
25  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
26  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
27  import org.apache.maven.model.DeploymentRepository;
28  import org.apache.maven.model.Repository;
29  import org.apache.maven.model.RepositoryBase;
30  import org.apache.maven.model.RepositoryPolicy;
31  import org.codehaus.plexus.PlexusContainer;
32  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33  
34  import java.util.ArrayList;
35  import java.util.Iterator;
36  import java.util.List;
37  
38  public final class ProjectUtils
39  {
40      private ProjectUtils()
41      {
42      }
43  
44      public static List buildArtifactRepositories( List repositories,
45                                                    ArtifactRepositoryFactory artifactRepositoryFactory,
46                                                    PlexusContainer container )
47          throws InvalidRepositoryException
48      {
49  
50          List repos = new ArrayList();
51  
52          for ( Iterator i = repositories.iterator(); i.hasNext(); )
53          {
54              Repository mavenRepo = (Repository) i.next();
55  
56              ArtifactRepository artifactRepo =
57                  buildArtifactRepository( mavenRepo, artifactRepositoryFactory, container );
58  
59              if ( !repos.contains( artifactRepo ) )
60              {
61                  repos.add( artifactRepo );
62              }
63          }
64          return repos;
65      }
66  
67      public static ArtifactRepository buildDeploymentArtifactRepository( DeploymentRepository repo,
68                                                                          ArtifactRepositoryFactory artifactRepositoryFactory,
69                                                                          PlexusContainer container )
70          throws InvalidRepositoryException
71      {
72          if ( repo != null )
73          {
74              String id = repo.getId();
75              String url = repo.getUrl();
76  
77              // TODO: make this a map inside the factory instead, so no lookup needed
78              ArtifactRepositoryLayout layout = getRepositoryLayout( repo, container );
79  
80              return artifactRepositoryFactory.createDeploymentArtifactRepository( id, url, layout,
81                                                                                   repo.isUniqueVersion() );
82          }
83          else
84          {
85              return null;
86          }
87      }
88  
89      public static ArtifactRepository buildArtifactRepository( Repository repo,
90                                                                ArtifactRepositoryFactory artifactRepositoryFactory,
91                                                                PlexusContainer container )
92          throws InvalidRepositoryException
93      {
94          if ( repo != null )
95          {
96              String id = repo.getId();
97              String url = repo.getUrl();
98  
99              if ( id == null || id.trim().length() < 1 )
100             {
101                 throw new InvalidRepositoryException( "Repository ID must not be empty (URL is: " + url + ").", new IllegalArgumentException( "repository.id" ) );
102             }
103 
104             if ( url == null || url.trim().length() < 1 )
105             {
106                 throw new InvalidRepositoryException( "Repository URL must not be empty (ID is: " + id + ").", new IllegalArgumentException( "repository.url" ) );
107             }
108 
109             // TODO: make this a map inside the factory instead, so no lookup needed
110             ArtifactRepositoryLayout layout = getRepositoryLayout( repo, container );
111 
112             ArtifactRepositoryPolicy snapshots = buildArtifactRepositoryPolicy( repo.getSnapshots() );
113             ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy( repo.getReleases() );
114 
115             return artifactRepositoryFactory.createArtifactRepository( id, url, layout, snapshots, releases );
116         }
117         else
118         {
119             return null;
120         }
121     }
122 
123     private static ArtifactRepositoryPolicy buildArtifactRepositoryPolicy( RepositoryPolicy policy )
124     {
125         boolean enabled = true;
126         String updatePolicy = null;
127         String checksumPolicy = null;
128 
129         if ( policy != null )
130         {
131             enabled = policy.isEnabled();
132             if ( policy.getUpdatePolicy() != null )
133             {
134                 updatePolicy = policy.getUpdatePolicy();
135             }
136             if ( policy.getChecksumPolicy() != null )
137             {
138                 checksumPolicy = policy.getChecksumPolicy();
139             }
140         }
141 
142         return new ArtifactRepositoryPolicy( enabled, updatePolicy, checksumPolicy );
143     }
144 
145     private static ArtifactRepositoryLayout getRepositoryLayout( RepositoryBase mavenRepo, PlexusContainer container )
146         throws InvalidRepositoryException
147     {
148         String layout = mavenRepo.getLayout();
149 
150         ArtifactRepositoryLayout repositoryLayout;
151         try
152         {
153             repositoryLayout = (ArtifactRepositoryLayout) container.lookup( ArtifactRepositoryLayout.ROLE, layout );
154         }
155         catch ( ComponentLookupException e )
156         {
157             throw new InvalidRepositoryException( "Cannot find layout implementation corresponding to: \'" + layout +
158                 "\' for remote repository with id: \'" + mavenRepo.getId() + "\'.", e );
159         }
160         return repositoryLayout;
161     }
162 
163 }