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 | 12 | 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 | 10 | validate( project.getName(), "project.name" ); |
110 | |
|
111 | 10 | validate( project.getDescription(), "project.description" ); |
112 | |
|
113 | 10 | validate( project.getUrl(), "project.url" ); |
114 | |
|
115 | 10 | if ( project.getLicenses().isEmpty() ) |
116 | |
{ |
117 | 0 | throw new MojoExecutionException( "At least one license must be defined." ); |
118 | |
} |
119 | |
|
120 | 10 | if ( disableMaterialization ) |
121 | |
{ |
122 | 0 | 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 | 0 | if ( 'y' != inputHandler.readLine().toLowerCase().charAt( 0 ) ) |
130 | |
{ |
131 | 0 | disableMaterialization = false; |
132 | |
} |
133 | |
} |
134 | 0 | catch ( IOException e ) |
135 | |
{ |
136 | 0 | getLog().debug( "Error reading confirmation: " + e.getMessage(), e ); |
137 | 0 | } |
138 | |
|
139 | |
} |
140 | |
|
141 | 10 | if ( !disableMaterialization ) |
142 | |
{ |
143 | 10 | if ( project.getScm() == null ) |
144 | |
{ |
145 | 1 | 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 | 9 | validate( project.getScm().getUrl(), "project.scm.url" ); |
151 | |
|
152 | 9 | validate( project.getScm().getConnection(), "project.scm.connection" ); |
153 | |
} |
154 | |
} |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | 9 | File pom = project.getFile(); |
161 | |
|
162 | 9 | final String finalName = project.getBuild().getFinalName(); |
163 | |
|
164 | 9 | boolean batchMode = settings == null ? false : !settings.isInteractiveMode(); |
165 | 9 | List<File> files = |
166 | |
BundleUtils.selectProjectFiles( outputDirectory, inputHandler, finalName, pom, getLog(), batchMode ); |
167 | |
|
168 | 9 | File bundle = new File( outputDirectory, finalName + "-bundle.jar" ); |
169 | |
|
170 | |
try |
171 | |
{ |
172 | 9 | jarArchiver.addFile( pom, POM ); |
173 | |
|
174 | 9 | boolean artifactChecks = !"pom".equals( project.getPackaging() ); |
175 | 9 | boolean sourcesFound = false; |
176 | 9 | boolean javadocsFound = false; |
177 | |
|
178 | 9 | for ( File f : files ) |
179 | |
{ |
180 | 13 | if ( artifactChecks && f.getName().endsWith( finalName + "-sources.jar" ) ) |
181 | |
{ |
182 | 3 | sourcesFound = true; |
183 | |
} |
184 | 10 | else if ( artifactChecks && f.getName().equals( finalName + "-javadoc.jar" ) ) |
185 | |
{ |
186 | 2 | javadocsFound = true; |
187 | |
} |
188 | |
|
189 | 13 | jarArchiver.addFile( f, f.getName() ); |
190 | |
} |
191 | |
|
192 | 9 | if ( artifactChecks && !sourcesFound ) |
193 | |
{ |
194 | 5 | 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 | 9 | if ( artifactChecks && !javadocsFound ) |
199 | |
{ |
200 | 6 | 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 | 9 | jarArchiver.setDestFile( bundle ); |
205 | |
|
206 | 9 | jarArchiver.createArchive(); |
207 | |
} |
208 | 0 | catch ( Exception e ) |
209 | |
{ |
210 | 0 | throw new MojoExecutionException( "Error creating upload bundle archive.", e ); |
211 | 9 | } |
212 | 9 | } |
213 | |
|
214 | |
private void validate( String data, String expression ) |
215 | |
throws MojoExecutionException |
216 | |
{ |
217 | 48 | if ( StringUtils.isEmpty( data ) ) |
218 | |
{ |
219 | 0 | throw new MojoExecutionException( expression + " must be present." ); |
220 | |
} |
221 | 48 | } |
222 | |
} |