我可以通过定义一个 ConfigurationConverter 来解决这个问题(我的目标类型是 AnyLicenseInfo):
@NoArgsConstructor @ToString
public class LicenseConverter extends AbstractBasicConverter {
private static final Class<?> TYPE = AnyLicenseInfo.class;
@Override
public boolean canConvert(Class<?> type) { return type.equals(TYPE); }
@Override
public Object fromString(String string) throws ComponentConfigurationException {
Object object = null;
try {
object =
TYPE.cast(LicenseInfoFactory.parseSPDXLicenseString(string));
} catch (Exception exception) {
String message =
"Unable to convert '" + string + "' to " + TYPE.getName();
throw new ComponentConfigurationException(message, exception);
}
return object;
}
}
使用自定义 ComponentConfigurator 注册它:
@Named("license-mojo-component-configurator")
@NoArgsConstructor @ToString @Slf4j
public class LicenseMojoComponentConfigurator extends BasicComponentConfigurator {
@PostConstruct
public void init() {
converterLookup.registerConverter(new LicenseConverter());
}
@PreDestroy
public void destroy() { }
}
然后在@Mogo 注解中指定配置器:
@Mojo(name = "generate-license-resources",
configurator = "license-mojo-component-configurator",
requiresDependencyResolution = TEST,
defaultPhase = GENERATE_RESOURCES, requiresProject = true)
@NoArgsConstructor @ToString @Slf4j
public class GenerateLicenseResourcesMojo extends AbstractLicenseMojo {