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