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