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