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.project.artifact;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.metadata.AbstractArtifactMetadata;
26  import org.apache.maven.artifact.metadata.ArtifactMetadata;
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataStoreException;
29  import org.codehaus.plexus.util.FileUtils;
30  
31  /**
32   * Attach a POM to an artifact.
33   *
34   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
35   */
36  public class ProjectArtifactMetadata extends AbstractArtifactMetadata {
37      private final File file;
38  
39      public ProjectArtifactMetadata(Artifact artifact) {
40          this(artifact, null);
41      }
42  
43      public ProjectArtifactMetadata(Artifact artifact, File file) {
44          super(artifact);
45          this.file = file;
46      }
47  
48      public File getFile() {
49          return file;
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() + ".pom";
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          // ----------------------------------------------------------------------------
70          // I'm fully aware that the file could just be moved using File.rename but
71          // there are bugs in various JVM that have problems doing this across
72          // different filesystem. So we'll incur the small hit to actually copy
73          // here and be safe. jvz.
74          // ----------------------------------------------------------------------------
75  
76          try {
77              FileUtils.copyFile(file, destination);
78          } catch (IOException e) {
79              throw new RepositoryMetadataStoreException("Error copying POM to the local repository.", e);
80          }
81      }
82  
83      public String toString() {
84          return "project information for " + artifact.getArtifactId() + " " + artifact.getVersion();
85      }
86  
87      public boolean storedInArtifactVersionDirectory() {
88          return true;
89      }
90  
91      public String getBaseVersion() {
92          return artifact.getBaseVersion();
93      }
94  
95      public Object getKey() {
96          return "project " + artifact.getGroupId() + ":" + artifact.getArtifactId();
97      }
98  
99      public void merge(ArtifactMetadata metadata) {
100         ProjectArtifactMetadata m = (ProjectArtifactMetadata) metadata;
101         if (!m.file.equals(file)) {
102             throw new IllegalStateException("Cannot add two different pieces of metadata for: " + getKey());
103         }
104     }
105 
106     public void merge(org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata) {
107         this.merge((ArtifactMetadata) metadata);
108     }
109 }