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