View Javadoc

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