Skip to content

attachartifact throws ClassCastException when mavenProjectRefId points at the plain MavenProject reference #375

Description

@elharo

Summary

AttachArtifactTask casts the Maven project reference to MavenAntRunProject unconditionally. If the mavenProjectRefId attribute is set to a reference that holds a plain MavenProject (e.g. the maven.project reference the antrun mojo registers), the task throws a raw ClassCastException instead of a clear BuildException.

Affected code

src/main/java/org/apache/maven/ant/tasks/AttachArtifactTask.java lines 62-72 (master @ 441382c)

if (this.getProject().getReference(mavenProjectRefId) == null) {
    throw new BuildException("Maven project reference not found: " + mavenProjectRefId);
}

String type = configuration.getType();
if (type == null) {
    type = FileUtils.getExtension(file.getName());
}

MavenProject mavenProject =
        ((MavenAntRunProject) this.getProject().getReference(mavenProjectRefId)).getMavenProject();

Problem

The task's default reference (maven.project.ref) is a MavenAntRunProject wrapper, and the cast at line 71 is only valid for that wrapper. The mavenProjectRefId attribute is user-configurable, but any value resolving to a different type — most naturally maven.project, the plain MavenProject reference that the AntRunMojo also registers (AntRunMojo.java:350) — results in a ClassCastException with no indication of the real problem.

Expected behavior

The reference should be handled by type (e.g. check instanceof and unwrap MavenProject or MavenAntRunProject accordingly), or reject an incompatible reference with a clear BuildException, rather than crashing with a ClassCastException.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions