J Barclay J Barclay - 2 months ago 12
Java Question

Java/Maven - Saxon without SeviceLoader override

We are building a common component that is a dependency for multiple other projects.

Our project does some XSLT transformations and we need to use the Saxon engine.

We have full control over the specific XSLT transformation that must use Saxon, but no control over the classpath of the applications that are dependent on us, and we don't want to force them to use Saxon for other XML work.

We can manually invoke the Saxon library directly when doing our transformations using the API provided by those factories.

The problem is that Saxon uses the

pattern to inject itself as the
implementation using this file in the jar:


This means that applications that use us as a dependency might end up using Saxon instead of their existing XML libraries. Asking those applications to change their code to invoke their specific implementation is not an option.

Is there any way we can 'override' the Saxon library to remove the ServiceLoader implementation? Either using Maven, Java or some other process?


Answering this for any future developers with the same issue.

We were not able to find a way to solve this issue. We considered writing a Maven plugin to remove the META-INF/services/ file from the JAR but ultimately decided this was not an appropriate solution.

We are now in the same position we started - dependent applications end up with Saxon as a registered provider and it might override their existing configuration.

For those applications that must use a specific XSLT processer we have asked them to set the system property, e.g. javax.xml.transform.TransformerFactory=org.apache.xalan.processor.TransformerFactoryImpl