23
23
import java .io .IOException ;
24
24
import java .io .InputStream ;
25
25
import java .io .OutputStream ;
26
- import java .io .Reader ;
27
- import java .io .Writer ;
28
26
import java .nio .file .Files ;
29
- import java .util .Enumeration ;
27
+ import java .nio .file .Path ;
28
+ import java .nio .file .StandardCopyOption ;
29
+ import java .util .function .Predicate ;
30
30
import java .util .jar .JarEntry ;
31
31
import java .util .jar .JarFile ;
32
32
import java .util .regex .Pattern ;
43
43
import org .apache .maven .plugins .annotations .Mojo ;
44
44
import org .apache .maven .plugins .annotations .Parameter ;
45
45
import org .codehaus .plexus .util .FileUtils ;
46
- import org .codehaus .plexus .util .IOUtil ;
47
- import org .codehaus .plexus .util .StringUtils ;
48
- import org .codehaus .plexus .util .xml .XmlStreamReader ;
49
- import org .codehaus .plexus .util .xml .XmlStreamWriter ;
50
46
import org .codehaus .plexus .util .xml .pull .XmlPullParserException ;
51
47
import org .eclipse .aether .DefaultRepositoryCache ;
52
48
import org .eclipse .aether .DefaultRepositorySystemSession ;
61
57
import org .eclipse .aether .repository .LocalRepositoryManager ;
62
58
import org .eclipse .aether .util .artifact .SubArtifact ;
63
59
60
+ import static java .util .Objects .isNull ;
61
+ import static java .util .Objects .nonNull ;
62
+
64
63
/**
65
64
* Installs a file in the local repository.
66
65
*
@@ -162,6 +161,10 @@ public class InstallFileMojo extends AbstractMojo {
162
161
@ Parameter (property = "localRepositoryPath" )
163
162
private File localRepositoryPath ;
164
163
164
+ private static final Predicate <String > IS_EMPTY = s -> isNull (s ) || s .isEmpty ();
165
+
166
+ private static final Predicate <String > IS_POM_PACKAGING = "pom" ::equals ;
167
+
165
168
@ Override
166
169
public void execute () throws MojoExecutionException , MojoFailureException {
167
170
if (!file .exists ()) {
@@ -192,17 +195,17 @@ public void execute() throws MojoExecutionException, MojoFailureException {
192
195
193
196
File temporaryPom = null ;
194
197
195
- if (pomFile != null ) {
196
- processModel (readModel (pomFile ));
197
- } else {
198
+ if (pomFile == null ) {
198
199
temporaryPom = readingPomFromJarFile ();
199
200
if (!Boolean .TRUE .equals (generatePom )) {
200
201
pomFile = temporaryPom ;
201
202
getLog ().debug ("Using JAR embedded POM as pomFile" );
202
203
}
204
+ } else {
205
+ processModel (readModel (pomFile ));
203
206
}
204
207
205
- if (groupId == null || artifactId == null || version == null || packaging == null ) {
208
+ if (isNull ( groupId ) || isNull ( artifactId ) || isNull ( version ) || isNull ( packaging ) ) {
206
209
throw new MojoExecutionException ("The artifact information is incomplete: 'groupId', 'artifactId', "
207
210
+ "'version' and 'packaging' are required." );
208
211
}
@@ -213,13 +216,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
213
216
214
217
InstallRequest installRequest = new InstallRequest ();
215
218
216
- boolean isFilePom = classifier == null && "pom" . equals (packaging );
219
+ boolean isFilePom = isNull ( classifier ) && IS_POM_PACKAGING . test (packaging );
217
220
if (!isFilePom ) {
218
221
ArtifactType artifactType =
219
222
repositorySystemSession .getArtifactTypeRegistry ().get (packaging );
220
- if (artifactType != null
221
- && (classifier == null || classifier .isEmpty ())
222
- && !StringUtils .isEmpty (artifactType .getClassifier ())) {
223
+ if (nonNull (artifactType ) && IS_EMPTY .test (classifier ) && !IS_EMPTY .test (artifactType .getClassifier ())) {
223
224
classifier = artifactType .getClassifier ();
224
225
}
225
226
}
@@ -236,17 +237,17 @@ public void execute() throws MojoExecutionException, MojoFailureException {
236
237
+ LS + LS + "File in question is: " + file + LS );
237
238
}
238
239
239
- if (!"pom" .equals (packaging )) {
240
- if (pomFile != null ) {
241
- installRequest .addArtifact (new SubArtifact (mainArtifact , "" , "pom" , pomFile ));
242
- } else {
240
+ if (!IS_POM_PACKAGING .test (packaging )) {
241
+ if (isNull (pomFile )) {
243
242
if (Boolean .TRUE .equals (generatePom ) || (generatePom == null && !pomLocalFile .exists ())) {
244
243
temporaryPom = generatePomFile ();
245
244
getLog ().debug ("Installing generated POM" );
246
245
installRequest .addArtifact (new SubArtifact (mainArtifact , "" , "pom" , temporaryPom ));
247
246
} else if (generatePom == null ) {
248
247
getLog ().debug ("Skipping installation of generated POM, already present in local repository" );
249
248
}
249
+ } else {
250
+ installRequest .addArtifact (new SubArtifact (mainArtifact , "" , "pom" , pomFile ));
250
251
}
251
252
}
252
253
@@ -270,70 +271,40 @@ public void execute() throws MojoExecutionException, MojoFailureException {
270
271
}
271
272
}
272
273
273
- private File readingPomFromJarFile () throws MojoExecutionException {
274
- File pomFile = null ;
275
-
276
- JarFile jarFile = null ;
277
- try {
278
- Pattern pomEntry = Pattern .compile ("META-INF/maven/.*/pom\\ .xml" );
279
-
280
- jarFile = new JarFile (file );
281
-
282
- Enumeration <JarEntry > jarEntries = jarFile .entries ();
283
-
284
- while (jarEntries .hasMoreElements ()) {
285
- JarEntry entry = jarEntries .nextElement ();
286
-
287
- if (pomEntry .matcher (entry .getName ()).matches ()) {
288
- getLog ().debug ("Loading " + entry .getName ());
274
+ private static final Pattern POM_ENTRY_PATTERN = Pattern .compile ("META-INF/maven/.*/pom\\ .xml" );
289
275
290
- InputStream pomInputStream = null ;
291
- OutputStream pomOutputStream = null ;
276
+ private static final Predicate < JarEntry > IS_POM_ENTRY =
277
+ entry -> POM_ENTRY_PATTERN . matcher ( entry . getName ()). matches () ;
292
278
293
- try {
294
- pomInputStream = jarFile .getInputStream (entry );
279
+ private File readingPomFromJarFile () throws MojoExecutionException {
295
280
296
- String base = file .getName ();
297
- if (base .indexOf ('.' ) > 0 ) {
298
- base = base .substring (0 , base .lastIndexOf ('.' ));
299
- }
300
- pomFile = File .createTempFile (base , ".pom" );
281
+ String base = file .getName ();
282
+ if (base .contains ("." )) {
283
+ base = base .substring (0 , base .lastIndexOf ('.' ));
284
+ }
301
285
302
- pomOutputStream = Files . newOutputStream ( pomFile . toPath ());
286
+ try ( JarFile jarFile = new JarFile ( file )) {
303
287
304
- IOUtil . copy ( pomInputStream , pomOutputStream );
288
+ JarEntry pomEntry = jarFile . stream (). filter ( IS_POM_ENTRY ). findAny (). orElse ( null );
305
289
306
- pomOutputStream .close ();
307
- pomOutputStream = null ;
290
+ if (isNull (pomEntry )) {
291
+ // This means there is no entry which matches the "pom.xml"...(or in other words: not packaged by Maven)
292
+ getLog ().info ("pom.xml not found in " + file .getName ());
293
+ return null ;
294
+ }
308
295
309
- pomInputStream .close ();
310
- pomInputStream = null ;
296
+ Path tempPomFile = Files .createTempFile (base , ".pom" );
311
297
312
- processModel ( readModel ( pomFile ) );
298
+ Files . copy ( jarFile . getInputStream ( pomEntry ), tempPomFile , StandardCopyOption . REPLACE_EXISTING );
313
299
314
- break ;
315
- } finally {
316
- IOUtil .close (pomInputStream );
317
- IOUtil .close (pomOutputStream );
318
- }
319
- }
320
- }
300
+ getLog ().debug ("Loading " + pomEntry .getName ());
301
+ processModel (readModel (tempPomFile .toFile ()));
302
+ return tempPomFile .toFile ();
321
303
322
- if (pomFile == null ) {
323
- getLog ().info ("pom.xml not found in " + file .getName ());
324
- }
325
304
} catch (IOException e ) {
326
305
// ignore, artifact not packaged by Maven
327
- } finally {
328
- if (jarFile != null ) {
329
- try {
330
- jarFile .close ();
331
- } catch (IOException e ) {
332
- // we did our best
333
- }
334
- }
306
+ return null ;
335
307
}
336
- return pomFile ;
337
308
}
338
309
339
310
/**
@@ -344,21 +315,14 @@ private File readingPomFromJarFile() throws MojoExecutionException {
344
315
* @throws MojoExecutionException If the POM could not be parsed.
345
316
*/
346
317
private Model readModel (File pomFile ) throws MojoExecutionException {
347
- Reader reader = null ;
348
- try {
349
- reader = new XmlStreamReader (pomFile );
350
- final Model model = new MavenXpp3Reader ().read (reader );
351
- reader .close ();
352
- reader = null ;
353
- return model ;
318
+ try (InputStream reader = Files .newInputStream (pomFile .toPath ())) {
319
+ return new MavenXpp3Reader ().read (reader );
354
320
} catch (FileNotFoundException e ) {
355
321
throw new MojoExecutionException ("File not found " + pomFile , e );
356
322
} catch (IOException e ) {
357
323
throw new MojoExecutionException ("Error reading POM " + pomFile , e );
358
324
} catch (XmlPullParserException e ) {
359
325
throw new MojoExecutionException ("Error parsing POM " + pomFile , e );
360
- } finally {
361
- IOUtil .close (reader );
362
326
}
363
327
}
364
328
@@ -419,21 +383,15 @@ private Model generateModel() {
419
383
*/
420
384
private File generatePomFile () throws MojoExecutionException {
421
385
Model model = generateModel ();
422
-
423
- Writer writer = null ;
424
386
try {
425
- File pomFile = File .createTempFile ("mvninstall" , ".pom" );
426
-
427
- writer = new XmlStreamWriter (pomFile );
428
- new MavenXpp3Writer ().write (writer , model );
429
- writer .close ();
430
- writer = null ;
387
+ File tempPomFile = File .createTempFile ("mvninstall" , ".pom" );
431
388
432
- return pomFile ;
389
+ try (OutputStream writer = Files .newOutputStream (tempPomFile .toPath ())) {
390
+ new MavenXpp3Writer ().write (writer , model );
391
+ return tempPomFile ;
392
+ }
433
393
} catch (IOException e ) {
434
394
throw new MojoExecutionException ("Error writing temporary POM file: " + e .getMessage (), e );
435
- } finally {
436
- IOUtil .close (writer );
437
395
}
438
396
}
439
397
0 commit comments