Skip to content

Allow package-private @Parameter fields in Maven 4 API plugins #13459

Closed

Description

@gnodet

Summary

In Maven 3, plugin parameters annotated with @Parameter on complex objects (nested configuration classes like <archive>) are injected by Plexus's EnhancedComponentConfigurator via reflection. This configurator requires either public fields or public setters on the target class, because it runs in a different classloader/module and cannot access package-private members without setAccessible(true).

This forces plugin authors to either expose public fields (which violates typical checkstyle VisibilityModifier rules) or write boilerplate getters/setters with no real API value.

Context

While refactoring maven-jar-plugin to remove its maven-archiver dependency (PR apache/maven-jar-plugin#618), we introduced a local ArchiveConfiguration class holding plugin config. Martin Kanters suggested making inner classes package-private with no getters/setters — a reasonable design for an implementation-internal config class. However, this is currently not feasible because EnhancedComponentConfigurator cannot inject into package-private fields.

Proposal

For Maven 4 API plugins (those depending on maven-api-plugin / using the new @Mojo from org.apache.maven.api.plugin.annotations), the mojo parameter configurator should support injecting into package-private fields — either by:

  1. Calling field.setAccessible(true) before reflective access (already done for private fields in some configurators), or
  2. Providing a dedicated, type-safe parameter binding mechanism that doesn't rely on reflection visibility rules.

This would allow Maven 4 API plugins to write clean, encapsulated config classes without public boilerplate:

// Current (forced by Plexus configurator):
public class ArchiveConfiguration {
    private boolean compress = true;
    public boolean isCompress() { return compress; }
    public void setCompress(boolean compress) { this.compress = compress; }
    // ... 15 more getters/setters
}

// Desired (package-private, no boilerplate):
class ArchiveConfiguration {
    boolean compress = true;
    // used directly within the plugin package
}

Target

Maven 4.1.0

Activity

  1. slawekjaranowski commented on Oct 13, 2026

    @slawekjaranowski
    Member

    In Maven 3, plugin parameters annotated with @parameter on complex objects (nested configuration classes like ) are injected by Plexus's EnhancedComponentConfigurator via reflection. This configurator requires either public fields or public setters on the target class, because it runs in a different classloader/module and cannot access package-private members without setAccessible(true).

    Is it true?

    I see https://github.com/apache/maven-jar-plugin/blob/maven-jar-plugin-3.x/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java#L88-L89

    @Parameter
    private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();

    without setters ....

  2. gnodet commented on Oct 13, 2026

    @gnodet
    ContributorAuthor

    PR #13470 fixes this by overriding the instantiateObject() call site in EnhancedConfigurationConverter.fromConfiguration() with a local instantiate() method that uses getDeclaredConstructor() + setAccessible(true) — symmetric with how EnhancedCompositeBeanHelper.setFieldValue() already handles non-public fields.

  3. added a commit that references this issue on Oct 13, 2026
  4. added a commit that references this issue on Oct 22, 2026
    17c5312
  5. added a commit that references this issue on Oct 22, 2026
    d560929
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions