1 package org.apache.maven.archetype.mojos;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
42
43
44
45 @Mojo( name = "jar", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true )
46 public class JarMojo
47 extends AbstractMojo
48 {
49
50
51
52 @Parameter( defaultValue = "${project.build.outputDirectory}", required = true )
53 private File archetypeDirectory;
54
55
56
57
58 @Parameter( defaultValue = "${project.build.finalName}", alias = "jarName", required = true )
59 private String finalName;
60
61
62
63
64 @Parameter( defaultValue = "${project.build.directory}", required = true )
65 private File outputDirectory;
66
67
68
69
70 @Parameter( defaultValue = "${project}", readonly = true, required = true )
71 private MavenProject project;
72
73
74
75
76 @Component
77 private ArchetypeManager manager;
78
79
80
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 }