View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.site.descriptor;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.Writer;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.metadata.AbstractArtifactMetadata;
27  import org.apache.maven.artifact.metadata.ArtifactMetadata;
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataStoreException;
30  import org.apache.maven.doxia.site.SiteModel;
31  import org.apache.maven.doxia.site.io.xpp3.SiteXpp3Writer;
32  import org.codehaus.plexus.util.WriterFactory;
33  
34  /**
35   * Attach a POM to an artifact.
36   *
37   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
38   *
39   */
40  public class SiteDescriptorArtifactMetadata extends AbstractArtifactMetadata {
41      private final SiteModel siteModel;
42  
43      private final File file;
44  
45      public SiteDescriptorArtifactMetadata(Artifact artifact, SiteModel siteModel, File file) {
46          super(artifact);
47  
48          this.file = file;
49          this.siteModel = siteModel;
50      }
51  
52      public String getRemoteFilename() {
53          return getFilename();
54      }
55  
56      public String getLocalFilename(ArtifactRepository repository) {
57          return getFilename();
58      }
59  
60      private String getFilename() {
61          return getArtifactId() + "-" + artifact.getVersion() + "-" + file.getName();
62      }
63  
64      public void storeInLocalRepository(ArtifactRepository localRepository, ArtifactRepository remoteRepository)
65              throws RepositoryMetadataStoreException {
66          File destination = new File(
67                  localRepository.getBasedir(), localRepository.pathOfLocalRepositoryMetadata(this, remoteRepository));
68  
69          destination.getParentFile().mkdirs();
70  
71          try (Writer writer = WriterFactory.newXmlWriter(destination)) {
72              new SiteXpp3Writer().write(writer, siteModel);
73          } catch (IOException e) {
74              throw new RepositoryMetadataStoreException("Error saving in local repository", e);
75          }
76      }
77  
78      public String toString() {
79          return "site descriptor for " + artifact.getArtifactId() + " " + artifact.getVersion() + " " + file.getName();
80      }
81  
82      public boolean storedInArtifactVersionDirectory() {
83          return true;
84      }
85  
86      public String getBaseVersion() {
87          return artifact.getBaseVersion();
88      }
89  
90      public Object getKey() {
91          return "site descriptor " + artifact.getGroupId() + ":" + artifact.getArtifactId() + " " + file.getName();
92      }
93  
94      public void merge(ArtifactMetadata metadata) {
95          SiteDescriptorArtifactMetadata m = (SiteDescriptorArtifactMetadata) metadata;
96          if (!m.file.equals(file)) {
97              throw new IllegalStateException("Cannot add two different pieces of metadata for: " + getKey());
98          }
99      }
100 
101     public void merge(org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata) {
102         // FIXME what todo here ?
103     }
104 }