001package org.apache.maven.repository.internal;
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 java.util.ArrayList;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.LinkedHashMap;
026import java.util.List;
027import java.util.Map;
028
029import org.apache.maven.model.DependencyManagement;
030import org.apache.maven.model.DistributionManagement;
031import org.apache.maven.model.License;
032import org.apache.maven.model.Model;
033import org.apache.maven.model.Prerequisites;
034import org.apache.maven.model.Repository;
035import org.eclipse.aether.RepositorySystemSession;
036import org.eclipse.aether.artifact.Artifact;
037import org.eclipse.aether.artifact.ArtifactProperties;
038import org.eclipse.aether.artifact.ArtifactType;
039import org.eclipse.aether.artifact.ArtifactTypeRegistry;
040import org.eclipse.aether.artifact.DefaultArtifact;
041import org.eclipse.aether.artifact.DefaultArtifactType;
042import org.eclipse.aether.graph.Dependency;
043import org.eclipse.aether.graph.Exclusion;
044import org.eclipse.aether.resolution.ArtifactDescriptorResult;
045
046/**
047 * Populates Aether {@link ArtifactDescriptorResult} from Maven project {@link Model}.
048 * 
049 * @since 3.2.4
050 * @provisional This class is part of work in progress and can be changed or removed without notice.
051 */
052public class ArtifactDescriptorReaderDelegate
053{
054    public void populateResult( RepositorySystemSession session, ArtifactDescriptorResult result, Model model )
055    {
056        ArtifactTypeRegistry stereotypes = session.getArtifactTypeRegistry();
057
058        for ( Repository r : model.getRepositories() )
059        {
060            result.addRepository( ArtifactDescriptorUtils.toRemoteRepository( r ) );
061        }
062
063        for ( org.apache.maven.model.Dependency dependency : model.getDependencies() )
064        {
065            result.addDependency( convert( dependency, stereotypes ) );
066        }
067
068        DependencyManagement mngt = model.getDependencyManagement();
069        if ( mngt != null )
070        {
071            for ( org.apache.maven.model.Dependency dependency : mngt.getDependencies() )
072            {
073                result.addManagedDependency( convert( dependency, stereotypes ) );
074            }
075        }
076
077        Map<String, Object> properties = new LinkedHashMap<String, Object>();
078
079        Prerequisites prerequisites = model.getPrerequisites();
080        if ( prerequisites != null )
081        {
082            properties.put( "prerequisites.maven", prerequisites.getMaven() );
083        }
084
085        List<License> licenses = model.getLicenses();
086        properties.put( "license.count", licenses.size() );
087        for ( int i = 0; i < licenses.size(); i++ )
088        {
089            License license = licenses.get( i );
090            properties.put( "license." + i + ".name", license.getName() );
091            properties.put( "license." + i + ".url", license.getUrl() );
092            properties.put( "license." + i + ".comments", license.getComments() );
093            properties.put( "license." + i + ".distribution", license.getDistribution() );
094        }
095
096        result.setProperties( properties );
097
098        setArtifactProperties( result, model );
099    }
100
101    private Dependency convert( org.apache.maven.model.Dependency dependency, ArtifactTypeRegistry stereotypes )
102    {
103        ArtifactType stereotype = stereotypes.get( dependency.getType() );
104        if ( stereotype == null )
105        {
106            stereotype = new DefaultArtifactType( dependency.getType() );
107        }
108
109        boolean system = dependency.getSystemPath() != null && dependency.getSystemPath().length() > 0;
110
111        Map<String, String> props = null;
112        if ( system )
113        {
114            props = Collections.singletonMap( ArtifactProperties.LOCAL_PATH, dependency.getSystemPath() );
115        }
116
117        Artifact artifact =
118            new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), null,
119                                 dependency.getVersion(), props, stereotype );
120
121        List<Exclusion> exclusions = new ArrayList<Exclusion>( dependency.getExclusions().size() );
122        for ( org.apache.maven.model.Exclusion exclusion : dependency.getExclusions() )
123        {
124            exclusions.add( convert( exclusion ) );
125        }
126
127        Dependency result = new Dependency( artifact, dependency.getScope(), dependency.isOptional(), exclusions );
128
129        return result;
130    }
131
132    private Exclusion convert( org.apache.maven.model.Exclusion exclusion )
133    {
134        return new Exclusion( exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*" );
135    }
136
137    private void setArtifactProperties( ArtifactDescriptorResult result, Model model )
138    {
139        String downloadUrl = null;
140        DistributionManagement distMngt = model.getDistributionManagement();
141        if ( distMngt != null )
142        {
143            downloadUrl = distMngt.getDownloadUrl();
144        }
145        if ( downloadUrl != null && downloadUrl.length() > 0 )
146        {
147            Artifact artifact = result.getArtifact();
148            Map<String, String> props = new HashMap<String, String>( artifact.getProperties() );
149            props.put( ArtifactProperties.DOWNLOAD_URL, downloadUrl );
150            result.setArtifact( artifact.setProperties( props ) );
151        }
152    }
153}