View Javadoc
1   package org.apache.maven.internal.impl;
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 javax.inject.Inject;
23  import javax.inject.Named;
24  import javax.inject.Singleton;
25  
26  import java.nio.file.Path;
27  
28  import org.apache.maven.api.LocalRepository;
29  import org.apache.maven.api.RemoteRepository;
30  import org.apache.maven.api.services.RepositoryFactory;
31  import org.apache.maven.api.model.Repository;
32  import org.eclipse.aether.RepositorySystem;
33  import org.eclipse.aether.repository.RepositoryPolicy;
34  
35  @Named
36  @Singleton
37  public class DefaultRepositoryFactory implements RepositoryFactory
38  {
39  
40      private final RepositorySystem repositorySystem;
41  
42      @Inject
43      public DefaultRepositoryFactory( RepositorySystem repositorySystem )
44      {
45          this.repositorySystem = repositorySystem;
46      }
47  
48      @Override
49      public LocalRepository createLocal( Path path )
50      {
51          return new DefaultLocalRepository( new org.eclipse.aether.repository.LocalRepository( path.toFile() ) );
52      }
53  
54      @Override
55      public RemoteRepository createRemote( String id, String url )
56      {
57          return new DefaultRemoteRepository(
58                  new org.eclipse.aether.repository.RemoteRepository.Builder( id, "default", url )
59                          .build() );
60      }
61  
62      @Override
63      public RemoteRepository createRemote( Repository repository )
64              throws IllegalArgumentException
65      {
66          return new DefaultRemoteRepository(
67                  new org.eclipse.aether.repository.RemoteRepository.Builder(
68                          repository.getId(), repository.getLayout(), repository.getUrl() )
69                          .setReleasePolicy( buildRepositoryPolicy( repository.getReleases() ) )
70                          .setSnapshotPolicy( buildRepositoryPolicy( repository.getSnapshots() ) )
71                          .build() );
72      }
73  
74      public static org.eclipse.aether.repository.RepositoryPolicy buildRepositoryPolicy(
75              org.apache.maven.api.model.RepositoryPolicy policy )
76      {
77          boolean enabled = true;
78          String updatePolicy = RepositoryPolicy.UPDATE_POLICY_DAILY;
79          String checksumPolicy = RepositoryPolicy.CHECKSUM_POLICY_FAIL;
80          if ( policy != null )
81          {
82              enabled = policy.isEnabled();
83              if ( policy.getUpdatePolicy() != null )
84              {
85                  updatePolicy = policy.getUpdatePolicy();
86              }
87              if ( policy.getChecksumPolicy() != null )
88              {
89                  checksumPolicy = policy.getChecksumPolicy();
90              }
91          }
92          return new org.eclipse.aether.repository.RepositoryPolicy(
93                  enabled, updatePolicy, checksumPolicy );
94      }
95  }