Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import javax.inject.Inject;

import java.io.File;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -112,10 +113,12 @@ public void execute() throws MojoExecutionException {
Set<Artifact> artifacts = project.getArtifacts();

for (Artifact artifact : artifacts) {
project.getProperties()
.setProperty(
artifact.getDependencyConflictId(),
artifact.getFile().getAbsolutePath());
File file = artifact.getFile();
if (file == null) {
throw new MojoExecutionException("Could not resolve artifact " + artifact.getDependencyConflictId()
+ " to a file; the dependency:properties goal requires a resolved artifact.");
}
project.getProperties().setProperty(artifact.getDependencyConflictId(), file.getAbsolutePath());
}

if (extraArtifacts != null) {
Expand All @@ -130,10 +133,12 @@ public void execute() throws MojoExecutionException {
resolverUtil.createArtifactFromParams(paramArtifact);
artifact = resolverUtil.resolveArtifact(artifact, project.getRemoteProjectRepositories());

this.project
.getProperties()
.setProperty(
toConflictId(artifact), artifact.getFile().getAbsolutePath());
File file = artifact.getFile();
if (file == null) {
throw new MojoExecutionException("Could not resolve extra artifact " + toConflictId(artifact)
+ " to a file; the dependency:properties goal requires a resolved artifact.");
}
this.project.getProperties().setProperty(toConflictId(artifact), file.getAbsolutePath());
}
} catch (ArtifactResolutionException | ArtifactDescriptorException e) {
throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
import org.apache.maven.api.plugin.testing.MojoExtension;
import org.apache.maven.api.plugin.testing.MojoTest;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.dependency.utils.ParamArtifact;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.ReflectionUtils;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -102,4 +104,15 @@ void testSetPropertiesForExtractArtifacts(PropertiesMojo mojo) throws Exception
assertTrue(project.getProperties().containsKey(depId2));
assertTrue(new File(project.getProperties().getProperty(depId1)).exists());
}

@Test
@InjectMojo(goal = "properties")
void testNullFileThrowsException(PropertiesMojo mojo) {
Artifact artifact = Mockito.mock(Artifact.class);
when(artifact.getDependencyConflictId()).thenReturn("group:artifact");
when(artifact.getFile()).thenReturn(null);
when(this.project.getArtifacts()).thenReturn(new HashSet<>(Arrays.asList(artifact)));

assertThrows(MojoExecutionException.class, mojo::execute);
}
}
Loading