View Javadoc
1   package org.apache.maven.archetype.mojos;
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.archetype.ArchetypeManager;
23  import org.apache.maven.archetype.catalog.Archetype;
24  import org.apache.maven.execution.MavenSession;
25  import org.apache.maven.plugin.AbstractMojo;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugins.annotations.Component;
28  import org.apache.maven.plugins.annotations.LifecyclePhase;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.apache.maven.project.MavenProject;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  /**
35   * Updates the local catalog
36   *
37   * @author rafale
38   */
39  @Mojo( name = "update-local-catalog", defaultPhase = LifecyclePhase.INSTALL )
40  public class UpdateLocalCatalogMojo
41      extends AbstractMojo
42  {
43      @Parameter( defaultValue = "${session}", readonly = true, required = true )
44      private MavenSession session;
45      
46      @Component
47      private ArchetypeManager manager;
48  
49      /**
50       * The archetype project to add/update to the local catalog.
51       */
52      @Parameter( defaultValue = "${project}", readonly = true, required = true )
53      private MavenProject project;
54  
55      public void execute()
56          throws MojoExecutionException
57      {
58          Archetype archetype = new Archetype();
59          archetype.setGroupId( project.getGroupId() );
60          archetype.setArtifactId( project.getArtifactId() );
61          archetype.setVersion( project.getVersion() );
62  
63          if ( StringUtils.isNotEmpty( project.getDescription() ) )
64          {
65              archetype.setDescription( project.getDescription() );
66          }
67          else
68          {
69              archetype.setDescription( project.getName() );
70          }
71  
72          manager.updateLocalCatalog( session.getProjectBuildingRequest(), archetype );
73      }
74  }