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