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