View Javadoc
1   package org.apache.maven.resolver.internal.ant;
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 java.io.File;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.apache.maven.model.Dependency;
29  import org.apache.maven.model.Parent;
30  import org.apache.maven.model.Repository;
31  import org.apache.maven.model.building.FileModelSource;
32  import org.apache.maven.model.building.ModelSource;
33  import org.apache.maven.model.resolution.InvalidRepositoryException;
34  import org.apache.maven.model.resolution.ModelResolver;
35  import org.apache.maven.model.resolution.UnresolvableModelException;
36  import org.eclipse.aether.RepositorySystem;
37  import org.eclipse.aether.RepositorySystemSession;
38  import org.eclipse.aether.artifact.Artifact;
39  import org.eclipse.aether.artifact.DefaultArtifact;
40  import org.eclipse.aether.impl.RemoteRepositoryManager;
41  import org.eclipse.aether.repository.RemoteRepository;
42  import org.eclipse.aether.repository.RepositoryPolicy;
43  import org.eclipse.aether.resolution.ArtifactRequest;
44  import org.eclipse.aether.resolution.ArtifactResolutionException;
45  
46  /**
47   * A model resolver to assist building of dependency POMs. This resolver gives priority to those repositories that have
48   * been initially specified and repositories discovered in dependency POMs are recessively merged into the search chain.
49   *
50   */
51  class AntModelResolver
52      implements ModelResolver
53  {
54  
55      private final RepositorySystemSession session;
56  
57      private final String context;
58  
59      private List<org.eclipse.aether.repository.RemoteRepository> repositories;
60  
61      private final RepositorySystem repoSys;
62  
63      private final RemoteRepositoryManager remoteRepositoryManager;
64  
65      private final Set<String> repositoryIds;
66  
67      AntModelResolver( final RepositorySystemSession session, final String context, final RepositorySystem repoSys,
68                        final RemoteRepositoryManager remoteRepositoryManager, final List<RemoteRepository> repositories )
69      {
70          this.session = session;
71          this.context = context;
72          this.repoSys = repoSys;
73          this.remoteRepositoryManager = remoteRepositoryManager;
74          this.repositories = repositories;
75          this.repositoryIds = new HashSet<String>();
76      }
77  
78      private AntModelResolver( final AntModelResolver original )
79      {
80          this.session = original.session;
81          this.context = original.context;
82          this.repoSys = original.repoSys;
83          this.remoteRepositoryManager = original.remoteRepositoryManager;
84          this.repositories = original.repositories;
85          this.repositoryIds = new HashSet<String>( original.repositoryIds );
86      }
87  
88      public void addRepository( final Repository repository )
89          throws InvalidRepositoryException
90      {
91          if ( !repositoryIds.add( repository.getId() ) )
92          {
93              return;
94          }
95  
96          final List<RemoteRepository> newRepositories = Collections.singletonList( convert( repository ) );
97  
98          this.repositories =
99              remoteRepositoryManager.aggregateRepositories( session, repositories, newRepositories, true );
100     }
101 
102     static RemoteRepository convert( final Repository repository )
103     {
104         final RemoteRepository.Builder builder =
105             new RemoteRepository.Builder( repository.getId(), repository.getLayout(), repository.getUrl() );
106         builder.setSnapshotPolicy( convert( repository.getSnapshots() ) );
107         builder.setReleasePolicy( convert( repository.getReleases() ) );
108         return builder.build();
109     }
110 
111     private static RepositoryPolicy convert( final org.apache.maven.model.RepositoryPolicy policy )
112     {
113         boolean enabled = true;
114         String checksums = RepositoryPolicy.CHECKSUM_POLICY_WARN;
115         String updates = RepositoryPolicy.UPDATE_POLICY_DAILY;
116 
117         if ( policy != null )
118         {
119             enabled = policy.isEnabled();
120             if ( policy.getUpdatePolicy() != null )
121             {
122                 updates = policy.getUpdatePolicy();
123             }
124             if ( policy.getChecksumPolicy() != null )
125             {
126                 checksums = policy.getChecksumPolicy();
127             }
128         }
129 
130         return new RepositoryPolicy( enabled, updates, checksums );
131     }
132 
133     public ModelResolver newCopy()
134     {
135         return new AntModelResolver( this );
136     }
137 
138     public ModelSource resolveModel( final String groupId, final String artifactId, final String version )
139         throws UnresolvableModelException
140     {
141         Artifact pomArtifact = new DefaultArtifact( groupId, artifactId, "", "pom", version );
142 
143         try
144         {
145             final ArtifactRequest request = new ArtifactRequest( pomArtifact, repositories, context );
146             pomArtifact = repoSys.resolveArtifact( session, request ).getArtifact();
147         }
148         catch ( final ArtifactResolutionException e )
149         {
150             throw new UnresolvableModelException( "Failed to resolve POM for " + groupId + ":" + artifactId + ":"
151                 + version + " due to " + e.getMessage(), groupId, artifactId, version, e );
152         }
153 
154         final File pomFile = pomArtifact.getFile();
155 
156         return new FileModelSource( pomFile );
157     }
158 
159     @Override
160     public ModelSource resolveModel( final Parent parent )
161         throws UnresolvableModelException
162     {
163         return resolveModel( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
164     }
165 
166     @Override
167     public ModelSource resolveModel( final Dependency dependency )
168         throws UnresolvableModelException
169     {
170         return resolveModel( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() );
171     }
172 
173     @Override
174     public void addRepository( final Repository repository, final boolean replace )
175         throws InvalidRepositoryException
176     {
177         addRepository( repository );
178     }
179 
180 }