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.common.ArchetypeArtifactManager;
24  import org.apache.maven.archetype.exception.UnknownArchetype;
25  import org.apache.maven.archetype.metadata.ArchetypeDescriptor;
26  import org.apache.maven.archetype.metadata.RequiredProperty;
27  import org.apache.maven.artifact.DependencyResolutionRequiredException;
28  import org.apache.maven.plugin.AbstractMojo;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.MojoFailureException;
31  import org.apache.maven.plugins.annotations.Component;
32  import org.apache.maven.plugins.annotations.LifecyclePhase;
33  import org.apache.maven.plugins.annotations.Mojo;
34  import org.apache.maven.plugins.annotations.Parameter;
35  import org.apache.maven.project.MavenProject;
36  
37  import java.io.File;
38  import java.io.IOException;
39  
40  /**
41   * Build a JAR from the current Archetype project.
42   *
43   * @author rafale
44   */
45  @Mojo( name = "jar", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true )
46  public class JarMojo
47      extends AbstractMojo
48  {
49      /**
50       * Directory containing the classes.
51       */
52      @Parameter( defaultValue = "${project.build.outputDirectory}", required = true )
53      private File archetypeDirectory;
54  
55      /**
56       * Name of the generated JAR.
57       */
58      @Parameter( defaultValue = "${project.build.finalName}", alias = "jarName", required = true )
59      private String finalName;
60  
61      /**
62       * Directory containing the generated JAR.
63       */
64      @Parameter( defaultValue = "${project.build.directory}", required = true )
65      private File outputDirectory;
66  
67      /**
68       * The Maven project.
69       */
70      @Parameter( defaultValue = "${project}", readonly = true, required = true )
71      private MavenProject project;
72  
73      /**
74       * The archetype manager component.
75       */
76      @Component
77      private ArchetypeManager manager;
78  
79      /**
80       * The archetype artifact manager component.
81       */
82      @Component
83      private ArchetypeArtifactManager archetypeArtifactManager;
84  
85      @Override
86      public void execute()
87          throws MojoExecutionException, MojoFailureException
88      {
89          try
90          {
91              getLog().info( "Building archetype jar: " + new File( outputDirectory, finalName ) );
92  
93              File jarFile = manager.archiveArchetype( archetypeDirectory, outputDirectory, finalName );
94  
95              checkArchetypeFile( jarFile );
96  
97              project.getArtifact().setFile( jarFile );
98          }
99          catch ( DependencyResolutionRequiredException ex )
100         {
101             throw new MojoExecutionException( ex.getMessage(), ex );
102         }
103         catch ( IOException ex )
104         {
105             throw new MojoExecutionException( ex.getMessage(), ex );
106         }
107     }
108 
109     private void checkArchetypeFile( File jarFile )
110         throws MojoExecutionException
111     {
112         try
113         {
114             if ( archetypeArtifactManager.isFileSetArchetype( jarFile ) )
115             {
116                 checkFileSetArchetypeFile( jarFile );
117             }
118             else if ( archetypeArtifactManager.isOldArchetype( jarFile ) )
119             {
120                 getLog().warn( "Building an Old (1.x) Archetype: consider migrating it to current 2.x Archetype." );
121             }
122             else
123             {
124                 throw new MojoExecutionException( "The current project does not build an archetype" );
125             }
126         }
127         catch ( UnknownArchetype ua )
128         {
129             throw new MojoExecutionException( ua.getMessage(), ua );
130         }
131     }
132 
133     private void checkFileSetArchetypeFile( File jarFile )
134         throws UnknownArchetype
135     {
136         ArchetypeDescriptor archetypeDescriptor = archetypeArtifactManager.getFileSetArchetypeDescriptor( jarFile );
137 
138         for ( RequiredProperty rp : archetypeDescriptor.getRequiredProperties() )
139         {
140             if ( rp.getKey().contains( "." ) )
141             {
142                 getLog().warn( "Invalid required property name '" + rp.getKey()
143                                    + "': dot character makes is unusable in Velocity template" );
144             }
145         }
146     }
147 }