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,
044                                File artifactFile )
045    {
046        String type = artifactType;
047
048        ArtifactHandler handler = null;
049
050        if ( type != null )
051        {
052            handler = artifactHandlerManager.getArtifactHandler( artifactType );
053        }
054
055        if ( handler == null )
056        {
057            handler = artifactHandlerManager.getArtifactHandler( "jar" );
058        }
059
060        Artifact artifact = new AttachedArtifact( project.getArtifact(), artifactType, artifactClassifier, handler );
061
062        artifact.setFile( artifactFile );
063        artifact.setResolved( true );
064
065        attachArtifact( project, artifact );
066    }
067
068    public void attachArtifact( MavenProject project, String artifactType, File artifactFile )
069    {
070        ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( artifactType );
071
072        Artifact artifact = new AttachedArtifact( project.getArtifact(), artifactType, handler );
073
074        artifact.setFile( artifactFile );
075        artifact.setResolved( true );
076
077        attachArtifact( project, artifact );
078    }
079
080    public void attachArtifact( MavenProject project, File artifactFile, String artifactClassifier )
081    {
082        Artifact projectArtifact = project.getArtifact();
083
084        Artifact artifact =
085            new AttachedArtifact( projectArtifact, projectArtifact.getType(), artifactClassifier,
086                                  projectArtifact.getArtifactHandler() );
087
088        artifact.setFile( artifactFile );
089        artifact.setResolved( true );
090
091        attachArtifact( project, artifact );
092    }
093
094    /**
095     * Add an attached artifact or replace the file for an existing artifact.
096     *
097     * @see MavenProject#addAttachedArtifact(org.apache.maven.artifact.Artifact)
098     * @param project project reference.
099     * @param artifact artifact to add or replace.
100     */
101    public void attachArtifact( MavenProject project, Artifact artifact )
102    {
103        project.addAttachedArtifact( artifact );
104    }
105
106    public void addResource( MavenProject project, String resourceDirectory, List<String> includes,
107                             List<String> excludes )
108    {
109        Resource resource = new Resource();
110        resource.setDirectory( resourceDirectory );
111        resource.setIncludes( includes );
112        resource.setExcludes( excludes );
113
114        project.addResource( resource );
115    }
116
117    public void addTestResource( MavenProject project, String resourceDirectory, List<String> includes,
118                                 List<String> excludes )
119    {
120        Resource resource = new Resource();
121        resource.setDirectory( resourceDirectory );
122        resource.setIncludes( includes );
123        resource.setExcludes( excludes );
124
125        project.addTestResource( resource );
126    }
127
128}