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.plugins.dependency.utils.translators;
20  
21  import java.util.LinkedHashSet;
22  import java.util.Set;
23  
24  import org.apache.maven.RepositoryUtils;
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.handler.ArtifactHandler;
27  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
28  import org.apache.maven.plugin.logging.Log;
29  import org.eclipse.aether.util.artifact.SubArtifact;
30  
31  /**
32   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
33   */
34  public class ClassifierTypeTranslator implements ArtifactTranslator {
35      private final ArtifactHandlerManager artifactHandlerManager;
36  
37      private String classifier;
38  
39      private String type;
40  
41      /**
42       * @param artifactHandlerManager {@link ArtifactHandlerManager}.
43       * @param theClassifier The classifier to use.
44       * @param theType The type.
45       */
46      public ClassifierTypeTranslator(
47              ArtifactHandlerManager artifactHandlerManager, String theClassifier, String theType) {
48          this.artifactHandlerManager = artifactHandlerManager;
49          this.classifier = theClassifier;
50          this.type = theType;
51      }
52  
53      /*
54       * (non-Javadoc)
55       * @see org.apache.mojo.dependency.utils.translators.ArtifactTranslator#translate(java.util.Set,
56       * org.apache.maven.plugin.logging.Log)
57       */
58      @Override
59      public Set<org.eclipse.aether.artifact.Artifact> translate(Set<Artifact> artifacts, Log log) {
60          Set<org.eclipse.aether.artifact.Artifact> results;
61  
62          log.debug("Translating Artifacts using Classifier: " + this.classifier + " and Type: " + this.type);
63          results = new LinkedHashSet<>();
64          for (Artifact artifact : artifacts) {
65              // this translator must pass both type and classifier here so we
66              // will use the
67              // base artifact value if null comes in
68              final String useType;
69              if (this.type != null && !this.type.isEmpty()) {
70                  useType = this.type;
71              } else {
72                  useType = artifact.getType();
73              }
74  
75              ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler(useType);
76  
77              final String extension;
78              if (artifactHandler != null) {
79                  extension = artifactHandler.getExtension();
80              } else {
81                  extension = this.type;
82              }
83  
84              String useClassifier;
85              if (this.classifier != null && !this.classifier.isEmpty()) {
86                  useClassifier = this.classifier;
87              } else {
88                  useClassifier = artifact.getClassifier();
89              }
90  
91              results.add(new SubArtifact(RepositoryUtils.toArtifact(artifact), useClassifier, extension));
92          }
93  
94          return results;
95      }
96  
97      /**
98       * @return Returns the type.
99       */
100     public String getType() {
101         return this.type;
102     }
103 
104     /**
105      * @param theType The type to set.
106      */
107     public void setType(String theType) {
108         this.type = theType;
109     }
110 
111     /**
112      * @return Returns the classifier.
113      */
114     public String getClassifier() {
115         return this.classifier;
116     }
117 
118     /**
119      * @param theClassifier The classifier to set.
120      */
121     public void setClassifier(String theClassifier) {
122         this.classifier = theClassifier;
123     }
124 }