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.artifact.Artifact;
25  import org.apache.maven.artifact.handler.ArtifactHandler;
26  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
27  import org.apache.maven.plugin.logging.Log;
28  import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
29  import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
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 artifactHanderManager {@link ArtifactHandlerManager}.
43       * @param theClassifier The classifier to use.
44       * @param theType The type.
45       */
46      public ClassifierTypeTranslator(
47              ArtifactHandlerManager artifactHanderManager, String theClassifier, String theType) {
48          this.artifactHandlerManager = artifactHanderManager;
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<ArtifactCoordinate> translate(Set<Artifact> artifacts, Log log) {
60          Set<ArtifactCoordinate> 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              DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
92              coordinate.setGroupId(artifact.getGroupId());
93              coordinate.setArtifactId(artifact.getArtifactId());
94              coordinate.setVersion(artifact.getVersion());
95              coordinate.setClassifier(useClassifier);
96              coordinate.setExtension(extension);
97  
98              // // Create a new artifact
99              // Artifact newArtifact = factory.createArtifactWithClassifier( artifact.getGroupId(), artifact
100             // .getArtifactId(), artifact.getVersion(), useType, useClassifier );
101             //
102             // // note the new artifacts will always have the scope set to null. We
103             // // should
104             // // reset it here so that it will pass other filters if needed
105             // newArtifact.setScope( artifact.getScope() );
106             //
107             // if ( Artifact.SCOPE_SYSTEM.equals( newArtifact.getScope() ) )
108             // {
109             // File baseDir = repositoryManager.getLocalRepositoryBasedir( buildingRequest );
110             // String path = repositoryManager.getPathForLocalArtifact( buildingRequest, newArtifact );
111             // newArtifact.setFile( new File( baseDir, path ) );
112             // }
113 
114             results.add(coordinate);
115         }
116 
117         return results;
118     }
119 
120     /**
121      * @return Returns the type.
122      */
123     public String getType() {
124         return this.type;
125     }
126 
127     /**
128      * @param theType The type to set.
129      */
130     public void setType(String theType) {
131         this.type = theType;
132     }
133 
134     /**
135      * @return Returns the classifier.
136      */
137     public String getClassifier() {
138         return this.classifier;
139     }
140 
141     /**
142      * @param theClassifier The classifier to set.
143      */
144     public void setClassifier(String theClassifier) {
145         this.classifier = theClassifier;
146     }
147 }