001package org.apache.maven.artifact.factory;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.artifact.Artifact;
023import org.apache.maven.artifact.DefaultArtifact;
024import org.apache.maven.artifact.handler.ArtifactHandler;
025import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
026import org.apache.maven.artifact.versioning.VersionRange;
027import org.codehaus.plexus.component.annotations.Component;
028import org.codehaus.plexus.component.annotations.Requirement;
029
030@Component( role = ArtifactFactory.class )
031public class DefaultArtifactFactory
032    implements ArtifactFactory
033{
034    @Requirement
035    private ArtifactHandlerManager artifactHandlerManager;
036
037    public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type )
038    {
039        return createArtifact( groupId, artifactId, version, scope, type, null, null );
040    }
041
042    public Artifact createArtifactWithClassifier( String groupId, String artifactId, String version, String type,
043                                                  String classifier )
044    {
045        return createArtifact( groupId, artifactId, version, null, type, classifier, null );
046    }
047
048    public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange,
049                                              String type, String classifier, String scope )
050    {
051        return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, null );
052    }
053
054    public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange,
055                                              String type, String classifier, String scope, boolean optional )
056    {
057        return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, null, optional );
058    }
059
060    public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange,
061                                              String type, String classifier, String scope, String inheritedScope )
062    {
063        return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope );
064    }
065
066    public Artifact createDependencyArtifact( String groupId, String artifactId, VersionRange versionRange,
067                                              String type, String classifier, String scope, String inheritedScope,
068                                              boolean optional )
069    {
070        return createArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, optional );
071    }
072
073    public Artifact createBuildArtifact( String groupId, String artifactId, String version, String packaging )
074    {
075        return createArtifact( groupId, artifactId, version, null, packaging, null, null );
076    }
077
078    public Artifact createProjectArtifact( String groupId, String artifactId, String version )
079    {
080        return createProjectArtifact( groupId, artifactId, version, null );
081    }
082
083    public Artifact createParentArtifact( String groupId, String artifactId, String version )
084    {
085        return createProjectArtifact( groupId, artifactId, version );
086    }
087
088    public Artifact createPluginArtifact( String groupId, String artifactId, VersionRange versionRange )
089    {
090        return createArtifact( groupId, artifactId, versionRange, "maven-plugin", null, Artifact.SCOPE_RUNTIME, null );
091    }
092
093    public Artifact createProjectArtifact( String groupId, String artifactId, String version, String scope )
094    {
095        return createArtifact( groupId, artifactId, version, scope, "pom" );
096    }
097
098    public Artifact createExtensionArtifact( String groupId, String artifactId, VersionRange versionRange )
099    {
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            // added to retain compile artifactScope. Remove if you want compile inherited as runtime
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            // system scopes come through unchanged...
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}