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