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  
28  public class DefaultArtifactFactory
29      implements ArtifactFactory
30  {
31      // TODO: remove, it doesn't know the ones from the plugins
32      private ArtifactHandlerManager artifactHandlerManager;
33  
34      public DefaultArtifactFactory()
35      {
36      }
37  
38      public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type )
39      {
40          return createArtifact( groupId, artifactId, version, scope, type, null, null );
41      }
42  
43      public Artifact createArtifactWithClassifier( String groupId, String artifactId, String version, String type,
44                                                    String classifier )
45      {
46          return createArtifact( groupId, artifactId, version, null, type, classifier, null );
47      }
48  
49      public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange, String type,
50                                                String classifier, String scope )
51      {
52          return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, null );
53      }
54  
55      public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange, String type,
56                                                String classifier, String scope, boolean optional )
57      {
58          return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, null, optional );
59      }
60  
61      public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange, String type,
62                                                String classifier, String scope, String inheritedScope )
63      {
64          return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope );
65      }
66  
67      public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange, String type,
68                                                String classifier, String scope, String inheritedScope, 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         // TODO: can refactor - inherited scope calculation belongs in the collector, use scope handler
124 
125         String desiredScope = Artifact.SCOPE_RUNTIME;
126         if ( inheritedScope == null )
127         {
128             desiredScope = scope;
129         }
130         else if ( Artifact.SCOPE_TEST.equals( scope ) || Artifact.SCOPE_PROVIDED.equals( scope ) )
131         {
132             return null;
133         }
134         else if ( Artifact.SCOPE_COMPILE.equals( scope ) && Artifact.SCOPE_COMPILE.equals( inheritedScope ) )
135         {
136             // added to retain compile scope. Remove if you want compile inherited as runtime
137             desiredScope = Artifact.SCOPE_COMPILE;
138         }
139 
140         if ( Artifact.SCOPE_TEST.equals( inheritedScope ) )
141         {
142             desiredScope = Artifact.SCOPE_TEST;
143         }
144 
145         if ( Artifact.SCOPE_PROVIDED.equals( inheritedScope ) )
146         {
147             desiredScope = Artifact.SCOPE_PROVIDED;
148         }
149 
150         if ( Artifact.SCOPE_SYSTEM.equals( scope ) )
151         {
152             // system scopes come through unchanged...
153             desiredScope = Artifact.SCOPE_SYSTEM;
154         }
155         
156         ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( type );
157 
158         return new DefaultArtifact( groupId, artifactId, versionRange, desiredScope, type, classifier, handler,
159                                     optional );
160     }
161 
162     protected ArtifactHandlerManager getArtifactHandlerManager()
163     {
164         return artifactHandlerManager;
165     }
166 }