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.artifact.repository.ArtifactRepository;
25  import org.apache.maven.model.Dependency;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.project.MavenProject;
28  
29  import java.io.File;
30  import java.util.List;
31  import java.util.Properties;
32  
33  /**
34   * Builds the remote POMs of user-specified artifacts. This mimics in part the Maven Remote Resources Plugin.
35   *
36   * @author Benjamin Bentmann
37   *
38   * @goal remote-pom
39   */
40  public class BuildRemotePomMojo
41      extends AbstractPomMojo
42  {
43  
44      /**
45       * The properties file to dump the POM info to.
46       *
47       * @parameter default-value="target/pom.properties"
48       */
49      private File propertiesFile;
50  
51      /**
52       * The local repository.
53       *
54       * @parameter default-value="${localRepository}"
55       * @readonly
56       * @required
57       */
58      private ArtifactRepository localRepository;
59  
60      /**
61       * The remote repositories of the current Maven project.
62       *
63       * @parameter default-value="${project.remoteArtifactRepositories}"
64       * @readonly
65       * @required
66       */
67      private List remoteRepositories;
68  
69      /**
70       * The artifact factory.
71       *
72       * @component
73       */
74      private ArtifactFactory factory;
75  
76      /**
77       * The dependencies to resolve.
78       *
79       * @parameter
80       */
81      private Dependency[] dependencies;
82  
83      /**
84       * Runs this mojo.
85       *
86       * @throws MojoExecutionException If the artifact file has not been set.
87       */
88      public void execute()
89          throws MojoExecutionException
90      {
91          Properties props = new Properties();
92  
93          getLog().info( "[MAVEN-CORE-IT-LOG] Building remote POMs" );
94  
95          if ( dependencies != null )
96          {
97              for ( Dependency dependency : dependencies )
98              {
99                  Artifact artifact =
100                     factory.createArtifactWithClassifier( dependency.getGroupId(), dependency.getArtifactId(),
101                                                           dependency.getVersion(), dependency.getType(),
102                                                           dependency.getClassifier() );
103 
104                 String id = artifact.getId();
105 
106                 getLog().info( "[MAVEN-CORE-IT-LOG] Building " + id );
107 
108                 try
109                 {
110                     MavenProject project = builder.buildFromRepository( artifact, remoteRepositories, localRepository );
111 
112                     dump( props, id + ".", project );
113                 }
114                 catch ( Exception e )
115                 {
116                     getLog().warn( "Failed to build remote POM for " + artifact.getId(), e );
117                 }
118 
119                 put( props, id + ".file", artifact.getFile() );
120                 put( props, id + ".version", artifact.getVersion() );
121             }
122         }
123 
124         store( props, propertiesFile );
125     }
126 
127 }