View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.artifact.factory;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.DefaultArtifact;
26  import org.apache.maven.artifact.handler.ArtifactHandler;
27  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
28  import org.apache.maven.artifact.versioning.VersionRange;
29  
30  /**
31   * DefaultArtifactFactory
32   *
33   */
34  @Named
35  @Singleton
36  @SuppressWarnings("checkstyle:parameternumber")
37  public class DefaultArtifactFactory implements ArtifactFactory {
38      private final ArtifactHandlerManager artifactHandlerManager;
39  
40      @Inject
41      public DefaultArtifactFactory(ArtifactHandlerManager artifactHandlerManager) {
42          this.artifactHandlerManager = artifactHandlerManager;
43      }
44  
45      public Artifact createArtifact(String groupId, String artifactId, String version, String scope, String type) {
46          return createArtifact(groupId, artifactId, version, scope, type, null, null);
47      }
48  
49      public Artifact createArtifactWithClassifier(
50              String groupId, String artifactId, String version, String type, String classifier) {
51          return createArtifact(groupId, artifactId, version, null, type, classifier, null);
52      }
53  
54      public Artifact createDependencyArtifact(
55              String groupId,
56              String artifactId,
57              VersionRange versionRange,
58              String type,
59              String classifier,
60              String scope) {
61          return createArtifact(groupId, artifactId, versionRange, type, classifier, scope, null);
62      }
63  
64      public Artifact createDependencyArtifact(
65              String groupId,
66              String artifactId,
67              VersionRange versionRange,
68              String type,
69              String classifier,
70              String scope,
71              boolean optional) {
72          return createArtifact(groupId, artifactId, versionRange, type, classifier, scope, null, optional);
73      }
74  
75      public Artifact createDependencyArtifact(
76              String groupId,
77              String artifactId,
78              VersionRange versionRange,
79              String type,
80              String classifier,
81              String scope,
82              String inheritedScope) {
83          return createArtifact(groupId, artifactId, versionRange, type, classifier, scope, inheritedScope);
84      }
85  
86      public Artifact createDependencyArtifact(
87              String groupId,
88              String artifactId,
89              VersionRange versionRange,
90              String type,
91              String classifier,
92              String scope,
93              String inheritedScope,
94              boolean optional) {
95          return createArtifact(groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, optional);
96      }
97  
98      public Artifact createBuildArtifact(String groupId, String artifactId, String version, String packaging) {
99          return createArtifact(groupId, artifactId, version, null, packaging, null, null);
100     }
101 
102     public Artifact createProjectArtifact(String groupId, String artifactId, String version) {
103         return createProjectArtifact(groupId, artifactId, version, null);
104     }
105 
106     public Artifact createParentArtifact(String groupId, String artifactId, String version) {
107         return createProjectArtifact(groupId, artifactId, version);
108     }
109 
110     public Artifact createPluginArtifact(String groupId, String artifactId, VersionRange versionRange) {
111         return createArtifact(groupId, artifactId, versionRange, "maven-plugin", null, Artifact.SCOPE_RUNTIME, null);
112     }
113 
114     public Artifact createProjectArtifact(String groupId, String artifactId, String version, String scope) {
115         return createArtifact(groupId, artifactId, version, scope, "pom");
116     }
117 
118     public Artifact createExtensionArtifact(String groupId, String artifactId, VersionRange versionRange) {
119         return createArtifact(groupId, artifactId, versionRange, "jar", null, Artifact.SCOPE_RUNTIME, null);
120     }
121 
122     private Artifact createArtifact(
123             String groupId,
124             String artifactId,
125             String version,
126             String scope,
127             String type,
128             String classifier,
129             String inheritedScope) {
130         VersionRange versionRange = null;
131         if (version != null) {
132             versionRange = VersionRange.createFromVersion(version);
133         }
134         return createArtifact(groupId, artifactId, versionRange, type, classifier, scope, inheritedScope);
135     }
136 
137     private Artifact createArtifact(
138             String groupId,
139             String artifactId,
140             VersionRange versionRange,
141             String type,
142             String classifier,
143             String scope,
144             String inheritedScope) {
145         return createArtifact(groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, false);
146     }
147 
148     private Artifact createArtifact(
149             String groupId,
150             String artifactId,
151             VersionRange versionRange,
152             String type,
153             String classifier,
154             String scope,
155             String inheritedScope,
156             boolean optional) {
157         String desiredScope = Artifact.SCOPE_RUNTIME;
158 
159         if (inheritedScope == null) {
160             desiredScope = scope;
161         } else if (Artifact.SCOPE_TEST.equals(scope) || Artifact.SCOPE_PROVIDED.equals(scope)) {
162             return null;
163         } else if (Artifact.SCOPE_COMPILE.equals(scope) && Artifact.SCOPE_COMPILE.equals(inheritedScope)) {
164             // added to retain compile artifactScope. Remove if you want compile inherited as runtime
165             desiredScope = Artifact.SCOPE_COMPILE;
166         }
167 
168         if (Artifact.SCOPE_TEST.equals(inheritedScope)) {
169             desiredScope = Artifact.SCOPE_TEST;
170         }
171 
172         if (Artifact.SCOPE_PROVIDED.equals(inheritedScope)) {
173             desiredScope = Artifact.SCOPE_PROVIDED;
174         }
175 
176         if (Artifact.SCOPE_SYSTEM.equals(scope)) {
177             // system scopes come through unchanged...
178             desiredScope = Artifact.SCOPE_SYSTEM;
179         }
180 
181         ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(type);
182 
183         return new DefaultArtifact(
184                 groupId, artifactId, versionRange, desiredScope, type, classifier, handler, optional);
185     }
186 }