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 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 }