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