View Javadoc
1   package org.apache.maven.plugins.site.descriptor;
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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.metadata.AbstractArtifactMetadata;
24  import org.apache.maven.artifact.metadata.ArtifactMetadata;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataStoreException;
27  import org.apache.maven.doxia.site.decoration.DecorationModel;
28  import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer;
29  import org.codehaus.plexus.util.WriterFactory;
30  
31  import java.io.File;
32  import java.io.IOException;
33  import java.io.Writer;
34  
35  /**
36   * Attach a POM to an artifact.
37   *
38   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
39   *
40   */
41  public class SiteDescriptorArtifactMetadata
42      extends AbstractArtifactMetadata
43  {
44      private final DecorationModel decoration;
45  
46      private final File file;
47  
48      public SiteDescriptorArtifactMetadata( Artifact artifact, DecorationModel decoration, File file )
49      {
50          super( artifact );
51  
52          this.file = file;
53          this.decoration = decoration;
54      }
55  
56      public String getRemoteFilename()
57      {
58          return getFilename();
59      }
60  
61      public String getLocalFilename( ArtifactRepository repository )
62      {
63          return getFilename();
64      }
65  
66      private String getFilename()
67      {
68          return getArtifactId() + "-" + artifact.getVersion() + "-" + file.getName();
69      }
70  
71      public void storeInLocalRepository( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
72          throws RepositoryMetadataStoreException
73      {
74          File destination = new File( localRepository.getBasedir(),
75                                       localRepository.pathOfLocalRepositoryMetadata( this, remoteRepository ) );
76  
77          destination.getParentFile().mkdirs();
78  
79          try ( Writer writer = WriterFactory.newXmlWriter( destination ) )
80          {
81              new DecorationXpp3Writer().write( writer, decoration );
82          }
83          catch ( IOException e )
84          {
85              throw new RepositoryMetadataStoreException( "Error saving in local repository", e );
86          }
87      }
88  
89      public String toString()
90      {
91          return "site descriptor for " + artifact.getArtifactId() + " " + artifact.getVersion() + " " + file.getName();
92      }
93  
94      public boolean storedInArtifactVersionDirectory()
95      {
96          return true;
97      }
98  
99      public String getBaseVersion()
100     {
101         return artifact.getBaseVersion();
102     }
103 
104     public Object getKey()
105     {
106         return "site descriptor " + artifact.getGroupId() + ":" + artifact.getArtifactId() + " " + file.getName();
107     }
108 
109     public void merge( ArtifactMetadata metadata )
110     {
111         SiteDescriptorArtifactMetadata m = (SiteDescriptorArtifactMetadata) metadata;
112         if ( !m.file.equals( file ) )
113         {
114             throw new IllegalStateException( "Cannot add two different pieces of metadata for: " + getKey() );
115         }
116     }
117 
118     public void merge( org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata )
119     {
120         // FIXME what todo here ?
121     }
122 }