View Javadoc
1   package org.apache.maven.artifact.factory;
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 javax.inject.Inject;
23  import javax.inject.Named;
24  import javax.inject.Singleton;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.DefaultArtifact;
28  import org.apache.maven.artifact.handler.ArtifactHandler;
29  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
30  import org.apache.maven.artifact.versioning.VersionRange;
31  
32  /**
33   * DefaultArtifactFactory
34   *
35   */
36  @Named
37  @Singleton
38  @SuppressWarnings( "checkstyle:parameternumber" )
39  public class DefaultArtifactFactory
40      implements ArtifactFactory
41  {
42      private final ArtifactHandlerManager artifactHandlerManager;
43  
44      @Inject
45      public DefaultArtifactFactory( ArtifactHandlerManager artifactHandlerManager )
46      {
47          this.artifactHandlerManager = artifactHandlerManager;
48      }
49  
50      public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type )
51      {
52          return createArtifact( groupId, artifactId, version, scope, type, null, null );
53      }
54  
55      public Artifact createArtifactWithClassifier( String groupId, String artifactId, String version, String type,
56                                                    String classifier )
57      {
58          return createArtifact( groupId, artifactId, version, null, type, classifier, null );
59      }
60  
61      public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange,
62                                                String type, String classifier, String scope )
63      {
64          return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, null );
65      }
66  
67      public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange,
68                                                String type, String classifier, String scope, boolean optional )
69      {
70          return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, null, optional );
71      }
72  
73      public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange,
74                                                String type, String classifier, String scope, String inheritedScope )
75      {
76          return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope );
77      }
78  
79      public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange,
80                                                String type, String classifier, String scope, String inheritedScope,
81                                                boolean optional )
82      {
83          return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, optional );
84      }
85  
86      public Artifact createBuildArtifact( String groupId, String artifactId, String version, String packaging )
87      {
88          return createArtifact( groupId, artifactId, version, null, packaging, null, null );
89      }
90  
91      public Artifact createProjectArtifact( String groupId, String artifactId, String version )
92      {
93          return createProjectArtifact( groupId, artifactId, version, null );
94      }
95  
96      public Artifact createParentArtifact( String groupId, String artifactId, String version )
97      {
98          return createProjectArtifact( groupId, artifactId, version );
99      }
100 
101     public Artifact createPluginArtifact( String groupId, String artifactId, VersionRange versionRange )
102     {
103         return createArtifact( groupId, artifactId, versionRange, "maven-plugin", null, Artifact.SCOPE_RUNTIME, null );
104     }
105 
106     public Artifact createProjectArtifact( String groupId, String artifactId, String version, String scope )
107     {
108         return createArtifact( groupId, artifactId, version, scope, "pom" );
109     }
110 
111     public Artifact createExtensionArtifact( String groupId, String artifactId, VersionRange versionRange )
112     {
113         return createArtifact( groupId, artifactId, versionRange, "jar", null, Artifact.SCOPE_RUNTIME, null );
114     }
115 
116     private Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type,
117                                      String classifier, String inheritedScope )
118     {
119         VersionRange versionRange = null;
120         if ( version != null )
121         {
122             versionRange = VersionRange.createFromVersion( version );
123         }
124         return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope );
125     }
126 
127     private Artifact createArtifact( String groupId, String artifactId, VersionRange versionRange, String type,
128                                      String classifier, String scope, String inheritedScope )
129     {
130         return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, false );
131     }
132 
133     private Artifact createArtifact( String groupId, String artifactId, VersionRange versionRange, String type,
134                                      String classifier, String scope, String inheritedScope, boolean optional )
135     {
136         String desiredScope = Artifact.SCOPE_RUNTIME;
137 
138         if ( inheritedScope == null )
139         {
140             desiredScope = scope;
141         }
142         else if ( Artifact.SCOPE_TEST.equals( scope ) || Artifact.SCOPE_PROVIDED.equals( scope ) )
143         {
144             return null;
145         }
146         else if ( Artifact.SCOPE_COMPILE.equals( scope ) && Artifact.SCOPE_COMPILE.equals( inheritedScope ) )
147         {
148             // added to retain compile artifactScope. Remove if you want compile inherited as runtime
149             desiredScope = Artifact.SCOPE_COMPILE;
150         }
151 
152         if ( Artifact.SCOPE_TEST.equals( inheritedScope ) )
153         {
154             desiredScope = Artifact.SCOPE_TEST;
155         }
156 
157         if ( Artifact.SCOPE_PROVIDED.equals( inheritedScope ) )
158         {
159             desiredScope = Artifact.SCOPE_PROVIDED;
160         }
161 
162         if ( Artifact.SCOPE_SYSTEM.equals( scope ) )
163         {
164             // system scopes come through unchanged...
165             desiredScope = Artifact.SCOPE_SYSTEM;
166         }
167 
168         ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( type );
169 
170         return new DefaultArtifact( groupId, artifactId, versionRange, desiredScope, type, classifier, handler,
171                                     optional );
172     }
173 }