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