1 package org.apache.maven.plugins.repository;
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.plugin.AbstractMojo;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.project.MavenProject;
25 import org.apache.maven.settings.Settings;
26 import org.codehaus.plexus.archiver.jar.JarArchiver;
27 import org.codehaus.plexus.components.interactivity.InputHandler;
28 import org.codehaus.plexus.util.StringUtils;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.List;
33
34
35
36
37
38
39
40
41 public class BundleCreateMojo
42 extends AbstractMojo
43 {
44 public static final String POM = "pom.xml";
45
46
47
48
49
50
51
52 private File outputDirectory;
53
54
55
56
57
58
59
60 private MavenProject project;
61
62
63
64
65
66
67
68 private boolean disableMaterialization;
69
70
71
72
73
74
75 private JarArchiver jarArchiver;
76
77
78
79
80 protected InputHandler inputHandler;
81
82
83
84
85
86 protected Settings settings;
87
88 public void execute()
89 throws MojoExecutionException
90 {
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 validate( project.getName(), "project.name" );
110
111 validate( project.getDescription(), "project.description" );
112
113 validate( project.getUrl(), "project.url" );
114
115 if ( project.getLicenses().isEmpty() )
116 {
117 throw new MojoExecutionException( "At least one license must be defined." );
118 }
119
120 if ( disableMaterialization )
121 {
122 getLog().warn( "Validations to confirm support for project materialization have been DISABLED." +
123 "\n\nYour project may not provide the POM elements necessary to allow users to retrieve sources on-demand," +
124 "\nor to easily checkout your project in an IDE. THIS CAN SERIOUSLY INCONVENIENCE YOUR USERS." +
125 "\n\nContinue? [y/N]" );
126
127 try
128 {
129 if ( 'y' != inputHandler.readLine().toLowerCase().charAt( 0 ) )
130 {
131 disableMaterialization = false;
132 }
133 }
134 catch ( IOException e )
135 {
136 getLog().debug( "Error reading confirmation: " + e.getMessage(), e );
137 }
138
139 }
140
141 if ( !disableMaterialization )
142 {
143 if ( project.getScm() == null )
144 {
145 throw new MojoExecutionException( "You must supply a valid <scm> section, with at least "
146 + "<url> (viewing URL) and <connection> (read-only tooling connection) specified." );
147 }
148 else
149 {
150 validate( project.getScm().getUrl(), "project.scm.url" );
151
152 validate( project.getScm().getConnection(), "project.scm.connection" );
153 }
154 }
155
156
157
158
159
160 File pom = project.getFile();
161
162 final String finalName = project.getBuild().getFinalName();
163
164 boolean batchMode = settings == null ? false : !settings.isInteractiveMode();
165 List<File> files =
166 BundleUtils.selectProjectFiles( outputDirectory, inputHandler, finalName, pom, getLog(), batchMode );
167
168 File bundle = new File( outputDirectory, finalName + "-bundle.jar" );
169
170 try
171 {
172 jarArchiver.addFile( pom, POM );
173
174 boolean artifactChecks = !"pom".equals( project.getPackaging() );
175 boolean sourcesFound = false;
176 boolean javadocsFound = false;
177
178 for ( File f : files )
179 {
180 if ( artifactChecks && f.getName().endsWith( finalName + "-sources.jar" ) )
181 {
182 sourcesFound = true;
183 }
184 else if ( artifactChecks && f.getName().equals( finalName + "-javadoc.jar" ) )
185 {
186 javadocsFound = true;
187 }
188
189 jarArchiver.addFile( f, f.getName() );
190 }
191
192 if ( artifactChecks && !sourcesFound )
193 {
194 getLog().warn( "Sources not included in upload bundle. In order to add sources please run"
195 + " \"mvn source:jar javadoc:jar repository:bundle-create\"" );
196 }
197
198 if ( artifactChecks && !javadocsFound )
199 {
200 getLog().warn( "Javadoc not included in upload bundle. In order to add javadocs please run"
201 + " \"mvn source:jar javadoc:jar repository:bundle-create\"" );
202 }
203
204 jarArchiver.setDestFile( bundle );
205
206 jarArchiver.createArchive();
207 }
208 catch ( Exception e )
209 {
210 throw new MojoExecutionException( "Error creating upload bundle archive.", e );
211 }
212 }
213
214 private void validate( String data, String expression )
215 throws MojoExecutionException
216 {
217 if ( StringUtils.isEmpty( data ) )
218 {
219 throw new MojoExecutionException( expression + " must be present." );
220 }
221 }
222 }