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.archetype.mojos;
20  
21  import javax.inject.Inject;
22  
23  import java.io.File;
24  
25  import org.apache.maven.archetype.catalog.ArchetypeCatalog;
26  import org.apache.maven.archetype.repositorycrawler.RepositoryCrawler;
27  import org.apache.maven.plugin.AbstractMojo;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  
32  /**
33   * Crawl a Maven repository (filesystem, not HTTP) and creates a catalog file.
34   *
35   * @author rafale
36   */
37  @Mojo(name = "crawl", requiresProject = false)
38  public class CrawlRepositoryMojo extends AbstractMojo {
39      /**
40       * The archetype's catalog to update.
41       */
42      @Parameter(property = "catalog")
43      private File catalogFile;
44  
45      private RepositoryCrawler crawler;
46  
47      @Inject
48      public CrawlRepositoryMojo(RepositoryCrawler crawler) {
49          this.crawler = crawler;
50      }
51  
52      /**
53       * The repository to crawl.
54       */
55      @Parameter(property = "repository", defaultValue = "${settings.localRepository}")
56      private File repository;
57  
58      @Override
59      public void execute() throws MojoExecutionException {
60          getLog().debug("repository " + repository + ", catalogFile " + catalogFile);
61  
62          if (repository == null) {
63              throw new MojoExecutionException("The repository is not defined. Use -Drepository=/path/to/repository");
64          }
65  
66          ArchetypeCatalog catalog = crawler.crawl(repository);
67  
68          if (catalogFile == null) {
69              catalogFile = new File(repository, "archetype-catalog.xml");
70          }
71  
72          crawler.writeCatalog(catalog, catalogFile);
73      }
74  }