View Javadoc
1   package org.apache.maven.plugin.coreit;
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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.factory.ArtifactFactory;
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.project.MavenProject;
27  
28  import java.util.Arrays;
29  import java.util.Collection;
30  import java.util.LinkedHashSet;
31  import java.util.Set;
32  
33  /**
34   * Injects artifacts from the plugin into the dependency artifacts of the project.
35   *
36   * @author Benjamin Bentmann
37   *
38   * @goal inject
39   */
40  public class InjectMojo
41      extends AbstractMojo
42  {
43  
44      /**
45       * The version-less keys in the form <code>groupId:artifactId</code> of the plugin artifacts to inject into
46       * dependency artifacts of the project.
47       *
48       * @parameter
49       */
50      private String[] artifacts;
51  
52      /**
53       * @parameter default-value="${plugin.artifacts}"
54       * @readonly
55       */
56      private Collection pluginArtifacts;
57  
58      /**
59       * The current Maven project.
60       *
61       * @parameter default-value="${project}"
62       * @required
63       * @readonly
64       */
65      private MavenProject project;
66  
67      /**
68       * The artifact factory.
69       *
70       * @component
71       */
72      private ArtifactFactory factory;
73  
74      /**
75       * Runs this mojo.
76       *
77       * @throws MojoExecutionException If an error occured.
78       */
79      public void execute()
80          throws MojoExecutionException
81      {
82          Set artifactKeys = new LinkedHashSet();
83  
84          if ( artifacts != null )
85          {
86              artifactKeys.addAll( Arrays.asList( artifacts ) );
87          }
88  
89          Set dependencyArtifacts = project.getDependencyArtifacts();
90  
91          if ( dependencyArtifacts != null )
92          {
93              dependencyArtifacts = new LinkedHashSet( dependencyArtifacts );
94          }
95          else
96          {
97              dependencyArtifacts = new LinkedHashSet();
98          }
99  
100         for ( Object pluginArtifact : pluginArtifacts )
101         {
102             Artifact artifact = (Artifact) pluginArtifact;
103 
104             String artifactKey = artifact.getGroupId() + ':' + artifact.getArtifactId();
105 
106             if ( artifactKeys.remove( artifactKey ) )
107             {
108                 artifact =
109                     factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
110                                             Artifact.SCOPE_COMPILE, artifact.getType() );
111 
112                 getLog().info( "[MAVEN-CORE-IT-LOG] Injecting dependency artifact " + artifact );
113 
114                 dependencyArtifacts.add( artifact );
115             }
116         }
117 
118         project.setDependencyArtifacts( dependencyArtifacts );
119 
120         getLog().info( "[MAVEN-CORE-IT-LOG] Set dependency artifacts to " + dependencyArtifacts );
121 
122         if ( !artifactKeys.isEmpty() )
123         {
124             getLog().warn( "[MAVEN-CORE-IT-LOG] These artifacts were not found " + artifactKeys );
125         }
126     }
127 
128 }