View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.internal.impl;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.nio.file.Path;
26  
27  import org.apache.maven.api.LocalRepository;
28  import org.apache.maven.api.RemoteRepository;
29  import org.apache.maven.api.model.Repository;
30  import org.apache.maven.api.services.RepositoryFactory;
31  import org.eclipse.aether.RepositorySystem;
32  import org.eclipse.aether.repository.RepositoryPolicy;
33  
34  @Named
35  @Singleton
36  public class DefaultRepositoryFactory implements RepositoryFactory {
37  
38      private final RepositorySystem repositorySystem;
39  
40      @Inject
41      public DefaultRepositoryFactory(RepositorySystem repositorySystem) {
42          this.repositorySystem = repositorySystem;
43      }
44  
45      @Override
46      public LocalRepository createLocal(Path path) {
47          return new DefaultLocalRepository(new org.eclipse.aether.repository.LocalRepository(path.toFile()));
48      }
49  
50      @Override
51      public RemoteRepository createRemote(String id, String url) {
52          return new DefaultRemoteRepository(
53                  new org.eclipse.aether.repository.RemoteRepository.Builder(id, "default", url).build());
54      }
55  
56      @Override
57      public RemoteRepository createRemote(Repository repository) throws IllegalArgumentException {
58          return new DefaultRemoteRepository(new org.eclipse.aether.repository.RemoteRepository.Builder(
59                          repository.getId(), repository.getLayout(), repository.getUrl())
60                  .setReleasePolicy(buildRepositoryPolicy(repository.getReleases()))
61                  .setSnapshotPolicy(buildRepositoryPolicy(repository.getSnapshots()))
62                  .build());
63      }
64  
65      public static org.eclipse.aether.repository.RepositoryPolicy buildRepositoryPolicy(
66              org.apache.maven.api.model.RepositoryPolicy policy) {
67          boolean enabled = true;
68          String updatePolicy = RepositoryPolicy.UPDATE_POLICY_DAILY;
69          String checksumPolicy = RepositoryPolicy.CHECKSUM_POLICY_FAIL;
70          if (policy != null) {
71              enabled = policy.isEnabled();
72              if (policy.getUpdatePolicy() != null) {
73                  updatePolicy = policy.getUpdatePolicy();
74              }
75              if (policy.getChecksumPolicy() != null) {
76                  checksumPolicy = policy.getChecksumPolicy();
77              }
78          }
79          return new org.eclipse.aether.repository.RepositoryPolicy(enabled, updatePolicy, checksumPolicy);
80      }
81  }