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