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.ArchetypeManager;
26  import org.apache.maven.archetype.catalog.Archetype;
27  import org.apache.maven.archetype.common.Constants;
28  import org.apache.maven.execution.MavenSession;
29  import org.apache.maven.plugin.AbstractMojo;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugins.annotations.LifecyclePhase;
32  import org.apache.maven.plugins.annotations.Mojo;
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.apache.maven.project.MavenProject;
35  import org.codehaus.plexus.util.StringUtils;
36  
37  /**
38   * Updates the local catalog
39   *
40   * @author rafale
41   */
42  @Mojo(name = "update-local-catalog", defaultPhase = LifecyclePhase.INSTALL)
43  public class UpdateLocalCatalogMojo extends AbstractMojo {
44      @Parameter(defaultValue = "${session}", readonly = true, required = true)
45      private MavenSession session;
46  
47      /**
48       * The archetype project to add/update to the local catalog.
49       */
50      @Parameter(defaultValue = "${project}", readonly = true, required = true)
51      private MavenProject project;
52  
53      private ArchetypeManager manager;
54  
55      @Inject
56      public UpdateLocalCatalogMojo(ArchetypeManager manager) {
57          this.manager = manager;
58      }
59  
60      @Override
61      public void execute() throws MojoExecutionException {
62          if (!Constants.MAVEN_ARCHETYPE_PACKAGING.equalsIgnoreCase(project.getPackaging())) {
63              getLog().debug("Wrong packaging type " + project.getPackaging() + ", skipping archetype "
64                      + project.getName());
65              return;
66          }
67          Archetype archetype = new Archetype();
68          archetype.setGroupId(project.getGroupId());
69          archetype.setArtifactId(project.getArtifactId());
70          archetype.setVersion(project.getVersion());
71  
72          if (StringUtils.isNotEmpty(project.getDescription())) {
73              archetype.setDescription(project.getDescription());
74          } else {
75              archetype.setDescription(project.getName());
76          }
77  
78          File catalog = manager.updateLocalCatalog(session.getRepositorySession(), archetype);
79          if (catalog != null) {
80              getLog().info("Updated local archetypes catalog " + catalog);
81          }
82      }
83  }