Skip to content

Commit

Permalink
Better handling of source sets
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed May 24, 2024
1 parent 82dc772 commit aa88c2f
Showing 1 changed file with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public Extension(Project project) {
this.project = project;
}

public void forSourceSet(SourceSet sourceSet, String capability) {
forSourceSet(sourceSet, capability, settings -> {});
public void forSourceSet(SourceSet sourceSet, String newBaseCapability) {
forSourceSet(sourceSet, newBaseCapability, settings -> {});
}

public void forSourceSet(SourceSet sourceSet, String newBaseCapability, Action<TransformSettings> action) {
Expand Down Expand Up @@ -70,7 +70,7 @@ public void forSourceSet(SourceSet sourceSet, String newBaseCapability, Action<T
var rootPackageJar = project.getTasks().register(sourceSet.getTaskName("rootPackageJar", ""), Jar.class, task -> {
task.dependsOn(transform.get());
task.from(transform.get().getOutputDirectory());
task.getArchiveClassifier().set("");
task.getArchiveClassifier().set("rootpackage-"+sourceSet.getName());
task.from(sourceSet.getOutput().getResourcesDir());

task.from(transformList.get().getListFile(), spec -> {
Expand Down Expand Up @@ -101,7 +101,7 @@ public void forSourceSet(SourceSet sourceSet, String newBaseCapability, Action<T
var rootPackageSourcesJar = project.getTasks().register(sourceSet.getTaskName("rootPackageSourcesJar", ""), Jar.class, task -> {
task.dependsOn(transformSources.get());
task.from(transformSources.get().getDestinationDirectory());
task.getArchiveClassifier().set("sources");
task.getArchiveClassifier().set("rootpackage-"+sourceSet.getName()+"-sources");

task.from(transformList.get().getListFile(), spec -> {
spec.into("META-INF");
Expand Down Expand Up @@ -187,6 +187,9 @@ private void setupElementsCopyOf(SourceSet sourceSet, String newBaseCapability,
rootPackageElements.setCanBeConsumed(true);
rootPackageElements.setCanBeResolved(false);
rootPackageElements.getDependencies().addAllLater(project.provider(originalElements::getAllDependencies));
if (!sourceSet.getName().equals("main")) {
rootPackageElements.getOutgoing().capability(project.getGroup()+":"+project.getName()+"-"+sourceSet.getName()+":"+project.getVersion());
}
copyAttributes(originalElements, rootPackageElements);
originalElements.getOutgoing().capability(newBaseCapability);

Expand All @@ -211,7 +214,9 @@ private void setupElementsCopyOf(SourceSet sourceSet, String newBaseCapability,
});

project.artifacts(artifactHandler ->
artifactHandler.add(rootPackageElements.getName(), rootPackageJar.get())
artifactHandler.add(rootPackageElements.getName(), rootPackageJar.get(), spec -> {
spec.setClassifier(sourceSetClassifier(sourceSet, ""));
})
);

prefixArtifacts(originalElements);
Expand Down Expand Up @@ -241,14 +246,26 @@ private Configuration setupRootPackageSourcesElements(SourceSet sourceSet, Strin
copyAttributes(sourcesElements, rootPackageSourcesElements);
sourcesElements.getOutgoing().capability(newBaseCapability);
project.artifacts(artifactHandler ->
artifactHandler.add(rootPackageSourcesElements.getName(), rootPackageSourcesJar.get())
artifactHandler.add(rootPackageSourcesElements.getName(), rootPackageSourcesJar.get(), spec -> {
spec.setClassifier(sourceSetClassifier(sourceSet, "sources"));
})
);

prefixArtifacts(sourcesElements);

return rootPackageSourcesElements;
}

private static String sourceSetClassifier(SourceSet sourceSet, String classifier) {
if (sourceSet.getName().equals("main")) {
return classifier;
} else if (classifier.isEmpty()) {
return sourceSet.getName();
} else {
return sourceSet.getName() + "-" + classifier;
}
}

public static abstract class TransformSettings {
public abstract Property<Boolean> getTransformSources();

Expand Down

0 comments on commit aa88c2f

Please sign in to comment.