1 package org.apache.maven.artifact.factory;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.DefaultArtifact;
24 import org.apache.maven.artifact.handler.ArtifactHandler;
25 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
26 import org.apache.maven.artifact.versioning.VersionRange;
27 import org.codehaus.plexus.component.annotations.Component;
28 import org.codehaus.plexus.component.annotations.Requirement;
29
30 @Component( role = ArtifactFactory.class )
31 public class DefaultArtifactFactory
32 implements ArtifactFactory
33 {
34 @Requirement
35 private ArtifactHandlerManager artifactHandlerManager;
36
37 public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type )
38 {
39 return createArtifact( groupId, artifactId, version, scope, type, null, null );
40 }
41
42 public Artifact createArtifactWithClassifier( String groupId, String artifactId, String version, String type,
43 String classifier )
44 {
45 return createArtifact( groupId, artifactId, version, null, type, classifier, null );
46 }
47
48 public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange,
49 String type, String classifier, String scope )
50 {
51 return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, null );
52 }
53
54 public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange,
55 String type, String classifier, String scope, boolean optional )
56 {
57 return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, null, optional );
58 }
59
60 public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange,
61 String type, String classifier, String scope, String inheritedScope )
62 {
63 return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope );
64 }
65
66 public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange,
67 String type, String classifier, String scope, String inheritedScope,
68 boolean optional )
69 {
70 return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, optional );
71 }
72
73 public Artifact createBuildArtifact( String groupId, String artifactId, String version, String packaging )
74 {
75 return createArtifact( groupId, artifactId, version, null, packaging, null, null );
76 }
77
78 public Artifact createProjectArtifact( String groupId, String artifactId, String version )
79 {
80 return createProjectArtifact( groupId, artifactId, version, null );
81 }
82
83 public Artifact createParentArtifact( String groupId, String artifactId, String version )
84 {
85 return createProjectArtifact( groupId, artifactId, version );
86 }
87
88 public Artifact createPluginArtifact( String groupId, String artifactId, VersionRange versionRange )
89 {
90 return createArtifact( groupId, artifactId, versionRange, "maven-plugin", null, Artifact.SCOPE_RUNTIME, null );
91 }
92
93 public Artifact createProjectArtifact( String groupId, String artifactId, String version, String scope )
94 {
95 return createArtifact( groupId, artifactId, version, scope, "pom" );
96 }
97
98 public Artifact createExtensionArtifact( String groupId, String artifactId, VersionRange versionRange )
99 {
100 return createArtifact( groupId, artifactId, versionRange, "jar", null, Artifact.SCOPE_RUNTIME, null );
101 }
102
103 private Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type,
104 String classifier, String inheritedScope )
105 {
106 VersionRange versionRange = null;
107 if ( version != null )
108 {
109 versionRange = VersionRange.createFromVersion( version );
110 }
111 return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope );
112 }
113
114 private Artifact createArtifact( String groupId, String artifactId, VersionRange versionRange, String type,
115 String classifier, String scope, String inheritedScope )
116 {
117 return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, false );
118 }
119
120 private Artifact createArtifact( String groupId, String artifactId, VersionRange versionRange, String type,
121 String classifier, String scope, String inheritedScope, boolean optional )
122 {
123 String desiredScope = Artifact.SCOPE_RUNTIME;
124
125 if ( inheritedScope == null )
126 {
127 desiredScope = scope;
128 }
129 else if ( Artifact.SCOPE_TEST.equals( scope ) || Artifact.SCOPE_PROVIDED.equals( scope ) )
130 {
131 return null;
132 }
133 else if ( Artifact.SCOPE_COMPILE.equals( scope ) && Artifact.SCOPE_COMPILE.equals( inheritedScope ) )
134 {
135
136 desiredScope = Artifact.SCOPE_COMPILE;
137 }
138
139 if ( Artifact.SCOPE_TEST.equals( inheritedScope ) )
140 {
141 desiredScope = Artifact.SCOPE_TEST;
142 }
143
144 if ( Artifact.SCOPE_PROVIDED.equals( inheritedScope ) )
145 {
146 desiredScope = Artifact.SCOPE_PROVIDED;
147 }
148
149 if ( Artifact.SCOPE_SYSTEM.equals( scope ) )
150 {
151
152 desiredScope = Artifact.SCOPE_SYSTEM;
153 }
154
155 ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( type );
156
157 return new DefaultArtifact( groupId, artifactId, versionRange, desiredScope, type, classifier, handler,
158 optional );
159 }
160 }