View Javadoc
1   package org.apache.maven.plugins.dependency.utils.translators;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.LinkedHashSet;
23  import java.util.Set;
24  
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.apache.maven.shared.artifact.ArtifactCoordinate;
30  import org.apache.maven.shared.artifact.DefaultArtifactCoordinate;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  /**
34   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
35   * @version $Id: ClassifierTypeTranslator.java 1807877 2017-09-09 10:35:59Z khmarbaise $
36   */
37  public class ClassifierTypeTranslator
38      implements ArtifactTranslator
39  {
40      private ArtifactHandlerManager artifactHandlerManager;
41  
42      private String classifier;
43  
44      private String type;
45  
46      /**
47       * @param artifactHanderManager {@link ArtifactHandlerManager}.
48       * @param theClassifier The classifier to use.
49       * @param theType The type.
50       */
51      public ClassifierTypeTranslator( ArtifactHandlerManager artifactHanderManager, String theClassifier,
52                                       String theType )
53      {
54          this.artifactHandlerManager = artifactHanderManager;
55          this.classifier = theClassifier;
56          this.type = theType;
57      }
58  
59      /*
60       * (non-Javadoc)
61       * @see org.apache.mojo.dependency.utils.translators.ArtifactTranslator#translate(java.util.Set,
62       * org.apache.maven.plugin.logging.Log)
63       */
64      @Override
65      public Set<ArtifactCoordinate> translate( Set<Artifact> artifacts, Log log )
66      {
67          Set<ArtifactCoordinate> results;
68  
69          log.debug( "Translating Artifacts using Classifier: " + this.classifier + " and Type: " + this.type );
70          results = new LinkedHashSet<ArtifactCoordinate>();
71          for ( Artifact artifact : artifacts )
72          {
73              // this translator must pass both type and classifier here so we
74              // will use the
75              // base artifact value if null comes in
76              final String useType;
77              if ( StringUtils.isNotEmpty( this.type ) )
78              {
79                  useType = this.type;
80              }
81              else
82              {
83                  useType = artifact.getType();
84              }
85  
86              ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler( useType );
87  
88              final String extension;
89              if ( artifactHandler != null )
90              {
91                  extension = artifactHandler.getExtension();
92              }
93              else
94              {
95                  extension = this.type;
96              }
97  
98              String useClassifier;
99              if ( StringUtils.isNotEmpty( this.classifier ) )
100             {
101                 useClassifier = this.classifier;
102             }
103             else
104             {
105                 useClassifier = artifact.getClassifier();
106             }
107 
108             DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
109             coordinate.setGroupId( artifact.getGroupId() );
110             coordinate.setArtifactId( artifact.getArtifactId() );
111             coordinate.setVersion( artifact.getVersion() );
112             coordinate.setClassifier( useClassifier );
113             coordinate.setExtension( extension );
114 
115             // // Create a new artifact
116             // Artifact newArtifact = factory.createArtifactWithClassifier( artifact.getGroupId(), artifact
117             // .getArtifactId(), artifact.getVersion(), useType, useClassifier );
118             //
119             // // note the new artifacts will always have the scope set to null. We
120             // // should
121             // // reset it here so that it will pass other filters if needed
122             // newArtifact.setScope( artifact.getScope() );
123             //
124             // if ( Artifact.SCOPE_SYSTEM.equals( newArtifact.getScope() ) )
125             // {
126             // File baseDir = repositoryManager.getLocalRepositoryBasedir( buildingRequest );
127             // String path = repositoryManager.getPathForLocalArtifact( buildingRequest, newArtifact );
128             // newArtifact.setFile( new File( baseDir, path ) );
129             // }
130 
131             results.add( coordinate );
132         }
133 
134         return results;
135     }
136 
137     /**
138      * @return Returns the type.
139      */
140     public String getType()
141     {
142         return this.type;
143     }
144 
145     /**
146      * @param theType The type to set.
147      */
148     public void setType( String theType )
149     {
150         this.type = theType;
151     }
152 
153     /**
154      * @return Returns the classifier.
155      */
156     public String getClassifier()
157     {
158         return this.classifier;
159     }
160 
161     /**
162      * @param theClassifier The classifier to set.
163      */
164     public void setClassifier( String theClassifier )
165     {
166         this.classifier = theClassifier;
167     }
168 
169 }