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