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.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   * DefaultMavenProjectHelper
38   */
39  @SuppressWarnings("deprecation")
40  @Singleton
41  @Named
42  public class DefaultMavenProjectHelper implements MavenProjectHelper {
43      protected final Logger logger = LoggerFactory.getLogger(getClass());
44  
45      @Inject
46      private ArtifactHandlerManager artifactHandlerManager;
47  
48      public void attachArtifact(
49              MavenProject project, String artifactType, String artifactClassifier, File artifactFile) {
50          String type = artifactType;
51  
52          ArtifactHandler handler = null;
53  
54          if (type != null) {
55              handler = artifactHandlerManager.getArtifactHandler(artifactType);
56          }
57  
58          if (handler == null) {
59              handler = artifactHandlerManager.getArtifactHandler("jar");
60          }
61  
62          Artifact artifact = new AttachedArtifact(project.getArtifact(), artifactType, artifactClassifier, handler);
63  
64          artifact.setFile(artifactFile);
65          artifact.setResolved(true);
66  
67          attachArtifact(project, artifact);
68      }
69  
70      public void attachArtifact(MavenProject project, String artifactType, File artifactFile) {
71          ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(artifactType);
72  
73          Artifact artifact = new AttachedArtifact(project.getArtifact(), artifactType, handler);
74  
75          artifact.setFile(artifactFile);
76          artifact.setResolved(true);
77  
78          attachArtifact(project, artifact);
79      }
80  
81      public void attachArtifact(MavenProject project, File artifactFile, String artifactClassifier) {
82          Artifact projectArtifact = project.getArtifact();
83  
84          Artifact artifact = new AttachedArtifact(
85                  projectArtifact, projectArtifact.getType(), artifactClassifier, 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         project.addAttachedArtifact(artifact);
102     }
103 
104     public void addResource(
105             MavenProject project, String resourceDirectory, List<String> includes, List<String> excludes) {
106         Resource resource = new Resource();
107         resource.setDirectory(resourceDirectory);
108         resource.setIncludes(includes);
109         resource.setExcludes(excludes);
110 
111         project.addResource(resource);
112     }
113 
114     public void addTestResource(
115             MavenProject project, String resourceDirectory, List<String> includes, List<String> excludes) {
116         Resource resource = new Resource();
117         resource.setDirectory(resourceDirectory);
118         resource.setIncludes(includes);
119         resource.setExcludes(excludes);
120 
121         project.addTestResource(resource);
122     }
123 }