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