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.project.MavenProject;
32
33 import java.io.File;
34 import java.io.IOException;
35
36
37
38
39
40
41
42
43
44 public class JarMojo
45 extends AbstractMojo
46 {
47
48
49
50
51
52
53 private File archetypeDirectory;
54
55
56
57
58
59
60
61 private String finalName;
62
63
64
65
66
67
68
69 private File outputDirectory;
70
71
72
73
74
75
76
77
78 private MavenProject project;
79
80
81
82
83
84
85 private ArchetypeManager manager;
86
87
88
89
90
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 }