KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > artifact > factory > DefaultArtifactFactory


1 package org.apache.maven.artifact.factory;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.DefaultArtifact;
21 import org.apache.maven.artifact.handler.ArtifactHandler;
22 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
23 import org.apache.maven.artifact.versioning.VersionRange;
24
25 public class DefaultArtifactFactory
26     implements ArtifactFactory
27 {
28     // TODO: remove, it doesn't know the ones from the plugins
29
private ArtifactHandlerManager artifactHandlerManager;
30
31     public DefaultArtifactFactory()
32     {
33     }
34
35     public Artifact createArtifact( String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version, String JavaDoc scope, String JavaDoc type )
36     {
37         return createArtifact( groupId, artifactId, version, scope, type, null, null );
38     }
39
40     public Artifact createArtifactWithClassifier( String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version, String JavaDoc type,
41                                                   String JavaDoc classifier )
42     {
43         return createArtifact( groupId, artifactId, version, null, type, classifier, null );
44     }
45
46     public Artifact createDependencyArtifact( String JavaDoc groupId, String JavaDoc artifactId, VersionRange versionRange, String JavaDoc type,
47                                               String JavaDoc classifier, String JavaDoc scope )
48     {
49         return createArtifact( groupId, artifactId, versionRange, type, classifier, null, null );
50     }
51
52     public Artifact createDependencyArtifact( String JavaDoc groupId, String JavaDoc artifactId, VersionRange versionRange, String JavaDoc type,
53                                               String JavaDoc classifier, String JavaDoc scope, boolean optional )
54     {
55         return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, null, optional );
56     }
57
58     public Artifact createDependencyArtifact( String JavaDoc groupId, String JavaDoc artifactId, VersionRange versionRange, String JavaDoc type,
59                                               String JavaDoc classifier, String JavaDoc scope, String JavaDoc inheritedScope )
60     {
61         return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope );
62     }
63
64     public Artifact createDependencyArtifact( String JavaDoc groupId, String JavaDoc artifactId, VersionRange versionRange, String JavaDoc type,
65                                               String JavaDoc classifier, String JavaDoc scope, String JavaDoc inheritedScope, boolean optional )
66     {
67         return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, optional );
68     }
69
70     public Artifact createBuildArtifact( String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version, String JavaDoc packaging )
71     {
72         return createArtifact( groupId, artifactId, version, null, packaging, null, null );
73     }
74
75     public Artifact createProjectArtifact( String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version )
76     {
77         return createProjectArtifact( groupId, artifactId, version, null );
78     }
79
80     public Artifact createParentArtifact( String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version )
81     {
82         return createProjectArtifact( groupId, artifactId, version );
83     }
84
85     public Artifact createPluginArtifact( String JavaDoc groupId, String JavaDoc artifactId, VersionRange versionRange )
86     {
87         return createArtifact( groupId, artifactId, versionRange, "maven-plugin", null, Artifact.SCOPE_RUNTIME, null );
88     }
89
90     public Artifact createProjectArtifact( String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version, String JavaDoc scope )
91     {
92         return createArtifact( groupId, artifactId, version, scope, "pom" );
93     }
94
95     public Artifact createExtensionArtifact( String JavaDoc groupId, String JavaDoc artifactId, VersionRange versionRange )
96     {
97         return createArtifact( groupId, artifactId, versionRange, "jar", null, Artifact.SCOPE_RUNTIME, null );
98     }
99
100     private Artifact createArtifact( String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version, String JavaDoc scope, String JavaDoc type,
101                                      String JavaDoc classifier, String JavaDoc inheritedScope )
102     {
103         VersionRange versionRange = null;
104         if ( version != null )
105         {
106             versionRange = VersionRange.createFromVersion( version );
107         }
108         return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope );
109     }
110
111     private Artifact createArtifact( String JavaDoc groupId, String JavaDoc artifactId, VersionRange versionRange, String JavaDoc type,
112                                      String JavaDoc classifier, String JavaDoc scope, String JavaDoc inheritedScope )
113     {
114         return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, false );
115     }
116
117     private Artifact createArtifact( String JavaDoc groupId, String JavaDoc artifactId, VersionRange versionRange, String JavaDoc type,
118                                      String JavaDoc classifier, String JavaDoc scope, String JavaDoc inheritedScope, boolean optional )
119     {
120         // TODO: can refactor - inherited scope calculation belongs in the collector, use scope handler
121

122         String JavaDoc desiredScope = Artifact.SCOPE_RUNTIME;
123         if ( inheritedScope == null )
124         {
125             desiredScope = scope;
126         }
127         else if ( Artifact.SCOPE_TEST.equals( scope ) || Artifact.SCOPE_PROVIDED.equals( scope ) )
128         {
129             return null;
130         }
131         else if ( Artifact.SCOPE_COMPILE.equals( scope ) && Artifact.SCOPE_COMPILE.equals( inheritedScope ) )
132         {
133             // added to retain compile scope. Remove if you want compile inherited as runtime
134
desiredScope = Artifact.SCOPE_COMPILE;
135         }
136
137         if ( Artifact.SCOPE_TEST.equals( inheritedScope ) )
138         {
139             desiredScope = Artifact.SCOPE_TEST;
140         }
141
142         if ( Artifact.SCOPE_PROVIDED.equals( inheritedScope ) )
143         {
144             desiredScope = Artifact.SCOPE_PROVIDED;
145         }
146
147         if ( Artifact.SCOPE_SYSTEM.equals( scope ) )
148         {
149             // system scopes come through unchanged...
150
desiredScope = Artifact.SCOPE_SYSTEM;
151         }
152         
153         ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( type );
154
155         return new DefaultArtifact( groupId, artifactId, versionRange, desiredScope, type, classifier, handler,
156                                     optional );
157     }
158 }
159
Popular Tags