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.eclipse.aether.artifact.Artifact;
30  import org.eclipse.aether.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( createRepositoryMetadata( artifact ), null );
44          this.artifact = artifact;
45      }
46  
47      public VersionsMetadata( Artifact artifact, File file )
48      {
49          super( createRepositoryMetadata( artifact ), file );
50          this.artifact = artifact;
51      }
52  
53      private static Metadata createRepositoryMetadata( Artifact artifact )
54      {
55          Metadata metadata = new Metadata();
56          metadata.setGroupId( artifact.getGroupId() );
57          metadata.setArtifactId( artifact.getArtifactId() );
58  
59          Versioning versioning = new Versioning();
60          versioning.addVersion( artifact.getBaseVersion() );
61          if ( !artifact.isSnapshot() )
62          {
63              versioning.setRelease( artifact.getBaseVersion() );
64          }
65          if ( "maven-plugin".equals( artifact.getProperty( ArtifactProperties.TYPE, "" ) ) )
66          {
67              versioning.setLatest( artifact.getBaseVersion() );
68          }
69  
70          metadata.setVersioning( versioning );
71  
72          return metadata;
73      }
74  
75      @Override
76      protected void merge( Metadata recessive )
77      {
78          Versioning versioning = metadata.getVersioning();
79          versioning.updateTimestamp();
80  
81          if ( recessive.getVersioning() != null )
82          {
83              if ( versioning.getLatest() == null )
84              {
85                  versioning.setLatest( recessive.getVersioning().getLatest() );
86              }
87              if ( versioning.getRelease() == null )
88              {
89                  versioning.setRelease( recessive.getVersioning().getRelease() );
90              }
91  
92              Collection<String> versions = new LinkedHashSet<String>( recessive.getVersioning().getVersions() );
93              versions.addAll( versioning.getVersions() );
94              versioning.setVersions( new ArrayList<String>( versions ) );
95          }
96      }
97  
98      public Object getKey()
99      {
100         return getGroupId() + ':' + getArtifactId();
101     }
102 
103     public static Object getKey( Artifact artifact )
104     {
105         return artifact.getGroupId() + ':' + artifact.getArtifactId();
106     }
107 
108     public MavenMetadata setFile( File file )
109     {
110         return new VersionsMetadata( artifact, file );
111     }
112 
113     public String getGroupId()
114     {
115         return artifact.getGroupId();
116     }
117 
118     public String getArtifactId()
119     {
120         return artifact.getArtifactId();
121     }
122 
123     public String getVersion()
124     {
125         return "";
126     }
127 
128     public Nature getNature()
129     {
130         return artifact.isSnapshot() ? Nature.RELEASE_OR_SNAPSHOT : Nature.RELEASE;
131     }
132 
133 }