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      public void execute()
86          throws MojoExecutionException, MojoFailureException
87      {
88          try
89          {
90              getLog().info( "Building archetype jar: " + new File( outputDirectory, finalName ) );
91  
92              File jarFile = manager.archiveArchetype( archetypeDirectory, outputDirectory, finalName );
93  
94              checkArchetypeFile( jarFile );
95  
96              project.getArtifact().setFile( jarFile );
97          }
98          catch ( DependencyResolutionRequiredException ex )
99          {
100             throw new MojoExecutionException( ex.getMessage(), ex );
101         }
102         catch ( IOException ex )
103         {
104             throw new MojoExecutionException( ex.getMessage(), ex );
105         }
106     }
107 
108     private void checkArchetypeFile( File jarFile )
109         throws MojoExecutionException
110     {
111         try
112         {
113             if ( archetypeArtifactManager.isFileSetArchetype( jarFile ) )
114             {
115                 checkFileSetArchetypeFile( jarFile );
116             }
117             else if ( !archetypeArtifactManager.isOldArchetype( jarFile ) )
118             {
119                 getLog().warn( "Building an Old (1.x) Archetype: consider migrating it to current 2.x Archetype." );
120             }
121             else
122             {
123                 throw new MojoExecutionException( "The current project does not built an archetype" );
124             }
125         }
126         catch ( UnknownArchetype ua )
127         {
128             throw new MojoExecutionException( ua.getMessage(), ua );
129         }
130     }
131 
132     private void checkFileSetArchetypeFile( File jarFile )
133         throws UnknownArchetype
134     {
135         ArchetypeDescriptor archetypeDescriptor = archetypeArtifactManager.getFileSetArchetypeDescriptor( jarFile );
136 
137         for ( RequiredProperty rp : archetypeDescriptor.getRequiredProperties() )
138         {
139             if ( rp.getKey().contains( "." ) )
140             {
141                 getLog().warn( "Invalid required property name '" + rp.getKey()
142                                    + "': dot character makes is unusable in Velocity template" );
143             }
144         }
145     }
146 }