View Javadoc
1   package org.apache.maven.project;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.List;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.handler.ArtifactHandler;
27  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
28  import org.apache.maven.model.Resource;
29  import org.apache.maven.project.artifact.AttachedArtifact;
30  import org.codehaus.plexus.component.annotations.Component;
31  import org.codehaus.plexus.component.annotations.Requirement;
32  import org.codehaus.plexus.logging.AbstractLogEnabled;
33  
34  /**
35   * DefaultMavenProjectHelper
36   */
37  @SuppressWarnings( "deprecation" )
38  @Component( role = MavenProjectHelper.class )
39  public class DefaultMavenProjectHelper
40      extends AbstractLogEnabled
41      implements MavenProjectHelper
42  {
43      @Requirement
44      private ArtifactHandlerManager artifactHandlerManager;
45  
46      public void attachArtifact( MavenProject project, String artifactType, String artifactClassifier,
47                                  File artifactFile )
48      {
49          String type = artifactType;
50  
51          ArtifactHandler handler = null;
52  
53          if ( type != null )
54          {
55              handler = artifactHandlerManager.getArtifactHandler( artifactType );
56          }
57  
58          if ( handler == null )
59          {
60              handler = artifactHandlerManager.getArtifactHandler( "jar" );
61          }
62  
63          Artifact artifact = new AttachedArtifact( project.getArtifact(), artifactType, artifactClassifier, handler );
64  
65          artifact.setFile( artifactFile );
66          artifact.setResolved( true );
67  
68          attachArtifact( project, artifact );
69      }
70  
71      public void attachArtifact( MavenProject project, String artifactType, File artifactFile )
72      {
73          ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( artifactType );
74  
75          Artifact artifact = new AttachedArtifact( project.getArtifact(), artifactType, handler );
76  
77          artifact.setFile( artifactFile );
78          artifact.setResolved( true );
79  
80          attachArtifact( project, artifact );
81      }
82  
83      public void attachArtifact( MavenProject project, File artifactFile, String artifactClassifier )
84      {
85          Artifact projectArtifact = project.getArtifact();
86  
87          Artifact artifact =
88              new AttachedArtifact( projectArtifact, projectArtifact.getType(), artifactClassifier,
89                                    projectArtifact.getArtifactHandler() );
90  
91          artifact.setFile( artifactFile );
92          artifact.setResolved( true );
93  
94          attachArtifact( project, artifact );
95      }
96  
97      /**
98       * Add an attached artifact or replace the file for an existing artifact.
99       *
100      * @see MavenProject#addAttachedArtifact(org.apache.maven.artifact.Artifact)
101      * @param project project reference.
102      * @param artifact artifact to add or replace.
103      */
104     public void attachArtifact( MavenProject project, Artifact artifact )
105     {
106         project.addAttachedArtifact( artifact );
107     }
108 
109     public void addResource( MavenProject project, String resourceDirectory, List<String> includes,
110                              List<String> excludes )
111     {
112         Resource resource = new Resource();
113         resource.setDirectory( resourceDirectory );
114         resource.setIncludes( includes );
115         resource.setExcludes( excludes );
116 
117         project.addResource( resource );
118     }
119 
120     public void addTestResource( MavenProject project, String resourceDirectory, List<String> includes,
121                                  List<String> excludes )
122     {
123         Resource resource = new Resource();
124         resource.setDirectory( resourceDirectory );
125         resource.setIncludes( includes );
126         resource.setExcludes( excludes );
127 
128         project.addTestResource( resource );
129     }
130 
131 }