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