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