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