-
Notifications
You must be signed in to change notification settings - Fork 20
Skip dependencies with no resolved artifact file instead of throwing NPE #386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ | |
|
|
||
| import org.apache.maven.artifact.Artifact; | ||
| import org.apache.maven.artifact.DependencyResolutionRequiredException; | ||
| import org.apache.maven.artifact.repository.ArtifactRepository; | ||
|
Check warning on line 36 in src/main/java/org/apache/maven/plugins/antrun/AntRunMojo.java
|
||
| import org.apache.maven.execution.MavenSession; | ||
| import org.apache.maven.plugin.AbstractMojo; | ||
| import org.apache.maven.plugin.MojoExecutionException; | ||
|
|
@@ -50,7 +50,7 @@ | |
| import org.apache.tools.ant.taskdefs.Typedef; | ||
| import org.apache.tools.ant.types.Path; | ||
| import org.codehaus.plexus.configuration.PlexusConfiguration; | ||
| import org.codehaus.plexus.util.ReaderFactory; | ||
|
Check warning on line 53 in src/main/java/org/apache/maven/plugins/antrun/AntRunMojo.java
|
||
|
|
||
| /** | ||
| * <p> | ||
|
|
@@ -133,7 +133,7 @@ | |
| * The local Maven repository | ||
| */ | ||
| @Parameter(property = "localRepository", readonly = true) | ||
| protected ArtifactRepository localRepository; | ||
|
Check warning on line 136 in src/main/java/org/apache/maven/plugins/antrun/AntRunMojo.java
|
||
|
|
||
| /** | ||
| * String to prepend to project and dependency property names. | ||
|
|
@@ -429,11 +429,7 @@ | |
|
|
||
| // Add properties for dependency artifacts | ||
| Set<Artifact> depArtifacts = mavenProject.getArtifacts(); | ||
| for (Artifact artifact : depArtifacts) { | ||
| String propName = artifact.getDependencyConflictId(); | ||
|
|
||
| antProject.setProperty(propertyPrefix + propName, artifact.getFile().getPath()); | ||
| } | ||
| setDependencyFileProperties(depArtifacts, antProject); | ||
|
|
||
| // Add a property containing the list of versions for the mapper | ||
| StringBuilder versionsBuffer = new StringBuilder(); | ||
|
|
@@ -443,6 +439,37 @@ | |
| antProject.setProperty(versionsPropertyName, versionsBuffer.toString()); | ||
| } | ||
|
|
||
| /** | ||
| * Registers one <code>${propertyPrefix}${dependencyConflictId}</code> property per dependency, | ||
| * holding the path to that dependency's artifact file. | ||
| * <p> | ||
| * An artifact's file may be <code>null</code> when it was never resolved, for instance under | ||
| * partial or offline resolution. Such dependencies are skipped with a warning naming them, | ||
| * rather than aborting the build: the property is simply absent, so a build that never | ||
| * references it still runs. | ||
| * | ||
| * @param depArtifacts the project's dependency artifacts, may be null | ||
| * @param antProject the Ant project to set properties on, not null | ||
| */ | ||
| void setDependencyFileProperties(Set<Artifact> depArtifacts, Project antProject) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should really be private |
||
| if (depArtifacts == null) { | ||
| return; | ||
| } | ||
|
|
||
| for (Artifact artifact : depArtifacts) { | ||
| String propName = artifact.getDependencyConflictId(); | ||
|
|
||
| File artifactFile = artifact.getFile(); | ||
| if (artifactFile == null) { | ||
| getLog().warn("Not setting property \"" + propertyPrefix + propName + "\": dependency " | ||
| + artifact.getId() + " has no resolved artifact file."); | ||
| continue; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can avoid continue with an else block |
||
| } | ||
|
|
||
| antProject.setProperty(propertyPrefix + propName, artifactFile.getPath()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Copy properties from the Ant project to the Maven project. | ||
| * | ||
|
|
@@ -540,7 +567,7 @@ | |
| return null; | ||
| } | ||
|
|
||
| try (LineNumberReader reader = new LineNumberReader(ReaderFactory.newXmlReader(antFile))) { | ||
|
Check warning on line 570 in src/main/java/org/apache/maven/plugins/antrun/AntRunMojo.java
|
||
| for (String line = reader.readLine(); line != null; line = reader.readLine()) { | ||
| if (reader.getLineNumber() == buildException.getLocation().getLineNumber()) { | ||
| return "around Ant part ..." + line.trim() + "... @ " | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.maven.plugins.antrun; | ||
|
|
||
| import java.io.File; | ||
| import java.lang.reflect.Field; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.Set; | ||
|
|
||
| import org.apache.maven.artifact.Artifact; | ||
| import org.apache.maven.artifact.DefaultArtifact; | ||
| import org.apache.maven.artifact.handler.DefaultArtifactHandler; | ||
| import org.apache.maven.artifact.versioning.VersionRange; | ||
| import org.apache.tools.ant.Project; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| /** | ||
| * A dependency whose artifact was never resolved has a null file. Registering the | ||
| * per-dependency path properties must not fall over on it. | ||
| */ | ||
| class AntRunMojoDependencyPropertiesTest { | ||
|
|
||
| /** | ||
| * execute() defaults propertyPrefix to "" before any of this runs; a directly | ||
| * constructed mojo has not been through that, so mirror it here. | ||
| */ | ||
| private static AntRunMojo mojoWithEmptyPrefix() throws Exception { | ||
| AntRunMojo mojo = new AntRunMojo(null); | ||
| Field prefix = AntRunMojo.class.getDeclaredField("propertyPrefix"); | ||
| prefix.setAccessible(true); | ||
| prefix.set(mojo, ""); | ||
| return mojo; | ||
| } | ||
|
|
||
| private static Artifact artifact(String artifactId, File file) { | ||
| Artifact artifact = new DefaultArtifact( | ||
| "org.example", | ||
| artifactId, | ||
| VersionRange.createFromVersion("1.0"), | ||
| "compile", | ||
| "jar", | ||
| null, | ||
| new DefaultArtifactHandler("jar")); | ||
| artifact.setFile(file); | ||
| return artifact; | ||
| } | ||
|
|
||
| @Test | ||
| void unresolvedDependencyIsSkippedInsteadOfThrowing() throws Exception { | ||
| Set<Artifact> artifacts = new LinkedHashSet<>(); | ||
| artifacts.add(artifact("unresolved", null)); | ||
|
|
||
| Project antProject = new Project(); | ||
| AntRunMojo mojo = mojoWithEmptyPrefix(); | ||
|
|
||
| assertDoesNotThrow(() -> mojo.setDependencyFileProperties(artifacts, antProject)); | ||
| assertNull( | ||
| antProject.getProperty("org.example:unresolved:jar"), | ||
| "no path property should be set for a dependency with no artifact file"); | ||
| } | ||
|
|
||
| @Test | ||
| void resolvedDependenciesStillGetTheirPathProperty() throws Exception { | ||
| File resolved = new File("target", "resolved-1.0.jar"); | ||
|
|
||
| Set<Artifact> artifacts = new LinkedHashSet<>(); | ||
| artifacts.add(artifact("unresolved", null)); | ||
| artifacts.add(artifact("resolved", resolved)); | ||
|
|
||
| Project antProject = new Project(); | ||
| AntRunMojo mojo = mojoWithEmptyPrefix(); | ||
|
|
||
| mojo.setDependencyFileProperties(artifacts, antProject); | ||
|
|
||
| // The unresolved artifact is listed first, so this also proves one bad | ||
| // dependency no longer prevents the rest from being registered. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete "no longer" |
||
| assertEquals( | ||
| resolved.getPath(), | ||
| antProject.getProperty("org.example:resolved:jar"), | ||
| "resolved dependencies must still get their path property"); | ||
| } | ||
|
|
||
| @Test | ||
| void nullArtifactSetIsTolerated() throws Exception { | ||
| Project antProject = new Project(); | ||
| AntRunMojo mojo = mojoWithEmptyPrefix(); | ||
|
|
||
| assertDoesNotThrow(() -> mojo.setDependencyFileProperties(null, antProject)); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
run-on sentence