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  import javax.inject.Inject;
24  import javax.inject.Named;
25  import javax.inject.Singleton;
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.handler.ArtifactHandler;
28  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
29  import org.apache.maven.model.Resource;
30  import org.apache.maven.project.artifact.AttachedArtifact;
31  import org.codehaus.plexus.logging.AbstractLogEnabled;
32  
33  /**
34   * DefaultMavenProjectHelper
35   */
36  @SuppressWarnings("deprecation")
37  @Named
38  @Singleton
39  public class DefaultMavenProjectHelper extends AbstractLogEnabled implements MavenProjectHelper {
40      private final ArtifactHandlerManager artifactHandlerManager;
41  
42      @Inject
43      public DefaultMavenProjectHelper(ArtifactHandlerManager artifactHandlerManager) {
44          this.artifactHandlerManager = artifactHandlerManager;
45      }
46  
47      public void attachArtifact(
48              MavenProject project, String artifactType, String artifactClassifier, File artifactFile) {
49          ArtifactHandler handler = null;
50  
51          if (artifactType != null) {
52              handler = artifactHandlerManager.getArtifactHandler(artifactType);
53          }
54  
55          if (handler == null) {
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          ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(artifactType);
69  
70          Artifact artifact = new AttachedArtifact(project.getArtifact(), artifactType, handler);
71  
72          artifact.setFile(artifactFile);
73          artifact.setResolved(true);
74  
75          attachArtifact(project, artifact);
76      }
77  
78      public void attachArtifact(MavenProject project, File artifactFile, String artifactClassifier) {
79          Artifact projectArtifact = project.getArtifact();
80  
81          Artifact artifact = new AttachedArtifact(
82                  projectArtifact, projectArtifact.getType(), artifactClassifier, projectArtifact.getArtifactHandler());
83  
84          artifact.setFile(artifactFile);
85          artifact.setResolved(true);
86  
87          attachArtifact(project, artifact);
88      }
89  
90      /**
91       * Add an attached artifact or replace the file for an existing artifact.
92       *
93       * @see MavenProject#addAttachedArtifact(org.apache.maven.artifact.Artifact)
94       * @param project project reference.
95       * @param artifact artifact to add or replace.
96       */
97      public void attachArtifact(MavenProject project, Artifact artifact) {
98          project.addAttachedArtifact(artifact);
99      }
100 
101     public void addResource(
102             MavenProject project, String resourceDirectory, List<String> includes, List<String> excludes) {
103         Resource resource = new Resource();
104         resource.setDirectory(resourceDirectory);
105         resource.setIncludes(includes);
106         resource.setExcludes(excludes);
107 
108         project.addResource(resource);
109     }
110 
111     public void addTestResource(
112             MavenProject project, String resourceDirectory, List<String> includes, List<String> excludes) {
113         Resource resource = new Resource();
114         resource.setDirectory(resourceDirectory);
115         resource.setIncludes(includes);
116         resource.setExcludes(excludes);
117 
118         project.addTestResource(resource);
119     }
120 }