View Javadoc

1   package org.apache.maven;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.maven.project.Dependency;
27  import org.apache.maven.project.Project;
28  import org.apache.maven.repository.Artifact;
29  import org.apache.maven.repository.DefaultArtifactFactory;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  /**
33   * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
34   */
35  public class ArtifactListBuilder
36  {
37      // --------------------------------------------------------------
38      // I M P L E M E N T A T I O N
39      // --------------------------------------------------------------
40  
41      /**
42       * Create the list of artifacts for a project based on the stated dependencies
43       * taking into account any user specified overrides.
44       *
45       * @param project MavenSession project.
46       * @return the list of artifacts for a project
47       */
48      public static List build( final Project project )
49      {
50          List projectArtifacts = new ArrayList();
51          boolean mavenJarOverride = project.getContext().getMavenJarOverride().booleanValue();
52  
53          for ( Iterator i = project.getDependencies().iterator(); i.hasNext(); )
54          {
55              Dependency d = (Dependency) i.next();
56              String mavenJarProperty = project.getContext()
57                  .getMavenJarOverride( Project.standardToLegacyId( d.getId() ) );
58              Artifact artifact = DefaultArtifactFactory.createArtifact( d );
59  
60              if ( mavenJarOverride && StringUtils.isNotEmpty( mavenJarProperty ) )
61              {
62                  // The jar override option has been set and we have a property
63                  // for the this dependency so override the path with the user
64                  // specified value.
65                  if ( Character.isDigit( mavenJarProperty.charAt( 0 ) ) || "SNAPSHOT".equals( mavenJarProperty ) )
66                  {
67                      // User is requesting a specific version of a dependency
68                      // be used.
69                      d.setVersion( mavenJarProperty );
70                      artifact.setPath( project.getContext().getMavenRepoLocal() + artifact.generatePath() );
71                      artifact.setOverrideType( Artifact.OVERRIDE_VERSION );
72                  }
73                  else
74                  {
75                      // User is requesting a specific path to a dependency
76                      // be used.
77                      artifact.setPath( new File( mavenJarProperty ).getAbsolutePath() );
78                      artifact.setOverrideType( Artifact.OVERRIDE_PATH );
79                  }
80              }
81              else
82              {
83                  artifact.setPath( project.getContext().getMavenRepoLocal() + artifact.generatePath() );
84              }
85  
86              projectArtifacts.add( artifact );
87          }
88  
89          return projectArtifacts;
90      }
91  }