001package org.apache.maven.project;
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.util.List;
024
025import org.apache.maven.artifact.Artifact;
026import org.apache.maven.artifact.handler.ArtifactHandler;
027import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
028import org.apache.maven.model.Resource;
029import org.apache.maven.project.artifact.AttachedArtifact;
030import org.codehaus.plexus.component.annotations.Component;
031import org.codehaus.plexus.component.annotations.Requirement;
032import org.codehaus.plexus.logging.AbstractLogEnabled;
033
034@SuppressWarnings( "deprecation" )
035@Component( role = MavenProjectHelper.class )
036public class DefaultMavenProjectHelper
037    extends AbstractLogEnabled
038    implements MavenProjectHelper
039{
040    @Requirement
041    private ArtifactHandlerManager artifactHandlerManager;
042
043    public void attachArtifact( MavenProject project, String artifactType, String artifactClassifier, File artifactFile )
044    {
045        String type = artifactType;
046
047        ArtifactHandler handler = null;
048
049        if ( type != null )
050        {
051            handler = artifactHandlerManager.getArtifactHandler( artifactType );
052        }
053
054        if ( handler == null )
055        {
056            handler = artifactHandlerManager.getArtifactHandler( "jar" );
057        }
058
059        Artifact artifact = new AttachedArtifact( project.getArtifact(), artifactType, artifactClassifier, handler );
060
061        artifact.setFile( artifactFile );
062        artifact.setResolved( true );
063
064        attachArtifact( project, artifact );
065    }
066
067    public void attachArtifact( MavenProject project, String artifactType, File artifactFile )
068    {
069        ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( artifactType );
070
071        Artifact artifact = new AttachedArtifact( project.getArtifact(), artifactType, handler );
072
073        artifact.setFile( artifactFile );
074        artifact.setResolved( true );
075
076        attachArtifact( project, artifact );
077    }
078
079    public void attachArtifact( MavenProject project, File artifactFile, String artifactClassifier )
080    {
081        Artifact projectArtifact = project.getArtifact();
082
083        Artifact artifact =
084            new AttachedArtifact( projectArtifact, projectArtifact.getType(), artifactClassifier,
085                                  projectArtifact.getArtifactHandler() );
086
087        artifact.setFile( artifactFile );
088        artifact.setResolved( true );
089
090        attachArtifact( project, artifact );
091    }
092
093    /**
094     * Add an attached artifact or replace the file for an existing artifact.
095     * 
096     * @see MavenProject#addAttachedArtifact(org.apache.maven.artifact.Artifact)
097     * @param project project reference.
098     * @param artifact artifact to add or replace.
099     */
100    public void attachArtifact( MavenProject project, Artifact artifact )
101    {
102        project.addAttachedArtifact( artifact );
103    }
104
105    public void addResource( MavenProject project, String resourceDirectory, List<String> includes,
106                             List<String> excludes )
107    {
108        Resource resource = new Resource();
109        resource.setDirectory( resourceDirectory );
110        resource.setIncludes( includes );
111        resource.setExcludes( excludes );
112
113        project.addResource( resource );
114    }
115
116    public void addTestResource( MavenProject project, String resourceDirectory, List<String> includes,
117                                 List<String> excludes )
118    {
119        Resource resource = new Resource();
120        resource.setDirectory( resourceDirectory );
121        resource.setIncludes( includes );
122        resource.setExcludes( excludes );
123
124        project.addTestResource( resource );
125    }
126
127}