001package org.apache.maven.project.artifact;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.io.IOException;
024
025import org.apache.maven.artifact.Artifact;
026import org.apache.maven.artifact.metadata.AbstractArtifactMetadata;
027import org.apache.maven.artifact.metadata.ArtifactMetadata;
028import org.apache.maven.artifact.repository.ArtifactRepository;
029import org.apache.maven.artifact.repository.metadata.RepositoryMetadataStoreException;
030import org.codehaus.plexus.util.FileUtils;
031
032/**
033 * Attach a POM to an artifact.
034 *
035 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
036 */
037public class ProjectArtifactMetadata
038    extends AbstractArtifactMetadata
039{
040    private final File file;
041
042    public ProjectArtifactMetadata( Artifact artifact )
043    {
044        this( artifact, null );
045    }
046
047    public ProjectArtifactMetadata( Artifact artifact, File file )
048    {
049        super( artifact );
050        this.file = file;
051    }
052
053    public File getFile()
054    {
055        return file;
056    }
057
058    public String getRemoteFilename()
059    {
060        return getFilename();
061    }
062
063    public String getLocalFilename( ArtifactRepository repository )
064    {
065        return getFilename();
066    }
067
068    private String getFilename()
069    {
070        return getArtifactId() + "-" + artifact.getVersion() + ".pom";
071    }
072
073    public void storeInLocalRepository( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
074        throws RepositoryMetadataStoreException
075    {
076        File destination =
077            new File( localRepository.getBasedir(), localRepository.pathOfLocalRepositoryMetadata( this,
078                                                                                                   remoteRepository ) );
079
080        // ----------------------------------------------------------------------------
081        // I'm fully aware that the file could just be moved using File.rename but
082        // there are bugs in various JVM that have problems doing this across
083        // different filesystem. So we'll incur the small hit to actually copy
084        // here and be safe. jvz.
085        // ----------------------------------------------------------------------------
086
087        try
088        {
089            FileUtils.copyFile( file, destination );
090        }
091        catch ( IOException e )
092        {
093            throw new RepositoryMetadataStoreException( "Error copying POM to the local repository.", e );
094        }
095    }
096
097    public String toString()
098    {
099        return "project information for " + artifact.getArtifactId() + " " + artifact.getVersion();
100    }
101
102    public boolean storedInArtifactVersionDirectory()
103    {
104        return true;
105    }
106
107    public String getBaseVersion()
108    {
109        return artifact.getBaseVersion();
110    }
111
112    public Object getKey()
113    {
114        return "project " + artifact.getGroupId() + ":" + artifact.getArtifactId();
115    }
116
117    public void merge( ArtifactMetadata metadata )
118    {
119        ProjectArtifactMetadata m = (ProjectArtifactMetadata) metadata;
120        if ( !m.file.equals( file ) )
121        {
122            throw new IllegalStateException( "Cannot add two different pieces of metadata for: " + getKey() );
123        }
124    }
125
126    public void merge( org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata )
127    {
128        this.merge( (ArtifactMetadata) metadata );
129    }
130}