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.transfer.artifact.ArtifactCoordinate;
30  import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  /**
34   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
35   */
36  public class ClassifierTypeTranslator
37      implements ArtifactTranslator
38  {
39      private ArtifactHandlerManager artifactHandlerManager;
40  
41      private String classifier;
42  
43      private String type;
44  
45      /**
46       * @param artifactHanderManager {@link ArtifactHandlerManager}.
47       * @param theClassifier The classifier to use.
48       * @param theType The type.
49       */
50      public ClassifierTypeTranslator( ArtifactHandlerManager artifactHanderManager, String theClassifier,
51                                       String theType )
52      {
53          this.artifactHandlerManager = artifactHanderManager;
54          this.classifier = theClassifier;
55          this.type = theType;
56      }
57  
58      /*
59       * (non-Javadoc)
60       * @see org.apache.mojo.dependency.utils.translators.ArtifactTranslator#translate(java.util.Set,
61       * org.apache.maven.plugin.logging.Log)
62       */
63      @Override
64      public Set<ArtifactCoordinate> translate( Set<Artifact> artifacts, Log log )
65      {
66          Set<ArtifactCoordinate> results;
67  
68          log.debug( "Translating Artifacts using Classifier: " + this.classifier + " and Type: " + this.type );
69          results = new LinkedHashSet<>();
70          for ( Artifact artifact : artifacts )
71          {
72              // this translator must pass both type and classifier here so we
73              // will use the
74              // base artifact value if null comes in
75              final String useType;
76              if ( StringUtils.isNotEmpty( this.type ) )
77              {
78                  useType = this.type;
79              }
80              else
81              {
82                  useType = artifact.getType();
83              }
84  
85              ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler( useType );
86  
87              final String extension;
88              if ( artifactHandler != null )
89              {
90                  extension = artifactHandler.getExtension();
91              }
92              else
93              {
94                  extension = this.type;
95              }
96  
97              String useClassifier;
98              if ( StringUtils.isNotEmpty( this.classifier ) )
99              {
100                 useClassifier = this.classifier;
101             }
102             else
103             {
104                 useClassifier = artifact.getClassifier();
105             }
106 
107             DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
108             coordinate.setGroupId( artifact.getGroupId() );
109             coordinate.setArtifactId( artifact.getArtifactId() );
110             coordinate.setVersion( artifact.getVersion() );
111             coordinate.setClassifier( useClassifier );
112             coordinate.setExtension( extension );
113 
114             // // Create a new artifact
115             // Artifact newArtifact = factory.createArtifactWithClassifier( artifact.getGroupId(), artifact
116             // .getArtifactId(), artifact.getVersion(), useType, useClassifier );
117             //
118             // // note the new artifacts will always have the scope set to null. We
119             // // should
120             // // reset it here so that it will pass other filters if needed
121             // newArtifact.setScope( artifact.getScope() );
122             //
123             // if ( Artifact.SCOPE_SYSTEM.equals( newArtifact.getScope() ) )
124             // {
125             // File baseDir = repositoryManager.getLocalRepositoryBasedir( buildingRequest );
126             // String path = repositoryManager.getPathForLocalArtifact( buildingRequest, newArtifact );
127             // newArtifact.setFile( new File( baseDir, path ) );
128             // }
129 
130             results.add( coordinate );
131         }
132 
133         return results;
134     }
135 
136     /**
137      * @return Returns the type.
138      */
139     public String getType()
140     {
141         return this.type;
142     }
143 
144     /**
145      * @param theType The type to set.
146      */
147     public void setType( String theType )
148     {
149         this.type = theType;
150     }
151 
152     /**
153      * @return Returns the classifier.
154      */
155     public String getClassifier()
156     {
157         return this.classifier;
158     }
159 
160     /**
161      * @param theClassifier The classifier to set.
162      */
163     public void setClassifier( String theClassifier )
164     {
165         this.classifier = theClassifier;
166     }
167 
168 }