yjxf vf
05/10/2025, 1:13 PMproject.getDependencies().registerTransform(
AccessTransform.class,
parameters -> {
parameters.parameters(p -> {
p.getAccessTransformerFiles().from(extension.getAccessTransformerFiles());
});
parameters.getFrom().attribute(
ModAccessTransformExtension.TRANSFORM_ACCESS,
false
).attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.JAR_TYPE);
parameters.getTo().attribute(
ModAccessTransformExtension.TRANSFORM_ACCESS,
true
).attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.JAR_TYPE);
}
);
Martin
05/10/2025, 1:18 PMorg.gradle.docstype
and match on "sources"
. Never used that myself but looks like this is the attribute that Gradle uses to indicate sources variantsMartin
05/10/2025, 1:18 PMyjxf vf
05/10/2025, 1:50 PMproject.getDependencies().getArtifactTypes().named(ArtifactTypeDefinition.JAR_TYPE, type -> {
type.getAttributes().attribute(ModAccessTransformExtension.TRANSFORM_ACCESS, false);
});
var objects = project.getObjects();
project.getDependencies().registerTransform(
AccessTransform.class,
spec -> {
spec.parameters(p -> {
p.getAccessTransformerFiles().from(extension.getAccessTransformerFiles());
});
spec.getFrom().attribute(
ModAccessTransformExtension.TRANSFORM_ACCESS,
false
).attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.JAR_TYPE)
.attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType.class, DocsType.SOURCES));
spec.getTo().attribute(
ModAccessTransformExtension.TRANSFORM_ACCESS,
true
).attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.JAR_TYPE)
.attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType.class, DocsType.SOURCES));
}
);
Vampire
05/10/2025, 2:07 PMsources
jar manually,
or uses Gradle to get the Jar with sources
classifier,
or uses an ArtifactResolutionQuery
to get sources,
the transform will not be triggered.
Only if some interested party uses proper variant-aware resolution to download the sources,
the artifact transform would be used, and of course also then only if it would request your additional transformed attribute,
why should it run the transform otherwise?
So before you put any more effort into this,
the question is, for whom do you want to do this sources transformation.
If you do request the sources yourself somewhere and there want to retrieve the transformed source it might work.
For all other cases you probably cannot make it work I guess.yjxf vf
05/10/2025, 2:19 PMVampire
05/10/2025, 2:24 PMyjxf vf
05/10/2025, 2:41 PMJonathing
05/10/2025, 6:37 PMVampire
05/10/2025, 9:54 PMJonathing
05/10/2025, 10:14 PMJonathing
05/10/2025, 10:14 PMVampire
05/10/2025, 10:19 PMabstract class MyTransform : TransformAction<TransformParameters.None> {
@get:InputArtifact
abstract val inputArtifact: Provider<FileSystemLocation>
override fun transform(outputs: TransformOutputs) {
outputs.file("${inputArtifact.get().asFile.name}.txt").writeText("")
}
}
dependencies {
registerTransform(MyTransform::class) {
from
.attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType.SOURCES))
.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.JAR_TYPE)
to
.attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType.SOURCES))
.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, "transformed-jar")
}
}
dependencies {
implementation("io.github.typesafegithub:github-workflows-kt:3.3.0")
}
val foo by tasks.registering {
doLast {
configurations.compileClasspath.get().incoming.artifactView {
withVariantReselection()
isLenient = true
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.DOCUMENTATION))
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EXTERNAL))
attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType.SOURCES))
attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, "transformed-jar")
}
}.files.forEach { println("FOO: $it") }
}
}
Vampire
05/10/2025, 10:23 PMDOCS_TYPE_ATTRIBUTE
from sources
to transformed-sources
, this will not work, because with the artifact transform you have to do withVariantReselection
and during variant selection artifact transforms are not considered, so it couldn't find a matching variant.
With an own attribute, it is the same as with the last case, you would have to make it an attribute of the artifact, not the configuration (like ARTIFACT_TYPE_ATTRIBUTE
), as then the variant reselection can find the proper variant that can then be transformed with an artifact transform so that the artifact attributes are matching.Jonathing
05/10/2025, 10:24 PMVampire
05/10/2025, 10:24 PMVampire
05/10/2025, 10:26 PMAhh, so I guess since IntelliJ and Eclipse have their own method for grabbing sources, it bypasses Gradle's transformations since it isn't using it.As I wrote, I have seen IJ doing both with Gradle projects, using artifact resolution queries or also using source variants. But even if it uses the source variants, it has to do it in a way that you can influence from the build script, so that you can adjust the attributes it uses. If it for example uses a detached configuration you cannot lay your finger on it.
Jonathing
05/10/2025, 10:33 PMVampire
05/10/2025, 10:42 PMOk, then I need to take a closer look at how variant selection and artifact transforms actually work, because it seems like there are still some things I am misunderstanding about them.Yes, they are not easy to grasp. I also needed quite some time and misunderstandings until I was able to half-way wrap my head around them. And I'm still not always sure I got it right. 😄 That there are different levels of artifacts (on configurations and on artifacts), but you request them the same for example. Or that during variant selection transforms do not come into play yet but you first have to find a matching variant of which artifacts you can then transform the attributes (also the configuration-level ones). That latter just here would not work as you need the original one for the variant selection. ...
a) source variants do not declare dependenciesNot really a pitfall, just something you have to keep in mind. For example if you create a configuration that extends another one and on the configuration set the attributes to get the sources, then you will most likely just get the sources of the directly declared dependencies but not for the transitivies. But that might even be what you want.
b) I need to use the ArtifactTypeDefinition attribute since merely selecting the sources docs type attribute doesn't do anything.No, not if we are talking about just retrieving them. In my example it was just used so that the "variant reselection" could be done with the original artifacts and then the artrifact-level attribute was used to trigger the transformation. Just to retrieve the sources of all dependencies (also transitive), that have a proper source variant published, this works just fine:
ependencies {
implementation("io.github.typesafegithub:github-workflows-kt:3.3.0")
}
val foo by tasks.registering {
doLast {
configurations.compileClasspath.get().incoming.artifactView {
withVariantReselection()
isLenient = true
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.DOCUMENTATION))
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EXTERNAL))
attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType.SOURCES))
}
}.files.forEach { println("FOO: $it") }
}
}
Vampire
05/10/2025, 10:45 PMcommons-io:commons-io:2.19.0
and it worked.Jonathing
05/10/2025, 10:45 PMsources
and javadoc
).Vampire
05/10/2025, 10:48 PM"com.fasterxml.jackson.core:jackson-core:2.19.0"
for example it does not work, because they do publish GMM, but do not publish a proper sources variant.Vampire
05/10/2025, 10:50 PMVampire
05/10/2025, 10:51 PMjackson-core
sources can again be retrieved, because then the sources variant is again auto-created from the pom information.Vampire
05/10/2025, 10:51 PMjackson-core
.Vampire
05/10/2025, 10:53 PMjava
component.
Especially before java { withSourcesJar() }
existed this was often made and some did not fix it when that came out.Vampire
05/10/2025, 10:54 PMVampire
05/10/2025, 10:59 PMVampire
05/10/2025, 11:05 PM