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  @SuppressWarnings( "deprecation" )
35  @Component( role = MavenProjectHelper.class )
36  public class DefaultMavenProjectHelper
37      extends AbstractLogEnabled
38      implements MavenProjectHelper
39  {
40      @Requirement
41      private ArtifactHandlerManager artifactHandlerManager;
42  
43      public void attachArtifact( MavenProject project, String artifactType, String artifactClassifier, File artifactFile )
44      {
45          String type = artifactType;
46  
47          ArtifactHandler handler = null;
48  
49          if ( type != null )
50          {
51              handler = artifactHandlerManager.getArtifactHandler( artifactType );
52          }
53  
54          if ( handler == null )
55          {
56              handler = artifactHandlerManager.getArtifactHandler( "jar" );
57          }
58  
59          Artifact artifact = new AttachedArtifact( project.getArtifact(), artifactType, artifactClassifier, handler );
60  
61          artifact.setFile( artifactFile );
62          artifact.setResolved( true );
63  
64          attachArtifact( project, artifact );
65      }
66  
67      public void attachArtifact( MavenProject project, String artifactType, File artifactFile )
68      {
69          ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( artifactType );
70  
71          Artifact artifact = new AttachedArtifact( project.getArtifact(), artifactType, handler );
72  
73          artifact.setFile( artifactFile );
74          artifact.setResolved( true );
75  
76          attachArtifact( project, artifact );
77      }
78  
79      public void attachArtifact( MavenProject project, File artifactFile, String artifactClassifier )
80      {
81          Artifact projectArtifact = project.getArtifact();
82  
83          Artifact artifact =
84              new AttachedArtifact( projectArtifact, projectArtifact.getType(), artifactClassifier,
85                                    projectArtifact.getArtifactHandler() );
86  
87          artifact.setFile( artifactFile );
88          artifact.setResolved( true );
89  
90          attachArtifact( project, artifact );
91      }
92  
93      /**
94       * Add an attached artifact or replace the file for an existing artifact.
95       *
96       * @see MavenProject#addAttachedArtifact(org.apache.maven.artifact.Artifact)
97       * @param project project reference.
98       * @param artifact artifact to add or replace.
99       */
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 }