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