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 org.apache.maven.artifact.Artifact;
023import org.apache.maven.artifact.handler.ArtifactHandler;
024import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
025import org.apache.maven.model.Resource;
026import org.apache.maven.project.artifact.AttachedArtifact;
027import org.codehaus.plexus.component.annotations.Component;
028import org.codehaus.plexus.component.annotations.Requirement;
029import org.codehaus.plexus.logging.AbstractLogEnabled;
030
031import java.io.File;
032import java.util.List;
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 = 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     * @see MavenProject#addAttachedArtifact(org.apache.maven.artifact.Artifact)
096     * @param project project reference.
097     * @param artifact artifact to add or replace.
098     */
099    public void attachArtifact( MavenProject project, Artifact artifact )
100    {
101        project.addAttachedArtifact( artifact );
102    }
103
104    public void addResource( MavenProject project, String resourceDirectory, List<String> includes, List<String> excludes )
105    {
106        Resource resource = new Resource();
107        resource.setDirectory( resourceDirectory );
108        resource.setIncludes( includes );
109        resource.setExcludes( excludes );
110
111        project.addResource( resource );
112    }
113
114    public void addTestResource( MavenProject project, String resourceDirectory, List<String> includes, List<String> excludes )
115    {
116        Resource resource = new Resource();
117        resource.setDirectory( resourceDirectory );
118        resource.setIncludes( includes );
119        resource.setExcludes( excludes );
120
121        project.addTestResource( resource );
122    }
123
124}