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