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