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.repository.internal;
20  
21  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
22  import org.apache.maven.model.Repository;
23  import org.eclipse.aether.artifact.Artifact;
24  import org.eclipse.aether.artifact.DefaultArtifact;
25  import org.eclipse.aether.repository.RemoteRepository;
26  import org.eclipse.aether.repository.RepositoryPolicy;
27  
28  /**
29   * <strong>Warning:</strong> This is an internal utility class that is only public for technical reasons, it is not part
30   * of the public API. In particular, this class can be changed or deleted without prior notice.
31   *
32   * @author Benjamin Bentmann
33   */
34  public class ArtifactDescriptorUtils {
35  
36      public static Artifact toPomArtifact(Artifact artifact) {
37          Artifact pomArtifact = artifact;
38  
39          if (pomArtifact.getClassifier().length() > 0 || !"pom".equals(pomArtifact.getExtension())) {
40              pomArtifact =
41                      new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), "pom", artifact.getVersion());
42          }
43  
44          return pomArtifact;
45      }
46  
47      public static RemoteRepository toRemoteRepository(Repository repository) {
48          RemoteRepository.Builder builder =
49                  new RemoteRepository.Builder(repository.getId(), repository.getLayout(), repository.getUrl());
50          builder.setSnapshotPolicy(toRepositoryPolicy(repository.getSnapshots()));
51          builder.setReleasePolicy(toRepositoryPolicy(repository.getReleases()));
52          return builder.build();
53      }
54  
55      public static RepositoryPolicy toRepositoryPolicy(org.apache.maven.model.RepositoryPolicy policy) {
56          boolean enabled = true;
57          String checksums = toRepositoryChecksumPolicy(ArtifactRepositoryPolicy.DEFAULT_CHECKSUM_POLICY);
58          String updates = RepositoryPolicy.UPDATE_POLICY_DAILY;
59  
60          if (policy != null) {
61              enabled = policy.isEnabled();
62              if (policy.getUpdatePolicy() != null) {
63                  updates = policy.getUpdatePolicy();
64              }
65              if (policy.getChecksumPolicy() != null) {
66                  checksums = policy.getChecksumPolicy();
67              }
68          }
69  
70          return new RepositoryPolicy(enabled, updates, checksums);
71      }
72  
73      public static String toRepositoryChecksumPolicy(final String artifactRepositoryPolicy) {
74          switch (artifactRepositoryPolicy) {
75              case ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL:
76                  return RepositoryPolicy.CHECKSUM_POLICY_FAIL;
77              case ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE:
78                  return RepositoryPolicy.CHECKSUM_POLICY_IGNORE;
79              case ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN:
80                  return RepositoryPolicy.CHECKSUM_POLICY_WARN;
81              default:
82                  throw new IllegalArgumentException("unknown repository checksum policy: " + artifactRepositoryPolicy);
83          }
84      }
85  }