Skip to content

Commit 30d2b53

Browse files
committed
[MINSTALL-192] - Code cleanups
* Reversed inverse logic * readingPomFromJarFile simplyfied a lot. - replaced IOUtil.copy with Files.copy() - Usage of try-with-resources instead of manually calling .close() * Removed StringUtils replaced with a Predicate.
1 parent 429ad5b commit 30d2b53

File tree

2 files changed

+64
-115
lines changed

2 files changed

+64
-115
lines changed

src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java

Lines changed: 48 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import java.io.IOException;
2424
import java.io.InputStream;
2525
import java.io.OutputStream;
26-
import java.io.Reader;
27-
import java.io.Writer;
2826
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;
3030
import java.util.jar.JarEntry;
3131
import java.util.jar.JarFile;
3232
import java.util.regex.Pattern;
@@ -43,10 +43,6 @@
4343
import org.apache.maven.plugins.annotations.Mojo;
4444
import org.apache.maven.plugins.annotations.Parameter;
4545
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;
5046
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
5147
import org.eclipse.aether.DefaultRepositoryCache;
5248
import org.eclipse.aether.DefaultRepositorySystemSession;
@@ -61,6 +57,9 @@
6157
import org.eclipse.aether.repository.LocalRepositoryManager;
6258
import org.eclipse.aether.util.artifact.SubArtifact;
6359

60+
import static java.util.Objects.isNull;
61+
import static java.util.Objects.nonNull;
62+
6463
/**
6564
* Installs a file in the local repository.
6665
*
@@ -162,6 +161,10 @@ public class InstallFileMojo extends AbstractMojo {
162161
@Parameter(property = "localRepositoryPath")
163162
private File localRepositoryPath;
164163

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+
165168
@Override
166169
public void execute() throws MojoExecutionException, MojoFailureException {
167170
if (!file.exists()) {
@@ -192,17 +195,17 @@ public void execute() throws MojoExecutionException, MojoFailureException {
192195

193196
File temporaryPom = null;
194197

195-
if (pomFile != null) {
196-
processModel(readModel(pomFile));
197-
} else {
198+
if (pomFile == null) {
198199
temporaryPom = readingPomFromJarFile();
199200
if (!Boolean.TRUE.equals(generatePom)) {
200201
pomFile = temporaryPom;
201202
getLog().debug("Using JAR embedded POM as pomFile");
202203
}
204+
} else {
205+
processModel(readModel(pomFile));
203206
}
204207

205-
if (groupId == null || artifactId == null || version == null || packaging == null) {
208+
if (isNull(groupId) || isNull(artifactId) || isNull(version) || isNull(packaging)) {
206209
throw new MojoExecutionException("The artifact information is incomplete: 'groupId', 'artifactId', "
207210
+ "'version' and 'packaging' are required.");
208211
}
@@ -213,13 +216,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
213216

214217
InstallRequest installRequest = new InstallRequest();
215218

216-
boolean isFilePom = classifier == null && "pom".equals(packaging);
219+
boolean isFilePom = isNull(classifier) && IS_POM_PACKAGING.test(packaging);
217220
if (!isFilePom) {
218221
ArtifactType artifactType =
219222
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())) {
223224
classifier = artifactType.getClassifier();
224225
}
225226
}
@@ -236,17 +237,17 @@ public void execute() throws MojoExecutionException, MojoFailureException {
236237
+ LS + LS + "File in question is: " + file + LS);
237238
}
238239

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)) {
243242
if (Boolean.TRUE.equals(generatePom) || (generatePom == null && !pomLocalFile.exists())) {
244243
temporaryPom = generatePomFile();
245244
getLog().debug("Installing generated POM");
246245
installRequest.addArtifact(new SubArtifact(mainArtifact, "", "pom", temporaryPom));
247246
} else if (generatePom == null) {
248247
getLog().debug("Skipping installation of generated POM, already present in local repository");
249248
}
249+
} else {
250+
installRequest.addArtifact(new SubArtifact(mainArtifact, "", "pom", pomFile));
250251
}
251252
}
252253

@@ -270,70 +271,40 @@ public void execute() throws MojoExecutionException, MojoFailureException {
270271
}
271272
}
272273

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");
289275

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();
292278

293-
try {
294-
pomInputStream = jarFile.getInputStream(entry);
279+
private File readingPomFromJarFile() throws MojoExecutionException {
295280

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

302-
pomOutputStream = Files.newOutputStream(pomFile.toPath());
286+
try (JarFile jarFile = new JarFile(file)) {
303287

304-
IOUtil.copy(pomInputStream, pomOutputStream);
288+
JarEntry pomEntry = jarFile.stream().filter(IS_POM_ENTRY).findAny().orElse(null);
305289

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

309-
pomInputStream.close();
310-
pomInputStream = null;
296+
Path tempPomFile = Files.createTempFile(base, ".pom");
311297

312-
processModel(readModel(pomFile));
298+
Files.copy(jarFile.getInputStream(pomEntry), tempPomFile, StandardCopyOption.REPLACE_EXISTING);
313299

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();
321303

322-
if (pomFile == null) {
323-
getLog().info("pom.xml not found in " + file.getName());
324-
}
325304
} catch (IOException e) {
326305
// 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;
335307
}
336-
return pomFile;
337308
}
338309

339310
/**
@@ -344,21 +315,14 @@ private File readingPomFromJarFile() throws MojoExecutionException {
344315
* @throws MojoExecutionException If the POM could not be parsed.
345316
*/
346317
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);
354320
} catch (FileNotFoundException e) {
355321
throw new MojoExecutionException("File not found " + pomFile, e);
356322
} catch (IOException e) {
357323
throw new MojoExecutionException("Error reading POM " + pomFile, e);
358324
} catch (XmlPullParserException e) {
359325
throw new MojoExecutionException("Error parsing POM " + pomFile, e);
360-
} finally {
361-
IOUtil.close(reader);
362326
}
363327
}
364328

@@ -419,21 +383,15 @@ private Model generateModel() {
419383
*/
420384
private File generatePomFile() throws MojoExecutionException {
421385
Model model = generateModel();
422-
423-
Writer writer = null;
424386
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");
431388

432-
return pomFile;
389+
try (OutputStream writer = Files.newOutputStream(tempPomFile.toPath())) {
390+
new MavenXpp3Writer().write(writer, model);
391+
return tempPomFile;
392+
}
433393
} catch (IOException e) {
434394
throw new MojoExecutionException("Error writing temporary POM file: " + e.getMessage(), e);
435-
} finally {
436-
IOUtil.close(writer);
437395
}
438396
}
439397

src/main/java/org/apache/maven/plugins/install/InstallMojo.java

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
package org.apache.maven.plugins.install;
2020

2121
import java.io.File;
22-
import java.util.ArrayList;
2322
import java.util.List;
2423
import java.util.Map;
24+
import java.util.function.Predicate;
25+
import java.util.stream.Collectors;
2526

2627
import org.apache.maven.RepositoryUtils;
2728
import org.apache.maven.execution.MavenSession;
@@ -122,15 +123,15 @@ public void execute() throws MojoExecutionException {
122123
getLog().info("Skipping artifact installation");
123124
putState(State.SKIPPED);
124125
} else {
125-
if (!installAtEnd) {
126+
if (installAtEnd) {
127+
getLog().info("Deferring install for " + project.getGroupId() + ":" + project.getArtifactId() + ":"
128+
+ project.getVersion() + " at end");
129+
putState(State.TO_BE_INSTALLED);
130+
} else {
126131
InstallRequest request = new InstallRequest();
127132
processProject(project, request);
128133
installProject(request);
129134
putState(State.INSTALLED);
130-
} else {
131-
getLog().info("Deferring install for " + project.getGroupId() + ":" + project.getArtifactId() + ":"
132-
+ project.getVersion() + " at end");
133-
putState(State.TO_BE_INSTALLED);
134135
}
135136
}
136137

@@ -149,35 +150,25 @@ public void execute() throws MojoExecutionException {
149150
}
150151

151152
private boolean allProjectsMarked(List<MavenProject> allProjectsUsingPlugin) {
152-
for (MavenProject reactorProject : allProjectsUsingPlugin) {
153-
if (!hasState(reactorProject)) {
154-
return false;
155-
}
156-
}
157-
return true;
153+
return allProjectsUsingPlugin.stream().allMatch(this::hasState);
158154
}
159155

156+
private final Predicate<MavenProject> hasMavenInstallPluginExecution =
157+
rp -> hasExecution(rp.getPlugin("org.apache.maven.plugins:maven-install-plugin"));
158+
160159
private List<MavenProject> getAllProjectsUsingPlugin() {
161-
ArrayList<MavenProject> result = new ArrayList<>();
162-
for (MavenProject reactorProject : reactorProjects) {
163-
if (hasExecution(reactorProject.getPlugin("org.apache.maven.plugins:maven-install-plugin"))) {
164-
result.add(reactorProject);
165-
}
166-
}
167-
return result;
160+
return reactorProjects.stream().filter(hasMavenInstallPluginExecution).collect(Collectors.toList());
168161
}
169162

163+
private final Predicate<PluginExecution> havingGoals = pe -> !pe.getGoals().isEmpty();
164+
private final Predicate<PluginExecution> nonePhase = pe -> !"none".equalsIgnoreCase(pe.getPhase());
165+
170166
private boolean hasExecution(Plugin plugin) {
171167
if (plugin == null) {
172168
return false;
173169
}
174170

175-
for (PluginExecution execution : plugin.getExecutions()) {
176-
if (!execution.getGoals().isEmpty() && !"none".equalsIgnoreCase(execution.getPhase())) {
177-
return true;
178-
}
179-
}
180-
return false;
171+
return plugin.getExecutions().stream().filter(havingGoals).anyMatch(nonePhase);
181172
}
182173

183174
private void installProject(InstallRequest request) throws MojoExecutionException {

0 commit comments

Comments
 (0)