View Javadoc

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