View Javadoc

1   package org.apache.maven.repository.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.LinkedHashSet;
26  
27  import org.apache.maven.artifact.repository.metadata.Metadata;
28  import org.apache.maven.artifact.repository.metadata.Versioning;
29  import org.sonatype.aether.artifact.Artifact;
30  import org.sonatype.aether.util.artifact.ArtifactProperties;
31  
32  /**
33   * @author Benjamin Bentmann
34   */
35  final class VersionsMetadata
36      extends MavenMetadata
37  {
38  
39      private final Artifact artifact;
40  
41      public VersionsMetadata( Artifact artifact )
42      {
43          super( createMetadata( artifact ), null );
44          this.artifact = artifact;
45      }
46  
47      public VersionsMetadata( Artifact artifact, File file )
48      {
49          super( createMetadata( artifact ), file );
50          this.artifact = artifact;
51      }
52  
53      private static Metadata createMetadata( Artifact artifact )
54      {
55          Versioning versioning = new Versioning();
56          versioning.addVersion( artifact.getBaseVersion() );
57          if ( !artifact.isSnapshot() )
58          {
59              versioning.setRelease( artifact.getBaseVersion() );
60          }
61          if ( "maven-plugin".equals( artifact.getProperty( ArtifactProperties.TYPE, "" ) ) )
62          {
63              versioning.setLatest( artifact.getBaseVersion() );
64          }
65  
66          Metadata metadata = new Metadata();
67          metadata.setVersioning( versioning );
68          metadata.setGroupId( artifact.getGroupId() );
69          metadata.setArtifactId( artifact.getArtifactId() );
70  
71          return metadata;
72      }
73  
74      @Override
75      protected void merge( Metadata recessive )
76      {
77          Versioning versioning = metadata.getVersioning();
78          versioning.updateTimestamp();
79  
80          if ( recessive.getVersioning() != null )
81          {
82              if ( versioning.getLatest() == null )
83              {
84                  versioning.setLatest( recessive.getVersioning().getLatest() );
85              }
86              if ( versioning.getRelease() == null )
87              {
88                  versioning.setRelease( recessive.getVersioning().getRelease() );
89              }
90  
91              Collection<String> versions = new LinkedHashSet<String>( recessive.getVersioning().getVersions() );
92              versions.addAll( versioning.getVersions() );
93              versioning.setVersions( new ArrayList<String>( versions ) );
94          }
95      }
96  
97      public Object getKey()
98      {
99          return getGroupId() + ':' + getArtifactId();
100     }
101 
102     public static Object getKey( Artifact artifact )
103     {
104         return artifact.getGroupId() + ':' + artifact.getArtifactId();
105     }
106 
107     public MavenMetadata setFile( File file )
108     {
109         return new VersionsMetadata( artifact, file );
110     }
111 
112     public String getGroupId()
113     {
114         return artifact.getGroupId();
115     }
116 
117     public String getArtifactId()
118     {
119         return artifact.getArtifactId();
120     }
121 
122     public String getVersion()
123     {
124         return "";
125     }
126 
127     public Nature getNature()
128     {
129         return artifact.isSnapshot() ? Nature.RELEASE_OR_SNAPSHOT : Nature.RELEASE;
130     }
131 
132 }