001    package 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    
022    import java.io.File;
023    import java.util.List;
024    
025    import org.apache.maven.artifact.Artifact;
026    import org.apache.maven.artifact.handler.ArtifactHandler;
027    import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
028    import org.apache.maven.model.Resource;
029    import org.apache.maven.project.artifact.AttachedArtifact;
030    import org.codehaus.plexus.component.annotations.Component;
031    import org.codehaus.plexus.component.annotations.Requirement;
032    import org.codehaus.plexus.logging.AbstractLogEnabled;
033    
034    @SuppressWarnings( "deprecation" )
035    @Component( role = MavenProjectHelper.class )
036    public 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        public void attachArtifact( MavenProject project, Artifact artifact )
094        {
095            try
096            {
097                project.addAttachedArtifact( artifact );
098            }
099            catch ( DuplicateArtifactAttachmentException dae )
100            {
101                getLogger().warn( dae.getMessage() );
102    
103                // We can throw this because it's unchecked, and won't change the MavenProjectHelper API, which would break
104                // backward compat if it did.
105                throw dae;
106            }
107        }
108    
109        public void addResource( MavenProject project, String resourceDirectory, List includes, List excludes )
110        {
111            Resource resource = new Resource();
112            resource.setDirectory( resourceDirectory );
113            resource.setIncludes( includes );
114            resource.setExcludes( excludes );
115    
116            project.addResource( resource );
117        }
118    
119        public void addTestResource( MavenProject project, String resourceDirectory, List includes, List excludes )
120        {
121            Resource resource = new Resource();
122            resource.setDirectory( resourceDirectory );
123            resource.setIncludes( includes );
124            resource.setExcludes( excludes );
125    
126            project.addTestResource( resource );
127        }
128    
129    }