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.xml.XmlStreamWriter;
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      @Override
53      public String getRemoteFilename() {
54          return getFilename();
55      }
56  
57      @Override
58      public String getLocalFilename(ArtifactRepository repository) {
59          return getFilename();
60      }
61  
62      private String getFilename() {
63          return getArtifactId() + "-" + artifact.getVersion() + "-" + file.getName();
64      }
65  
66      @Override
67      public void storeInLocalRepository(ArtifactRepository localRepository, ArtifactRepository remoteRepository)
68              throws RepositoryMetadataStoreException {
69          File destination = new File(
70                  localRepository.getBasedir(), localRepository.pathOfLocalRepositoryMetadata(this, remoteRepository));
71  
72          if (!destination.getParentFile().mkdirs()) {
73              throw new RepositoryMetadataStoreException(
74                      "Could not create artifact directory " + destination + " in local repository");
75          }
76  
77          try (Writer writer = new XmlStreamWriter(destination)) {
78              new SiteXpp3Writer().write(writer, siteModel);
79          } catch (IOException e) {
80              throw new RepositoryMetadataStoreException("Error saving in local repository", e);
81          }
82      }
83  
84      @Override
85      public String toString() {
86          return "site descriptor for " + artifact.getArtifactId() + " " + artifact.getVersion() + " " + file.getName();
87      }
88  
89      @Override
90      public boolean storedInArtifactVersionDirectory() {
91          return true;
92      }
93  
94      @Override
95      public String getBaseVersion() {
96          return artifact.getBaseVersion();
97      }
98  
99      @Override
100     public Object getKey() {
101         return "site descriptor " + artifact.getGroupId() + ":" + artifact.getArtifactId() + " " + file.getName();
102     }
103 
104     @Override
105     public void merge(ArtifactMetadata metadata) {
106         SiteDescriptorArtifactMetadata m = (SiteDescriptorArtifactMetadata) metadata;
107         if (!m.file.equals(file)) {
108             throw new IllegalStateException("Cannot add two different pieces of metadata for: " + getKey());
109         }
110     }
111 
112     @Override
113     public void merge(org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata) {
114         // FIXME what todo here ?
115     }
116 }