如何解决Quarkus Maven插件提示的MapStruct生成类私有成员不推荐使用警告?
Absolutely! You can configure MapStruct to generate package-private injected fields instead of private ones, which will resolve that Quarkus warning. Here's how to do it:
The Fix: Configure Field Visibility in MapStruct
MapStruct lets you control the visibility of generated fields via the fieldVisibility parameter on the @Mapper annotation (or a shared @MapperConfig for multiple mappers). For your use case, you'll want to set it to AccessLevel.PACKAGE_PRIVATE.
Modified Mapper Interface
Update your SampleMapper to include the visibility setting:
import org.mapstruct.AccessLevel; import org.mapstruct.Mapper; @Mapper( componentModel = "cdi", uses = SampleFactory.class, fieldVisibility = AccessLevel.PACKAGE_PRIVATE ) public interface SampleMapper { // 映射函数 }
Generated Implementation Result
With this change, MapStruct will generate the SampleMapperImpl with a package-private sampleFactory field, matching Quarkus's recommendation:
@ApplicationScoped public class SampleMapperImpl implements SampleMapper { SampleFactory sampleFactory; // No private modifier - package-private // 映射函数 }
Bonus: Centralized Configuration for Multiple Mappers
If you have multiple MapStruct mappers in your project, you can avoid repeating the setting by creating a shared @MapperConfig class:
import org.mapstruct.AccessLevel; import org.mapstruct.MapperConfig; @MapperConfig( componentModel = "cdi", fieldVisibility = AccessLevel.PACKAGE_PRIVATE ) public interface SharedMapperConfig {}
Then reference this config in your individual mappers:
@Mapper(uses = SampleFactory.class, config = SharedMapperConfig.class) public interface SampleMapper { // 映射函数 }
This feature is supported in MapStruct 1.3 and later, so your 1.4.2.Final version works perfectly with it. The Quarkus warning exists because its CDI proxying mechanism operates more efficiently with package-private members (no need to generate accessor methods for private fields), so this fix aligns with both tools' best practices.
内容的提问来源于stack exchange,提问作者Jaqueline Schweigert

